jean-luc T
jean-luc T

Reputation: 51

how to insert uniontype in hive

I read the famous example about union type in hive

CREATE TABLE union_test(foo UNIONTYPE<int, double, array<string>, struct<a:int,b:string>>);
SELECT foo FROM union_test;

{0:1}
{1:2.0}
{2:["three","four"]}
{3:{"a":5,"b":"five"}}
{2:["six","seven"]}
{3:{"a":8,"b":"eight"}}
{0:9}

Ok.. great. what is the syntax in hive sql to insert these lines ? I tried insert into union_test values (1.0) SemanticException [Error 10044]: Line 1:12 Cannot insert into target table because column number/types are different 'union_test': Cannot convert column 0 from string to uniontype,struct>.

On the other hand, if I create one table with a double, how can I feed it with union_test table ?

Surely there is a tip. Thanks

Upvotes: 0

Views: 3431

Answers (2)

Dean
Dean

Reputation: 1

CREATE table testtable5(c1 integer,c2 uniontype<integer,string>);
INSERT INTO testtable5 VALUES(5,create_union(0,1,'testing'));
SELECT create_union(if(c1<0,0,1),c1,c2) from testtable5;
INSERT INTO testtable5 VALUES(1,cretae_union(1,1,'testing'));
SELECT create_union(if(c1<0,0,1),c1,c2) from testtable5;

Upvotes: 0

Samson Scharfrichter
Samson Scharfrichter

Reputation: 9067

Did you consider looking at the documentation?

Straight from Hive Language Manual UDF, under "Complex type constructors"...

create_union (tag, val1, val2, ...)
Creates a union type with the value that is being pointed to by the tag parameter.


OK, that explanation about the "tag parameter" is very cryptic.
For examples, just look at the bottom of that blog post and/or at the answer to that question on the HortonWorks forum.

Upvotes: 1

Related Questions