如何将数据插入 Hive 中的复杂数据类型 "Struct"

How do you insert data into complex data type "Struct" in Hive

我对 Hive 和 Stack Overflow 完全陌生。我正在尝试创建一个具有复杂数据类型 "STRUCT" 的 table,然后使用 Hive 中的 INSERT INTO TABLE 填充它。

我正在使用以下代码:

CREATE TABLE struct_test
(
 address STRUCT<
                houseno:    STRING
               ,streetname: STRING
               ,town:       STRING
               ,postcode:   STRING
               >
);

INSERT INTO TABLE struct_test
SELECT NAMED_STRUCT('123', 'GoldStreet', London', W1a9JF') AS address
FROM dummy_table
LIMIT 1;

我收到以下错误:

Error while compiling statement: FAILED: semanticException [Error 10044]: Cannot insert into target because column number type are different 'struct_test': Cannot convert column 0 from struct to array>.

我能够使用类似的代码成功创建和填充数据类型数组,但在使用 Struct 时遇到困难。我已经尝试了很多我在网上找到的代码示例,但其中 none 似乎对我有用......我真的很感激这方面的一些帮助,因为我已经坚持了很长一段时间!谢谢

你的 sql 错误。你应该使用 sql:

INSERT INTO TABLE struct_test 
       SELECT NAMED_STRUCT('houseno','123','streetname','GoldStreet', 'town','London', 'postcode','W1a9JF') AS address 
           FROM dummy_table LIMIT 1;

您不能直接在 Hive.For 插入具有函数 named_struct 的结构中插入复杂数据类型。您需要创建一个虚拟 table,其中包含要插入到所需 table 的 Structs 列中的数据。 就像你的情况一样,创建一个虚拟 table

CREATE TABLE DUMMY ( houseno:    STRING
           ,streetname: STRING
           ,town:       STRING
           ,postcode:   STRING);

然后插入所需的 table do

INSERT INTO struct_test SELECT named_struct('houseno',houseno,'streetname'
                  ,streetname,'town',town,'postcode',postcode) from dummy;

可能:

你必须在句子中给出来自虚拟或其他 table 的列名。

INSERT INTO TABLE struct_test
SELECT NAMED_STRUCT('houseno','123','streetname','GoldStreet', 'town','London', 'postcode','W1a9JF') AS address 
 FROM dummy

INSERT INTO TABLE struct_test
SELECT NAMED_STRUCT('houseno',tb.col1,'streetname',tb.col2, 'town',tb.col3, 'postcode',tb.col4) AS address 
 FROM table1 as tb

无需创建任何虚拟对象 table :只需使用命令 :

insert into struct_test
select named_struct("houseno","house_number","streetname","xxxy","town","town_name","postcode","postcode_name");