对于hql中拼接字符串的函数使用

背景:在工作过程中,一直经常性接触到hql拼接字符串的工作内容,但每次都是最常规的拼接,效率并不高。故调研发现hive早已提供相关函数进行更简便处理。

  1. named_struct
  • 专门用来拼接 json 的函数
  • 使用方法:named_struct(columnName1, value1, columnName2, value2, ...)


    named_struct
  1. collect_list(可重复) 与collect_set(不可重复)
  • 返回一个数组
  • 与json函数配合使用构成json数组


    json_array
select
'id',
collect_list(json)
from
(select 'id',named_struct("name","color","age",18) as json 
union all
select 'id',named_struct("name","Doull","age",19) as json)t
group by 'id';
arr
  1. concat_ws
  • 会将数组中的数据,再按照指定分隔符进行拼接,即去数组化
  • concat_ws 参数需为 string or array<string>,不能为其他类型


    error

    exam
  1. concat
  • 可使用concat拼接,将字符串拼接成json


    concat
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容