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)

相关推荐
明航咨询—张老师4 小时前
软考软件评测师技术深度剖析:2026年考试改革后的知识体系与备考架构
大数据·架构
千桐科技5 小时前
qData 数据中台开源版从 MySQL 到 Doris完成一次数据同步
大数据·mysql·开源·doris·数据中台·qdata
ACP广源盛139246256738 小时前
GSV6155@ACP# 搭配 AI 服务器、AI PC 完整适配方案
大数据·服务器·人工智能·分布式·单片机·嵌入式硬件
stonewl25999 小时前
2026化工行业 GHS/CLP 国产标签打印软件合规打印方案
大数据·人工智能·物联网
xywww1689 小时前
AWS 账号权限怎么分:根用户和 IAM 用户区别及日常使用建议
大数据·开发语言·人工智能·python·gpt·云计算·aws
Quincy_Freak10 小时前
SQLiteGo V2.3 版本更新说明
大数据·数据库·sqlite·数据库管理·大数据分析·sqlitego
牛奶咖啡1310 小时前
大数据Hadoop运维应用实践——双NameNode高可用Hadoop集群架构(下)
大数据·hadoop·hadoop集群配置文件解析·hadoop高可用集群安装部署·配置指定多台服务器相互免密·配置hadoop服务开机自启·ansible部署hadoop
在水一缸10 小时前
深度解析 GPT-5.6:大模型架构演进与复杂任务实战指南
大数据·人工智能·gpt·架构·大模型·架构演进·gpt-5.6
无忧智库11 小时前
智慧电厂:引领发电行业新革命(PPT)
大数据·人工智能
小果因子实验室11 小时前
量化研究--编写第一个回测算法策略研究
大数据·区块链