字符串和数组< struct>列的配置单元串联

dpiehjr4  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(367)

我有两个字符串列和一个数组列。我的要求是将数组转换为字符串,并与其他字符串列连接,以便在连接的字符串列上执行md5函数
但将数组转换为字符串是不可能的,我也尝试使用explode和inline函数来提取数组内容,但到目前为止还没有成功
你知道怎么做吗

fbcarpbf

fbcarpbf1#

分解数组并获取结构元素,使用结构元素构建所需的字符串并收集字符串数组,使用concat\ws将其转换为字符串,然后与其他列连接。这样地:

with mydata as (
select ID, my_array  
from
( --some array<struct> example
 select 1 ID, array(named_struct("city","Hudson","state","NY"),named_struct("city","San Jose","state","CA"),named_struct("city","Albany","state","NY")) as my_array
 union all
 select 2 ID, array(named_struct("city","San Jose","state","CA"),named_struct("city","San Diego","state","CA")) as my_array
)s
)

select ID, concat(ID,'-', --'-' is a delimiter
                 concat_ws(',',collect_list(element)) --collect array of strings and concatenate it using ',' delimiter
                 ) as my_string --concatenate with ID column also
from
(
select s.ID, concat_ws(':',a.mystruct.city, mystruct.state) as element --concatenate struct using : as a delimiter Or concatenate in some other way
  from mydata s 
       lateral view explode(s.my_array) a as mystruct
)s 
group by ID 
;

退货:

OK
1       1-Hudson:NY,San Jose:CA,Albany:NY
2       2-San Jose:CA,San Diego:CA
Time taken: 63.368 seconds, Fetched: 2 row(s)

使用inline可以分解结构元素

with mydata as (
select ID, my_array  
from
( --some array<struct> example
 select 1 ID, array(named_struct("city","Hudson","state","NY"),named_struct("city","San Jose","state","CA"),named_struct("city","Albany","state","NY")) as my_array
 union all
 select 2 ID, array(named_struct("city","San Jose","state","CA"),named_struct("city","San Diego","state","CA")) as my_array
)s
)

select s.ID, a.city, a.state
  from mydata s 
       lateral view inline(s.my_array) a as city, state

;

并在字符串、collect array、concat\u ws等中按需要再次连接它们

相关问题