加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

hive的三种复合数据类型array、map、struct以及自定义分割符示例

(2017-04-19 22:03:26)
标签:

hive

大数据

分隔符

符合数据类型

array

分类: hive
Hive的数据类型主要有int、boolean、date、array、map、struct等,在这只描述array,map,struct三种。
1.array(等同于数组,可以使用下标来操作相应的元素)

默认分割符下的array,示例如下:
有一群学生,id,name,hobby(多个)
create table t3_arr(
id int,
name string,
hobyy array
) row format delimited
 fields terminated by '\t';
hive> load data local inpath 'data/t1' into table t3_arr;
查看数组中的某一个元素
hive> select id, hobyy[1] from t3_arr;
设置数组中的分隔符
它的默认分隔符为\002,使用ctrl+v ctrl+b可以输入。

自定义分割符,示例如下:
create table t3_arr_1(
id int,
name string,
hobyy array
) row format delimited
 fields terminated by '\t'
 collection items terminted by ',';
使用','作为array中的分割符,在导入数据的文件中,array的元素以','分开的。
例如:
1  张三   唱歌,游泳,看书
加载数据:
hive> load data local inpath 'data/t2' into table t3_arr_1;
查询数据:
hive> select id, name, hobby[0],hobby[1] from t3_arr_1;

2.map(等同于hashmap的操作)
map也是有默认的分隔符的,它的分隔符是\003,对应的输入为ctrl+v ctrl+c,等同array
自定义分割符:
有一群学生,id,name,scores(chinese,math,english)
create table t4_map (
id int,
name string,
scores map
) row format delimited
fields terminated by '\t'
collection items terminated by ','
map keys terminated by ':';
使用','作为map元素的分割符,对于key和value间的分割符使用:,在导入数据的文件中数据示例如下:
1 张三 Chinese:90,english:80,math:70
加载数据:
load data local inpath 'data/t4_map' into table t4_map;
查询map中的特定的值:
hive> select id, name, scores["chinese"] from t4_map;
3.struct
有点想咱们的java中的对象,c语言中的struct,可以容纳多种数据类的数据
create table t5_struct(
id int,
name string,
info struct
) row format delimited
fields terminated by '\t'
collection items terminated by ',';

自定义使用','作为struct的分隔符,在导入数据的文件中,struct元素用','分割,数据示例如下:
1 张三 address,39

查看其中的struct中某一列数据
hive> select id, name, info.subway from t5_struct;

自定义分隔符
row format delimited
列分隔符:fields terminated by ...
集合元素分割 collection items terminated by ...
map:key-value分割 map keys terminated by ...
行分割 lines terminated by ...(一般不写)

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有