在配置单元中,如何将字符串数组转换为数字数组

cl25kdpy  于 2021-06-27  发布在  Hive
关注(0)|答案(1)|浏览(385)

我有一个Hive表,如下所示:

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]

我需要将它们转换成一个float数组,以便在 ST_Constains(ST_MultiPolygon(), st_point()) 确定点是否在某个区域中。
我是新来的Hive,不知道如果这是可能的,任何帮助将非常感谢。

hsgswve4

hsgswve41#

你可以分解数组,释放值,再收集数组。演示:

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

相关问题