Hive复杂类型

文章介绍了Hive中三种主要的数据结构:array数组,类似于Java中的数组;Map类似于Python的字典,用于存储键值对;struct则类似于C语言的结构体。创建和操作这些数据结构的方法,如array的size和array_contains函数,Map的键值访问及size,map_keys,map_values函数,以及struct的字段访问等,都在文中进行了详细阐述。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

array数组

Map映射

struct结构

array数组

      array数组和java中数组差不多,形式为[元素1,元素2,元素3,...]

      建表语句:

create table table_name(name string, location array<string>) row format delimited fields terminated by '\t' collection items terminated by ',';

      collection items terminated by '分隔符' 是设置数组分隔符的语句 ,注意加载数据与sql语句中符号都在英文(或中文)状态下输入

      常用函数:

        size(数组), 统计数组元素个数

select size(array) from table_name;

        array_contains(数组, 数据),查看指定元素在指定数组中是否存在

select * from table_name where array_contains(array, 元素);

Map映射

      map与python中的字典类似,表现为{"k1":"v1","k2":"v2",....}

      创建语句

create table test_map(name string ,member map<string, string>) row format delimited fields terminated by ',' collection items terminated by '#' map keys terminated by ':';

      map keys terminated by ':' 是键值对内部k,v间的分隔符,collection items则和array相似,针对建表语句给出示例txt文件

zhangsan,father:zhangbaba#mother:zhangmama
Lily,father:Bob#mother:Lucy

       常用函数:

        直接使用member['mother']得到key为mother的value

        size(member)获得键值对数量

        map_keys(member)返回所有的key,以array形式展示

        map_values(member)返回所有value,以array形式展示

struct结构

      struct结构与c语言的结构体类似,表现为{name:v1 , age:v2}

      创建语句

create table test_struct(id int, info struct<name:string, age:int>) row format delimited fields terminated by '#' collection items terminated by ':';

      给出对应的txt数据

1#Lily:18
2#Bob:28
3#Aric:55

      取数据使用info.name就可以得到需要的数据

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值