hive高级数据类型

【自取】最近整理的,有需要可以领取学习:

hive的高级数据类型主要包括:数组类型、map类型、结构体类型、集合类型,以下将分别详细介绍。

1)数组类型
array_type:array

— 建表语句

create table test.array_table(
name string,
age int,
addr array

hive> desc test.array_table;
OK
name string
age int
addr array

— 测试数据

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

— 建表语句

create table test.map_table(
name string,
detail map

hive> desc test.map_table;
OK
name string
detail map

— 测试数据

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

— 建表语句

create table test.struct_table(
name string,
detail struct

hive> desc test.struct_table;
OK
name string
detail struct

— 测试数据

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

— 建表语句

create table test.union_table(
detail uniontype

hive> desc test.union_table;
OK
detail uniontype

Original: https://www.cnblogs.com/wcwen1990/p/9771728.html
Author: ChavinKing
Title: hive高级数据类型

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6785/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部