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)

相关推荐
ganshenml2 小时前
【GIT】Git 本地无法识别远程分支的原因与解决方法 not a valid ref
大数据·git·elasticsearch
火山引擎开发者社区3 小时前
DeepSeek-V3.2正式登陆火山方舟
大数据·人工智能
jqpwxt3 小时前
启点创新山水景区智慧旅游SAAS平台,智慧景区售检票系统,景区门票管理系统
大数据·旅游
陀螺财经4 小时前
加密热潮“席卷”美国军界
大数据·人工智能·区块链
打码人的日常分享5 小时前
智慧城市一网统管建设方案,新型城市整体建设方案(PPT)
大数据·运维·服务器·人工智能·信息可视化·智慧城市
Sui_Network5 小时前
21shares 在纳斯达克推出 2 倍 SUI 杠杆 ETF(TXXS)
大数据·人工智能·游戏·金融·区块链
龙亘川5 小时前
开箱即用的智慧城市一网统管 AI 平台——功能模块详解(3)
大数据·人工智能·智慧城市·智慧城市一网统管 ai 平台
dragonzoebai6 小时前
ol加载互联网瓦片大于18级时空白
大数据
AI营销快线6 小时前
AI营销下半场:B2B选型指南
大数据·人工智能
一只专注api接口开发的技术猿7 小时前
构建电商数据中台:基于淘宝 API 关键词搜索接口的设计与实现
大数据·开发语言·数据库