Hive:在查询中将数组<string>转换为array <int>

Pie*_*and 6 arrays hadoop hive

我有两张桌子:

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

create table b (
`1` array<int>);
Run Code Online (Sandbox Code Playgroud)

我想把表格放在表格b中(表格b为空):

insert into table b
select * from a;
Run Code Online (Sandbox Code Playgroud)

这样做时我收到以下错误:

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>.
Run Code Online (Sandbox Code Playgroud)

如果字段只是类型string和字段,我不会得到这个错误int.

有没有办法用数组进行转换?

lef*_*oin 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)
Run Code Online (Sandbox Code Playgroud)

转换数组:

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;
Run Code Online (Sandbox Code Playgroud)

结果:

OK
int_array
[1,2,3]
Time taken: 44.668 seconds, Fetched: 1 row(s)
Run Code Online (Sandbox Code Playgroud)

如果您想在插入+选择查询中添加更多列,请使用 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
;
Run Code Online (Sandbox Code Playgroud)