hive使用struct、map与array类型字段

栏目: 服务器 · 发布时间: 4年前

内容简介:hive支持struct,map,array三种集合类型与定义一个包含struct字段的表

hive支持struct,map,array三种集合类型

struct

C语言golang 中的struct类似,可以通过 . 语法来访问

定义一个包含struct字段的表

create table test2(field1 struct<name:string,age:int> comment "test field") row format delimited fields terminated by "," collection items terminated by ":";

其中 row format delimited fields terminated by "," 表示字段之间用","分割

collection items terminated by ":"; 字段中各个元素项之间用":"分割

然后导入数据,可以使用 sql 语句和文件的方式,首先是sql语句

insert into test2(field1)values(named_struct("name",'zhangsan',"age",25));
insert into test2(field1)values(named_struct("name","lisi","age",23));

从文件导入,新建一个test.txt,内容如下

name:wangwu|age:27
name:zhaoliu|age:28

使用 load data 语句从本地导入

load data local inpath '/Users/zhangsheng/hive/note/hive/test.txt' into table test2;

使用 . 语法查询数据

-- 查询所有数据
select * from test2;
-- 查询name字段
select field1.name from test2;
-- 查询name为zhangsan的记录
select field1 from test2 where field1.name = "zhangsan";

结果如下

hive使用struct、map与array类型字段

result1.png

hive使用struct、map与array类型字段

result2.png

hive使用struct、map与array类型字段

result3.png

map

同struct, map 的用法基本相似,以下为创建一个map类型字段

create table test3(field2 map<string,string>) row format delimited fields terminated by ',' collection items terminated by "|" map keys terminated by ":";

map keys terminated by ":"表示键值对之间用":"来分割

同样的,先使用sql方式插入

insert into test3(field2)values(str_to_map("name:zhangsan,age:25")),(str_to_map("name:lisi,age:23"));

再导入文件,文件内容如下:

load data local inpath  '/Users/zhangsheng/hive/note/hive/test.txt' into table test3;

map的访问通过 map[key] 的形式进行,测试一下

select * from test3;
select field2["name"] as name,field2["age"] as age from test3;
select * from test3 where field2["age"] > 25;

结果如下

hive使用struct、map与array类型字段

result4.png

hive使用struct、map与array类型字段

result5.png

hive使用struct、map与array类型字段

result6.png

array

与所有支持数组的编程语言一致,使用 array[下标] 的方式访问,创建一个 array 类型字段

create table test4(field4 array<string>);

插入数据

insert into test4(field4)values(array("zhangsan","lisi","wangwu"));
insert into test4(field4)values(`array`("lily","bob","alice"));
insert into test4(field4)values(`array`("A","B","C"));

查询测试

select * from test4;
select field4[0] from test4;
select * from test4 where field4[0]="zhangsan";

结果如下

hive使用struct、map与array类型字段

result7.png

hive使用struct、map与array类型字段

result8.png

hive使用struct、map与array类型字段

result9.png


以上所述就是小编给大家介绍的《hive使用struct、map与array类型字段》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Google's PageRank and Beyond

Google's PageRank and Beyond

Amy N. Langville、Carl D. Meyer / Princeton University Press / 2006-7-23 / USD 57.50

Why doesn't your home page appear on the first page of search results, even when you query your own name? How do other web pages always appear at the top? What creates these powerful rankings? And how......一起来看看 《Google's PageRank and Beyond》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具