将String转换为array< int>在蜂巢 [英] Convert String to array<int> in Hive
问题描述
我有一个用String表示的整数数组.例如,"[1,2,2,3]"
I have a integer array represented with String. For example, "[1,2,2,3]"
Hive表中的字段类型是数组整数,我想知道是否有任何Hive内置UDF可以将上述字符串转换为数组整数.
And the field type in Hive table is the array integer, I was wondering if there is any Hive build-in UDF that can cast the above string into the array integer.
谢谢
推荐答案
tl; dr 我不知道会为您执行此操作的Hive UDF,并且自行转换可能很麻烦
tl;dr I do not know of a Hive UDF that will do this for you, and casting on your own can be gross.
不,没有UDF.至于构建自己的解决方案:
No, there isn't a UDF. As for building your own solution:
投射到array [string]会很容易-只需使用 regexp_replace
放下方括号,然后在,
上分割结果字符串即可.
Casting to array[string] would be easy enough - just drop the square brackets using regexp_replace
and split the resulting string on ,
.
问题是将任意大小的数组从array [string]转换为array [int].您可以逐一地分别转换数组元素:
The problem is converting an array[string] to array[int] for arrays of arbitrary size. You can individually cast the array elements one by one:
hive> select id, my_array from array_table limit 3;
OK
10023307 ["0.20296966","0.17753501","-0.03543373"]
100308007 ["0.16155224","0.1945944","0.09167781"]
100384207 ["0.025892768","0.023214806","-0.003712816"]
hive> select array(cast(my_array[0] as double), cast(my_array[1] as double), cast(my_array[2] as double)) from array_table limit 3;
OK
[0.20296966,0.17753501,-0.03543373]
[0.16155224,0.1945944,0.09167781]
[0.025892768,0.023214806,-0.003712816]
但是这种方法只能用,因为我知道我有长度为3的数组.
but this approach only works because I know I have arrays of length 3.
这篇关于将String转换为array< int>在蜂巢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!