对于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,不能为其他类型


    error

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


    concat

你可能感兴趣的:(对于hql中拼接字符串的函数使用)