使用 hql 将列从列表转换为数组并将数组转换为配置单元中的列表

问题描述

我想使用 hql 将以下列从列表转换为数组

列表 数组
[10:20] [10,20]
[30:40:50] [30,40,50]

我也想把它从数组转换成列表

数组 列表
[10,20] [10:20]
[30,50] [30:40:50]

解决方法

要列出演示的数组:

with mytable as (
select stack (2,array(10,20),array(30,40,50)
) as myarray
)

select myarray,concat('[',concat_ws(':',collect_list(string(element))),']') as list
  from mytable
       lateral view explode(myarray) e as element
       group by myarray;

结果:

myarray     list
[10,20]     [10:20]
[30,50]  [30:40:50]

列表到数组演示:

with mytable as (
select stack (2,'[10:20]','[30:40:50]'
) as list
)

select list,collect_list(int(element)) myarray
  from mytable
       lateral view explode (split(regexp_replace(list,'\\[|\\]',''),':')) e as element
  group by list;

结果:

list        myarray
[30:40:50]  [30,50]
[10:20]     [10,20]

如果您对 array<string> 没问题,那么转换就简单多了:

with mytable as (
select stack (2,split(regexp_replace(list,':') myarray
  from mytable;

结果:

list        myarray
[10:20]     ["10","20"]
[30:40:50]  ["30","40","50"]