Hive3:三种常用的复杂数据类型

一、Array类型

1、数据示例

2、实操

元数据

复制代码
zhangsan	beijing,shanghai,tianjin,hangzhou
wangwu	changchun,chengdu,wuhan,beijin

创建表

sql 复制代码
CREATE TABLE myhive.test_array(name string, work_locations array<string>)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
COLLECTION ITEMS TERMINATED BY ',';

COLLECTION ITEMS TERMINATED BY ',' 表示集合(array)元素的分隔符是逗号

加载数据

sql 复制代码
load data local inpath '/home/atguigu/data_for_array_type.txt' overwrite into table myhive.test_array;

常用查询语句

sql 复制代码
-- 查询loction数组中第一个元素
select name, work_locations[0] location from myhive.test_array;
-- 查询location数组中元素的个数
select name, size(work_locations) location from myhive.test_array;
-- 查询location数组中包含tianjin的信息
select * from myhive.test_array where array_contains(work_locations,'tianjin');

二、Map类型

1、数据示例

2、实操

元数据

复制代码
1,林杰均,father:林大明#mother:小甜甜#brother:小甜,28
2,周杰伦,father:马小云#mother:黄大奕#brother:小天,22
3,王葱,father:王林#mother:如花#sister:潇潇,29
4,马大云,father:周街轮#mother:美美,26

创建表

sql 复制代码
CREATE TABLE myhive.test_map(id int, name string, members map<string,string>, age int)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY '#' 
MAP KEYS TERMINATED BY ':';

不同键值对之间:COLLECTION ITEMS TERMINATED BY '分隔符' 分隔

一个键值对内,使用: MAP KEYS TERMINATED BY '分隔符' 分隔K-V

加载数据

sql 复制代码
load data local inpath '/home/atguigu/data_for_map_type.txt' overwrite into table myhive.test_map;

常用查询语句

sql 复制代码
-- 查询father、mother这两个map的key
select id, name, members['father'] father, members['mother'] mother, age from myhive.test_map;
-- 查询全部map的key,使用map_keys函数,结果是array类型
select id, name, map_keys(members) as relation from myhive.test_map;
-- 查询全部map的value,使用map_values函数,结果是array类型
select id, name, map_values(members) as relation from myhive.test_map;
-- 查询map类型的KV对数量
select id,name,size(members) num from myhive.test_map;
-- 查询map的key中有brother的数据
select * from myhive.test_map where array_contains(map_keys(members), 'brother');

三、Struct类型

1、数据示例

2、实操

元数据

复制代码
1#周杰轮:11
2#林均杰:16
3#刘德滑:21
4#张学油:26
5#蔡依临:23

创建表

sql 复制代码
CREATE TABLE myhive.test_struct(id string, info struct<name:string, age:int>)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '#'
COLLECTION ITEMS TERMINATED BY ':';

加载数据

sql 复制代码
load data local inpath '/home/atguigu/data_for_struct_type.txt' overwrite into table myhive.test_struct;


常用查询语句

sql 复制代码
# 直接使用列名.子列名 即可从struct中取出子列查询
select id, info.name from test_struct;

对比

相关推荐
IvorySQL4 分钟前
PG 日报|新增 VFD 缓存监控视图,精细化数据库调优
大数据·数据库·人工智能·缓存·postgresql·区块链
咏方舟【长江支流】6 分钟前
【开源】跨语言·跨平台·跨数据库(3) —— 为用宝框架增加ORM,让AI执行
数据库·人工智能·开源·ai编程·orm·咏方舟-长江支流·用宝框架
工业甲酰苯胺16 分钟前
AI×JNPF实战:3个案例拆解企业数智转型核心逻辑
大数据·数据库·人工智能·低代码
360智汇云21 分钟前
一次Redis超时引发的“冤案“
数据库·redis·php
她说彩礼65万1 小时前
Asp.net core 返回类型
数据库·后端·asp.net
weixin_307779131 小时前
Linux下Docker Compose里运行Milvus向量数据库故障诊断Shell脚本
linux·运维·数据库·docker·milvus
Mem0rin1 小时前
【MySQL】MySQL 增删改查(CRUD)
数据库·mysql
吴声子夜歌2 小时前
Redis 6.x——监控指标
数据库·redis·缓存
再让我睡两分钟2 小时前
【无标题】
android·java·数据库·人工智能·prompt·ai应用开发
爸爸6192 小时前
30 原始 SQL 查询与聚合统计:querySql 实战
数据库·sql·华为·harmonyos·鸿蒙系统