Hive: 在查询中将字符串数组转换为整数数组

6

我有两个表:

create table a (
`1` array<string>);

create table b (
`1` array<int>);

我想将表格a放入表格b中(表格b为空):
insert into table b
select * from a;

当我这样做时,我会收到以下错误提示:
FAILED: SemanticException [Error 10044]: Line 1:18 Cannot insert into
target table because column number/types are different 'b': Cannot
convert column 0 from array<string> to array<int>.

如果字段只是 stringint 类型,我就不会遇到这个错误。

有没有一种方法可以对数组进行强制转换?

3个回答

3
使用explode()collect_list()重新组装数组。
初始字符串数组示例:
hive> select array('1','2','3') string_array;
OK
string_array
["1","2","3"]
Time taken: 1.109 seconds, Fetched: 1 row(s)

数组转换:

hive> select collect_list(cast(array_element as int)) int_array --cast and collect array
       from( select explode(string_array) array_element         --explode array
               from (select array('1','2','3') string_array     --initial array
                    )s 
           )s;

结果:

OK
int_array
[1,2,3]
Time taken: 44.668 seconds, Fetched: 1 row(s)

如果你想在插入和选择查询中添加更多列,则可以使用 lateral view [outer]

select col1, col2, collect_list(cast(array_element as int)) int_array
 from
(
select col1, col2 , array_element         
  from table
       lateral view outer explode(string_array) a as array_element         
)s
group by col1, col2
;

谢谢!我会尝试的! - dolphinZhang
1
选择 col1、col2 和 collect_list(cast(array_element as int)) int_array 从 ( 选择 col1、col2、array_element
来自 table lateral view outer explode(string_array) a as array_element
)s 按 col1、col2 分组 ;``` 解决我的问题
- dolphinZhang
选择 col1、col2 和 collect_list(cast(array_element as int)) int_array from ( select col1, col2 , array_element from table lateral view outer explode(string_array) a as array_element )s group by col1, col2 这个语句是可以正常工作的。但我有一个问题,那就是数组元素如何保持与原始数组相同的顺序? - ningyuwhut
1
在这个例子中,顺序不被保留(不能保证)。为了获得相同的顺序,请使用 posexplode 代替 explode 并获取位置,并在内部子查询中使用 distribute by col1、col2 sort by position,就像这个答案中所示:https://stackoverflow.com/a/61936243/2700344 - leftjoin

2
Brickhouse jar可以比将它们转换为列表并收集回来更快地完成此操作。请将此jar添加到hdfs位置。
使用下面的链接下载brick house jar
add jar hdfs://hadoop-/pathtojar/brickhouse-0.7.1.jar;   
create temporary function cast_array as 'brickhouse.udf.collect.CastArrayUDF';   
select cast_array(columns, 'int') AS columname from table;  
select cast_array(columns, 'string') AS columname from table

0
有没有办法对数组进行强制转换?
不太容易。如果您知道数组的大小,可以手动进行强制转换,但如果不知道,可能需要使用结构体。请参阅我对类似问题的回答

另外:我无法对其他答案进行投票,但它在嵌套选择中失败,其中有多个数组。

与其将数组元素转换并重构原始数组,不如将所有元素转换后合并到单个数组中。例如:

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
    >     collect_list(cast(array_element as double)) int_array
    > from (
    >     select
    >         explode(my_array) array_element
    >     from (
    >         select
    >             my_array
    >         from array_table limit 3
    >     ) X
    > ) s;
OK
[0.20296966,0.17753501,-0.03543373,0.16155224,0.1945944,0.09167781,0.025892768,0.023214806,-0.003712816]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接