hive提供一种复合类型的数据
struct:可以使用“.”来存取数据
map:可以使用键值对来存取数据
array:array中存取的数据为相同类型,其中的数据可以通过下表获取数据
创建 struct类型
create table student_struct(id INT, info struct<name:STRING, age:INT>) ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘,‘ //指定表在加载数据时候的列分割符 COLLECTION ITEMS TERMINATED BY ‘:‘; //指定每个字段之间的item的分隔符
导入测试数据
1,zhang:24 2,wang:23 3,feng:22 4,li:22 5,zhou:21 6,xing:20 7,cai:19 8,yi:18 9,lan:17
查询表中数据
使用struct的"."来查询数据
hive> select*from student_struct where info.age>20; OK 1 {"name":"zhang","age":24} 2 {"name":"wang","age":23} 3 {"name":"feng","age":22} 4 {"name":"li","age":22} 5 {"name":"zhou","age":21} Time taken: 2.31 seconds, Fetched: 5 row(s)
创建array
create table student_array(id INT, stuArray array<String>) ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘,‘ COLLECTION ITEMS TERMINATED BY ‘:‘;
加载测试数据(stuArray里面存储的是相同的数据类型,可以有多个值)
同 struct 数据
查询表中数据(array类型,可以通过你创建表时定义列的别名,通过下标来获取数据)
hive> select*from student_array where stuArray[1]>20; OK 1 ["zhang","24"] 2 ["wang","23"] 3 ["feng","22"] 4 ["li","22"] 5 ["zhou","21"] Time taken: 2.63 seconds, Fetched: 5 row(s)
创建Map(stuMap可以有多个,我这里写了两个)
create table student_map(id string, stuMap map<string, int>) ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\t‘ COLLECTION ITEMS TERMINATED BY ‘,‘ MAP KEYS TERMINATED BY ‘:‘;
导入测试数据
1 zhang:24,shangHai:100 2 wang:23,shangHai:200 3 feng:22,shangHai:1900 4 li:22,shangHai:2900 5 zhou:21,shangHai:1200 6 xing:20,shangHai:1200 7 cai:19,shangHai:600 8 yi:18,shangHai:7600 9 lan:17,shangHai:8000
查询数据
hive> select * from student_map where stuMap[‘shangHai‘]=100; OK 1 {"zhang":24,"shangHai":100} Time taken: 0.944 seconds, Fetched: 1 row(s) hive>
时间: 2024-11-05 13:50:06