sql - 使用 hql 将列从列表转换为数组,以及将数组转换为 hive 中的列表

标签 sql arrays hive hiveql

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

<表类=“s-表”> <标题> 列表 数组 <正文> [10:20] [10,20] [30:40:50] [30,40,50]

我还想将它从数组转换为列表

<表类=“s-表”> <标题> 数组 列表 <正文> [10,20] [10:20] [30,40,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,40,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,40,50]
[10:20]     [10,20]

如果您同意array<string> ,那么转换就简单多了:

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

select list, split(regexp_replace(list,'\\[|\\]',''),':') myarray
  from mytable;

结果:

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

关于sql - 使用 hql 将列从列表转换为数组,以及将数组转换为 hive 中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65694067/

相关文章:

mysql - 表内的内连接

sql - 应该更新日期不执行的postgres函数

PHP PDO fetchAll() 在选择临时表时返回空数组

c - 动态分配用户输入的字符串

ios - 在 if 语句中使用多个 contains 函数 (Swift)

java - Hive GenericUDF 错误 - RuntimeException typeInfo 不能为 null

mysql - mySql 查询中的 If 语句

javascript - 使用 anchor .attr href 作为计数器中数组的起点

csv - Hive - 加载以管道开头的管道分隔数据

hadoop - 两个日期时间之间的 Spark / hive 时间