1. hive窗口打印库和表头
在hive-site.xml
中加入如下两个配置:
<property>
<name>hive.cli.print.header</name>
<value>true</value>
</property>
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
</property>
2. hive支持的类型
Complex Types
arrays: ARRAY<data_type> (Note: negative values and non-constant expressions are allowed as of Hive 0.14.)
maps: MAP<primitive_type, data_type> (Note: negative values and non-constant expressions are allowed as of Hive 0.14.)
structs: STRUCT<col_name : data_type [COMMENT col_comment], …>
union: UNIONTYPE<data_type, data_type, …> (Note: Only available starting with Hive 0.7.0.)
3. 复杂类型
3.1 ARRAY、MAP 和 STRUCT
数据类型 | 描述 | 语法 |
---|---|---|
STRUCT | 通过点符号 访问元素内容。如列的数据类型是struct<street:string,city:string> ,那么第1个元素可以通过字段名.street 来引用 | STRUCT<col_name : data_type [COMMENT col_comment], ...> |
MAP | MAP是一组键值对元组集合,使用数组表示法可以访问数据。如列的数据类型是map<string,int> ,列某一行的值为{"xiao song":18,"xiaoxiao song":19} ,那么可以通过 字段名['xiao song'] 获取第一个元素对应的值 | MAP<primitive_type, data_type> |
arrays | 数组是一组具有相同类型和名称的变量的集合。这些变量称为数组的元素,每个数组元素都有一个编号,编号从零开始。如数组值为["bingbing","lili"] ,那么第2个元素可以通过字段名[1] 进行引用。 | ARRAY<data_type> |
3.2 复杂类型使用范例
(1)创建本地测试文件test.txt
songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
cz,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
zx,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
zmy,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
tom,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
mike,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
jack,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
(2)创建测试表test
create table test(
name string,
friends array<string>,
children map<string, int>,
address struct<street:string, city:string>
)
row format delimited fields terminated by ','
collection items terminated by '_'
map keys terminated by ':'
lines terminated by '\n';
字段解释:
row format delimited fields terminated by ',' -- 列分隔符
collection items terminated by '_' --MAP STRUCT 和 ARRAY 的分隔符(数据分割符号)
map keys terminated by ':' -- MAP中的key与value的分隔符
lines terminated by '\n'; -- 行分隔符
(3)导入文本数据到测试表
load data local inpath '/home/hdfs/test.txt' into table test;
(4)访问三种集合列里的数据,(ARRAY,MAP,STRUCT的访问方式)
select friends[1],children['xiao song'],address.city from test;