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)

相关推荐
TMT星球1 小时前
伽利略机器人亮相WRC 2026,突破形态局限发布“陆行具身系统”Galileo X
大数据·人工智能·机器人
一只鹿鹿鹿2 小时前
数据资产管理解决方案(Word文件)
大数据·数据库·安全·web安全·系统安全
m0_638079622 小时前
2026年AI论文写作辅助工具技术对比与使用观察
大数据·人工智能
商业数据派2 小时前
日赚近1亿的网易,这个季度栽在了拼多多身上
大数据·人工智能
晓子文集3 小时前
Tushare接口文档:月线行情(monthly)
大数据·数据库·金融数据·量化投资·tushare
长谷深风1113 小时前
AI Tool 设计:粒度、参数与错误恢复怎么做
java·大数据·人工智能·ai agent·agent工作流·智能体设计·ai产品设计
leoZ2315 小时前
10-Git 仓库蒸馏术:从代码仓库到 OpenClaw 虚拟人-蒸馏工具链
大数据·git·深度学习·神经网络·目标检测·elasticsearch·lstm
夏贰四6 小时前
管控数据加载调度如何规避任务冲突?数据加载运维监控体系该如何搭建?
java·大数据·运维
BYSJMG7 小时前
计算机毕业设计选题推荐:基于大数据的心脏病风险数据可视化与分析(Hadoop+Spark+PySpark)
大数据·hadoop·信息可视化·数据分析·spark·课程设计
长谷深风1117 小时前
好的 Tool Schema,不是字段越全越好
java·大数据·人工智能·ai agent·agent工作流·智能体设计·ai产品设计