当前位置: 首页 > news >正文

hive中struct相关函数总结

目录

  • hive官方函数解释
  • 示例
  • 实战

hive官方函数解释

hive官网函数大全地址:添加链接描述

Return TypeNameDescription
structstruct(val1, val2, val3, …)Creates a struct with the given field values. Struct field names will be col1, col2, …
structnamed_struct(name1, val1, name2, val2, …)Creates a struct with the given field names and values. (As of Hive 0.8.0.)
array<struct {‘x’,‘y’}>array(struct(, ), struct(, ), struct(, ))Creates a array of struct type
T1,…,Tninline(ARRAY<STRUCTf1:T1,...,fn:Tn> a)Explodes an array of structs to multiple rows. Returns a row-set with N columns (N = number of top level elements in the struct), one row per struct from the array. (As of Hive 0.10.)

示例

1、struct(field1, field2, …):创建一个 Struct 对象,由多个字段组成。

SELECT struct('张三', 20, '男', '2022-09-01') AS student_info;
---结果
student_info
{"col1":"张三","col2":20,"col3":"男","col4":"2022-09-01"}

2、named_struct(name1, value1, name2, value2, …):创建一个 Named Struct 对象,由多个名称和对应的值组成。

SELECT named_struct('name', '张三', 'age', 20, 'gender', '男', 'enrollment_date', '2022-09-01') AS student_info;
---结果
student_info
{"name":"张三","age":20,"gender":"男","enrollment_date":"2022-09-01"}

3、array(struct(, ), struct(, ), struct(, ))

sql1:
select array(struct( '张三',  20,  '男',  '2022-09-01'),struct( '李四',  18,  '男',  '2022-10-01')) as student_info
----结果
student_info
[{"col1":"张三","col2":20,"col3":"男","col4":"2022-09-01"},{"col1":"李四","col2":18,"col3":"男","col4":"2022-10-01"}]
sql2:
select array(named_struct('name', '张三', 'age', 20, 'gender', '男', 'enrollment_date', '2022-09-01'),named_struct('name', '李四', 'age', 18, 'gender', '男', 'enrollment_date', '2022-10-01')) as  student_info
----结果
student_info
[{"name":"张三","age":20,"gender":"男","enrollment_date":"2022-09-01"},{"name":"李四","age":18,"gender":"男","enrollment_date":"2022-10-01"}]

4、inline(ARRAY<STRUCTf1:T1,...,fn:Tn>

sql1:
select inline(array(struct('A',10,date '2015-01-01'),struct('B',20,date '2016-02-02')));
select inline(array(struct('A',10,date '2015-01-01'),struct('B',20,date '2016-02-02'))) as (col1,col2,col3);
select tf.* from (select 0) t lateral view inline(array(struct('A',10,date '2015-01-01'),struct('B',20,date '2016-02-02'))) tf;
select tf.* from (select 0) t lateral view inline(array(struct('A',10,date '2015-01-01'),struct('B',20,date '2016-02-02'))) tf as col1,col2,col3;
----以上四个结果均为:
col1                col2                 col3	
A                   10                 2015-01-01
B                   20                 2016-02-02sql2:
select inline(array(named_struct('name', '张三', 'age', 20, 'gender', '男', 'enrollment_date', '2022-09-01'),named_struct('name', '李四', 'age', 18, 'gender', '男', 'enrollment_date', '2022-10-01'))) as (name,age,gender,enrollment_date);----结果为:
name         age            gender           enrollment_date	
张三         202022-09-01
李四         182022-10-01

实战

如何将上述struct类型的数据转换为string格式?

select
concat('[',concat_ws( ',',collect_set(concat('{','"name":"',nvl(temp.name,''),'",','"age":"',nvl(temp.age,''),'",','"gender":"',nvl(temp.gender,''),'",','"enrollment_date":"',nvl(temp.enrollment_date,''),'"','}'))),']') as student_info1      
from 
(select array(struct('张三', 20, '男',  '2022-09-01'),struct('李四',  18,  '男',  '2022-10-01'))  as  student_info
) t 
lateral view inline(t.student_info)  temp as name,age,gender,enrollment_date;
------结果为
student_info1	
[{"name":"李四","age":"18","gender":"男","enrollment_date":"2022-10-01"},{"name":"张三","age":"20","gender":"男","enrollment_date":"2022-09-01"}]
http://www.lryc.cn/news/270297.html

相关文章:

  • macos下转换.dmg文件为 .iso .cdr文件的简单方法
  • ALSA学习(5)——设备中的alsa
  • uniapp中组件库的丰富NumberBox 步进器的用法
  • 【Matlab】基于遗传算法优化BP神经网络 (GA-BP)的数据时序预测
  • 计算机毕业设计 基于HTML5+CSS3的在线英语阅读分级平台的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 云原生|kubernetes|kubernetes资源备份和集群迁移神器velero的部署和使用
  • 【26.4K⭐】ShareX:一款开源免费、功能强大且丰富的截屏录屏软件
  • 什么是ajax,为什么使用ajax?
  • AI面板识别 - 华为OD统一考试
  • Linux之磁盘分区,挂载
  • 2核2G3M服务器上传速度多少?以阿里云和腾讯云为例
  • Cisco模拟器-OSPF路由协议
  • SpEL 的使用
  • 数据采集实战:电商详情页数据埋点
  • 计算机网络——计算大题(七)
  • 子网掩码与IP段计算
  • 【译文】IEEE白皮书 6G 太赫兹技术的基本原理 2023版
  • AUTOSAR从入门到精通-网络通信(UDPNm)(三)
  • ubuntu 使用openssl制作一个自签名证书
  • WPF+Halcon 培训项目实战(1-5):Halcon安装,图像处理,Halcon简单模板匹配
  • 虚函数的讲解
  • Java强软弱虚引用
  • QCharView使用
  • 华为hcia之ipv6实验手册
  • 算法设计与分析-图算法小结BFS/DFS/Topologic/Dijkstra/Floyd/最大流
  • CentOS 8 安装指定版本ansible
  • 策略模式(及案例)
  • 苹果CMS超级播放器专业版无授权全开源,附带安装教程
  • 项目记录:利用Redis实现缓存以提升查询效率
  • 腾讯云16核32G28M轻量服务器CPU流量性能测评