码迷,mamicode.com
首页 > 其他好文 > 详细

hive高级数据类型

时间:2018-10-11 12:57:41      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:desc   ted   mina   orm   format   rmi   数组类   beijing   nio   

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 ‘\t‘
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:811651208@qq.com
nope    age:28,addr:beijing,phone:13899998888,email:nope@gmail.com
wzq    age:28,addr:shanghai,phone:13899998888,email:wzq@gmail.com
zhangshuai    age:28,addr:tianjing,phone:13899998888,email:zs@gmail.com


-- 加载数据

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":"811651208@qq.com"}
nope    {"age":"28","addr":"beijing","phone":"13899998888","email":"nope@gmail.com"}
wzq    {"age":"28","addr":"shanghai","phone":"13899998888","email":"wzq@gmail.com"}
zhangshuai    {"age":"28","addr":"tianjing","phone":"13899998888","email":"zs@gmail.com"}
Time taken: 0.183 seconds, Fetched: 4 row(s)

hive> select detail from test.map_table;
OK
{"age":"28","addr":"beijing","phone":"15998888888","email":"811651208@qq.com"}
{"age":"28","addr":"beijing","phone":"13899998888","email":"nope@gmail.com"}
{"age":"28","addr":"shanghai","phone":"13899998888","email":"wzq@gmail.com"}
{"age":"28","addr":"tianjing","phone":"13899998888","email":"zs@gmail.com"}
Time taken: 0.177 seconds, Fetched: 4 row(s)

hive> select name,detail[‘email‘] from test.map_table;
OK
chavin    811651208@qq.com
nope    nope@gmail.com
wzq    wzq@gmail.com
zhangshuai    zs@gmail.com
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 ‘\t‘
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,811651208@qq.com
nope    28,13899998888,nope@gmail.com
wzq    28,13899998888,wzq@gmail.com
zhangshuai    28,13899998888,zs@gmail.com

-- 加载数据

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":"811651208@qq.com"}
nope    {"age":28,"phone":"13899998888","email":"nope@gmail.com"}
wzq    {"age":28,"phone":"13899998888","email":"wzq@gmail.com"}
zhangshuai    {"age":28,"phone":"13899998888","email":"zs@gmail.com"}
Time taken: 0.183 seconds, Fetched: 4 row(s)

hive> select name,detail.email from test.struct_table;
OK
chavin    811651208@qq.com
nope    nope@gmail.com
wzq    wzq@gmail.com
zhangshuai    zs@gmail.com
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 ‘\t‘
collection items terminated by ‘,‘
;

hive> desc test.union_table;
OK                                    
detail                  uniontype<int,string>                       
Time taken: 0.141 seconds, Fetched: 2 row(s)

hive高级数据类型

标签:desc   ted   mina   orm   format   rmi   数组类   beijing   nio   

原文地址:https://www.cnblogs.com/wcwen1990/p/9771728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!