【问题标题】:Convert a column from list to array and array to list in hive using hql使用 hql 将列表中的列转换为数组和数组到 hive 中的列表
【发布时间】:2021-01-13 00:32:31
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: sql arrays hive hiveql


    【解决方案1】:

    数组列表演示:

    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"]
    

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 2019-11-10
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      相关资源
      最近更新 更多