【问题标题】:In Hive, how to convert an array of string to an array of numeric numbers在 Hive 中,如何将字符串数组转换为数字数组
【发布时间】:2019-09-24 12:07:07
【问题描述】:

我有一个如下所示的配置单元表:

id | value
 1 | ['0', '0', '1', '0', '1', '1', '0', '0']
 2 | ['2', '0', '3', '0', '3', '1', '2', '1']

我希望结果如下:

id | value
 1 | [0,0,1,0,1,1,0,0]
 2 | [2,0,3,0,3,1,2,1]

我需要将它们转换为浮点数组,以便我可以在ST_Constains(ST_MultiPolygon(), st_point()) 中使用它们来确定一个点是否在一个区域中。

我是 Hive 的新手,不确定这是否可能,非常感谢任何帮助。

【问题讨论】:

    标签: sql arrays hive hiveql numeric


    【解决方案1】:

    您可以再次分解数组、转换值、收集数组。 演示:

    with your_table as(
    select stack(2,
     1 , array('0', '0', '1', '0', '1', '1', '0', '0'),
     2 , array('2', '0', '3', '0', '3', '1', '2', '1')
     ) as (id,value)
     ) --use your_table instead of this
    
    
     select s.id, 
            s.value                            as original_array, 
            collect_list(cast(s.str as float)) as array_float 
     from
    (select t.*, s.* 
     from your_table t
                   lateral view outer posexplode(t.value)s as pos,str       
       distribute by t.id, t.value 
             sort by s.pos --preserve order in the array
     )s  
    group by s.id, s.value;  
    

    结果:

    OK
    1       ["0","0","1","0","1","1","0","0"]       [0.0,0.0,1.0,0.0,1.0,1.0,0.0,0.0]
    2       ["2","0","3","0","3","1","2","1"]       [2.0,0.0,3.0,0.0,3.0,1.0,2.0,1.0]
    

    另请参阅查询https://stackoverflow.com/a/57392965/2700344中有关排序数组的答案

    【讨论】:

    • 非常感谢。这正是我想要的。我花了很长时间来理解代码中的每一个步骤。没有一条线是无用的。我很感激。
    • @GuanyanLin 欢迎您!如果有帮助,请接受/投票
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多