hive 中复杂类型的使用
hive 中的复杂类型包括 array(数组)、map(字典)、struct(结构体)等,下面分别介绍。
- array
1. 创建临时表
hive -e "use dataalgo; create table cb_array_test(citeid int, order_id_list array<int>) row format delimited fields terminated by '\t' collection items terminated by ','";
2. 写入数据 cat cb_array_test.txt 1 1,2,3 2 4,5,6 3 7,8,9 hive -e "use dataalgo; load data inpath '/tmp/cb_array_test.txt' overwrite into table cb_array_test"
3. 测试结果 hive -e "use dataalgo; select * from dataalgo.cb_array_test" 1 [1,2,3] 2 [4,5,6] 3 [7,8,9]
- struct
1. 创建临时表
hive -e "use dataalgo; drop table if exists cb_test; create table cb_test(id int, name struct<first:string, last:string>) row format delimited fields terminated by '\t' collection items terminated by ':' "
2. 写入数据 cat struct.txt 1 'a':'a' 1 'b':'b' 1 'c':'c' 1 'd':'d' hive -e "use dataalgo; load data inpath '/tmp/struct.txt' overwrite into table cb_test"
3. 测试结果 hive -e "use dataalgo; select * from cb_test"
1 {"first":"'a'","last":"'a'"}
1 {"first":"'b'","last":"'b'"}
1 {"first":"'c'","last":"'c'"}
1 {"first":"'d'","last":"'d'"}
- map
1. 创建临时表
hive -e "use dataalgo; drop table if exists cb_test_map; create table cb_test_map(id int, name map<string, string>) row format delimited fields terminated by '\t' collection items terminated by ',' map keys terminated by ':'"
2. 写入数据 cat cb_map_test.txt 1 a:a,b:b
2 c:c,d:d
3 e:e,f:f hive -e "use dataalgo; load data inpath '/tmp/cb_map_test.txt' overwrite into table cb_test_map"
3. 测试结果
hive -e "use dataalgo; select * from cb_test_map"
1 {"a":"a","b":"b"}
2 {"c":"c","d":"d"}
3 {"e":"e","f":"f"}