如何在Hive中将数据插入复杂数据类型“Struct”

时间:2016-09-08 15:09:24

标签: hadoop struct hive hiveql complextype

我对Hive和Stack Overflow完全不熟悉。我正在尝试创建一个复杂数据类型为“STRUCT”的表,然后在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;

我收到以下错误:

  

编译语句时出错:FAILED:semanticException [错误   10044]:因为列号类型而无法插入目标   不同的'struct_test':无法将第0列从struct转换为   阵列&GT;

我能够成功使用类似的代码来创建和填充数据类型数组,但是我在使用Struct时遇到了困难。我已经尝试了很多我在网上找到的代码示例,但它们似乎都不适合我......我真的很感激一些帮助,因为我已经坚持了很长一段时间了!感谢。

4 个答案:

答案 0 :(得分:8)

你的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;

答案 1 :(得分:7)

您不能直接在Hive中插入复杂数据类型。对于插入结构,您有函数named_struct。您需要创建一个虚拟表,其中包含要插入所需表的“结构”列中的数据。 就像在你的情况下创建一个虚拟表

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

然后插入所需的表格

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

答案 2 :(得分:2)

无需创建任何虚拟表:只需使用命令:

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

答案 3 :(得分:0)

是可能的:

你必须从句子或其他表中给出句子中的列名。

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
相关问题