hive高级数据类型
Posted wcwen1990
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hive高级数据类型相关的知识,希望对你有一定的参考价值。
hive的高级数据类型主要包括:数组类型、map类型、结构体类型、集合类型,以下将分别详细介绍。
1)数组类型
array_type:array<data_type>
-- 建表语句
create table test.array_table(
name string,
age int,
addr array<string>
)
row format delimited fields terminated by ‘,‘
collection items terminated by ‘:‘
;
hive> desc test.array_table;
OK
name string
age int
addr array<string>
-- 测试数据
chavin,28,beijing:shanghai:shenyang
nope,28,beijing:shanghai:taiyuan
wzq,28,beijing:dalian:shenyang:fuxin
zhangshuai,28,beijing:shenyang:fuxin
-- 加载数据
hive> load data local inpath ‘/home/hadoop/array_table.txt‘ into table test.array_table;
Loading data to table test.array_table
OK
Time taken: 0.918 seconds
-- 查询
hive> select * from test.array_table;
OK
chavin 28 ["beijing","shanghai","shenyang"]
nope 28 ["beijing","shanghai","taiyuan"]
wzq 28 ["beijing","dalian","shenyang","fuxin"]
zhangshuai 28 ["beijing","shenyang","fuxin"]
Time taken: 0.199 seconds, Fetched: 4 row(s)
hive> select addr[1] from test.array_table;
OK
shanghai
shanghai
dalian
shenyang
Time taken: 0.186 seconds, Fetched: 4 row(s)
hive> select addr[0] from test.array_table;
OK
beijing
beijing
beijing
beijing
Time taken: 0.212 seconds, Fetched: 4 row(s)
2)map类型
map_type:map<primary_type,data_type>
-- 建表语句
create table test.map_table(
name string,
detail map<string,string>
)
row format delimited fields terminated by ‘ ‘
collection items terminated by ‘,‘
map keys terminated by ‘:‘
;
hive> desc test.map_table;
OK
name string
detail map<string,string>
Time taken: 0.146 seconds, Fetched: 2 row(s)
-- 测试数据
chavin age:28,addr:beijing,phone:15998888888,email:[email protected]
nope age:28,addr:beijing,phone:13899998888,email:[email protected]
wzq age:28,addr:shanghai,phone:13899998888,email:[email protected]
zhangshuai age:28,addr:tianjing,phone:13899998888,email:[email protected]
-- 加载数据
hive> load data local inpath ‘/home/hadoop/map.txt‘ into table test.map_table;
Loading data to table test.map_table
OK
Time taken: 0.641 seconds
-- 查询数据
hive> select * from test.map_table;
OK
chavin {"age":"28","addr":"beijing","phone":"15998888888","email":"[email protected]"}
nope {"age":"28","addr":"beijing","phone":"13899998888","email":"[email protected]"}
wzq {"age":"28","addr":"shanghai","phone":"13899998888","email":"[email protected]"}
zhangshuai {"age":"28","addr":"tianjing","phone":"13899998888","email":"[email protected]"}
Time taken: 0.183 seconds, Fetched: 4 row(s)
hive> select detail from test.map_table;
OK
{"age":"28","addr":"beijing","phone":"15998888888","email":"[email protected]"}
{"age":"28","addr":"beijing","phone":"13899998888","email":"[email protected]"}
{"age":"28","addr":"shanghai","phone":"13899998888","email":"[email protected]"}
{"age":"28","addr":"tianjing","phone":"13899998888","email":"[email protected]"}
Time taken: 0.177 seconds, Fetched: 4 row(s)
hive> select name,detail[‘email‘] from test.map_table;
OK
chavin [email protected]
nope [email protected]
wzq [email protected]
zhangshuai [email protected]
Time taken: 0.179 seconds, Fetched: 4 row(s)
3)结构体类型
struct_type:struct<col_name:data_type,... ...>
-- 建表语句
create table test.struct_table(
name string,
detail struct<age:int,phone:string,email:string>
)
row format delimited fields terminated by ‘ ‘
collection items terminated by ‘,‘
;
hive> desc test.struct_table;
OK
name string
detail struct<age:int,phone:string,email:string>
Time taken: 0.139 seconds, Fetched: 2 row(s)
-- 测试数据
chavin 28,15998888888,[email protected]
nope 28,13899998888,[email protected]
wzq 28,13899998888,[email protected]
zhangshuai 28,13899998888,[email protected]
-- 加载数据
hive> load data local inpath ‘/home/hadoop/struct.txt‘ into table test.struct_table;
Loading data to table test.struct_table
OK
Time taken: 0.564 seconds
-- 查询数据
hive> select * from test.struct_table;
OK
chavin {"age":28,"phone":"15998888888","email":"[email protected]"}
nope {"age":28,"phone":"13899998888","email":"[email protected]"}
wzq {"age":28,"phone":"13899998888","email":"[email protected]"}
zhangshuai {"age":28,"phone":"13899998888","email":"[email protected]"}
Time taken: 0.183 seconds, Fetched: 4 row(s)
hive> select name,detail.email from test.struct_table;
OK
chavin [email protected]
nope [email protected]
wzq [email protected]
zhangshuai [email protected]
Time taken: 0.869 seconds, Fetched: 4 row(s)
4)集合类型
union_type:uniontype<data_type,data_type,... ...>
-- 建表语句
create table test.union_table(
detail uniontype<int,string>
)
row format delimited fields terminated by ‘ ‘
collection items terminated by ‘,‘
;
hive> desc test.union_table;
OK
detail uniontype<int,string>
Time taken: 0.141 seconds, Fetched: 2 row(s)
以上是关于hive高级数据类型的主要内容,如果未能解决你的问题,请参考以下文章