在配置单元中作为select from array和struct表插入

xpszyzbs  于 2021-06-24  发布在  Hive
关注(0)|答案(2)|浏览(356)

我很难将结构化数组插入到表中,就像从另一个表中选择一样:
这是我的创建表:

create table temp4
(
nome_1 string
,array_nm1 array<struct<nome1:string, chave1:string, valor1:string, record1:struct<nome4:string>>>
,array_nm2 struct<nome2:string, chave2:string, valor2:string>
);

然后我将数据插入:

INSERT INTO temp4 select 
'artur',
ARRAY(NAMED_STRUCT('nome1','artur','chave1','a1','valor1','23', 'record1', NAMED_STRUCT('nome4','lucas'))) as array_nm1,
NAMED_STRUCT('nome2','seidi','chave2','s1','valor2','08') as array_nm2
FROM (select '1' ) t;

这个很好用。因此,我创建了另一个表,并尝试从“temp4”输入数据,但无法将数据从temp4插入到另一个复杂的表中。比如:

create table temp5
(
name_1 string
,array_nm3 array<struct<name1:string,key1:string,value1:string,rec1:struct<name4:string>>>
,array_nm4 struct<name2:string,key2:string,value2:string>
);

我试过这样的方法:

INSERT into temp5
select
name_1 as name_1,
array(named_struct(name1,nome1,key1,chave1, value1,valor1, rec1, named_struct(name4, nome4))) as array_nm3,
named_struct(name2,nome2,key2,chave2, value2, valor2) as array_nm4
from temp4

但没有成功。还尝试将array\u nm1.nome1用作array\u nm3.name1,但不起作用。

vybvopom

vybvopom1#

尝试按如下所示更改插入查询。在命名的结构中将键设为字符串值。同样命名的结构键应该与temp5 ddl中定义的键匹配。

INSERT into temp5
select
 name_1 as name_1,
 array(named_struct('name1',nome1,'key1',chave1, 'value1',valor1, 'rec1',named_struct('name4', nome4))) as array_nm3,
 named_struct('name2',nome2,'key2',chave2, 'value2', valor2) as array_nm4

from temp4
bvuwiixz

bvuwiixz2#

找到解决方案:

INSERT into temp5
select
nome_1 as name_1,
array(named_struct('name1',tb.array_nm1[0].nome1,'key1',tb.array_nm1[0].chave1, 'value1',tb.array_nm1[0].valor1, 'rec1',named_struct('name4', tb.array_nm1[0].record1.nome4))) as array_nm3,
named_struct('name2',tb.array_nm2.nome2,'key2',tb.array_nm2.chave2, 'value2', tb.array_nm2.valor2) as array_nm4
from temp4 as tb

相关问题