在 Hive 中,如何将字符串数组转换为数字数组

In Hive, how to convert an array of string to an array of numeric numbers

我有一个蜂巢 table,如下所示:

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 的新手,不确定是否可行,非常感谢任何帮助。

可以再爆阵、施值、收阵。 演示:

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]

另请参阅有关在查询中对数组进行排序的答案