Hive03_数据类型

数据类型

1 案例实操

(1)假设某表有如下一行,我们用 JSON 格式来表示其数据结构。在 Hive 下访问的格式为

json 复制代码
{
 "name": "wukong",
 "friends": ["bajie" , "lili"] , //列表 Array, 
 "children": { //键值 Map,
 "xiao sun": 18 ,
 "xiaoxiao sun": 19
 }
 "address": { //结构 Struct,
 "street": "hui long guan",
 "city": "beijing"
 }
}

(2)基于上述数据结构,我们在 Hive 里创建对应的表,并导入数据。

创建本地测试文件 hive/datas/ test.txt

html 复制代码
wukong,bajie_lili,xiao sun:18_xiaoxiao sun:19,hui long guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing

注意:MA,STRUCT 和 ARRAY 里的元素间关系都可以用同一个字符表示,这里用"_"。

(3)Hive 上创建测试表 test

sql 复制代码
create table test(
name string,
friends array<string>,
children map<string, int>,
address struct<street:string, city:string>
)
row format delimited fields terminated by ','
collection items terminated by '_'
map keys terminated by ':'
lines terminated by '\n';

字段解释:

row format delimited fields terminated by ',' -- 列分隔符

collection items terminated by '_' --MAP STRUCT 和 ARRAY 的分隔符(数据分割符号)

map keys terminated by ':' -- MAP 中的 key 与 value 的分隔符

lines terminated by '\n'; -- 行分隔符

(4)导入文本数据到测试表

在 hive/datas下创建info.sql,写入上面的sql语句。

执行sql文件,创建表结构

sh 复制代码
[root@hadoop2 hive]# bin/hive -f /usr/soft/hive/datas/info.sql 

启动sql,查看表结构是否创建成功;

将test.txt中的数据,写入到表中

sh 复制代码
[root@hadoop2 datas]# hadoop fs -put friend.txt /user/hive/warehouse/info

5)访问三种集合列里的数据,以下分别是 ARRAY,MAP,STRUCT 的访问方式

sql 复制代码
hive (default)> select friends[1],children['xiao sun'],address.city from 
test
where name="wukong";

查询结果:

OK

_c0 _c1 city

lili 18 beijing

Time taken: 0.076 seconds, Fetched: 1 row(s)

果:**

OK

_c0 _c1 city

lili 18 beijing

Time taken: 0.076 seconds, Fetched: 1 row(s)

相关推荐
Apache Flink3 小时前
Apache Flink Agents 0.2.0 发布公告
大数据·flink·apache
永霖光电_UVLED4 小时前
打造更优异的 UVB 激光器
大数据·制造·量子计算
m0_466525294 小时前
绿盟科技风云卫AI安全能力平台成果重磅发布
大数据·数据库·人工智能·安全
晟诺数字人4 小时前
2026年海外直播变革:数字人如何改变游戏规则
大数据·人工智能·产品运营
vx_biyesheji00014 小时前
豆瓣电影推荐系统 | Python Django 协同过滤 Echarts可视化 深度学习 大数据 毕业设计源码
大数据·爬虫·python·深度学习·django·毕业设计·echarts
2501_943695334 小时前
高职大数据与会计专业,考CDA证后能转纯数据分析岗吗?
大数据·数据挖掘·数据分析
实时数据5 小时前
通过大数据的深度分析与精准营销策略,企业能够有效实现精准引流
大数据
子榆.5 小时前
CANN 性能分析与调优实战:使用 msprof 定位瓶颈,榨干硬件每一分算力
大数据·网络·人工智能
新芒5 小时前
暖通行业两位数下滑,未来靠什么赢?
大数据·人工智能
忆~遂愿6 小时前
CANN ATVOSS 算子库深度解析:基于 Ascend C 模板的 Vector 算子子程序化建模与融合优化机制
大数据·人工智能