Support Questions
Find answers, ask questions, and share your expertise

How to get specified datatype in union type in hive???

Highlighted

How to get specified datatype in union type in hive???

Rising Star

I have column in hive table like

UNIONTYPE<int, double, array<string>, struct<a:int,b:string>

This one will results when select that column like following.

1)How to get particular tag/column type eg: struct (tag no: 3)

2)If the selected type is struct ,then how to access fields(filelds of struct) in it

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

Re: How to get specified datatype in union type in hive???

@Suresh Bonam, Union types can at any one point hold exactly one of their specified data types. You can create an instance of this type using the create_union UDF:

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}
{1:10.0}

The first part in the deserialized union is the tag which lets us know which part of the union is being used. In this example 0 means the first data_type from the definition which is an int and so on.

To create a union you have to provide this tag to the create_union UDF:

SELECT create_union(0, key), create_union(if(key<100, 0, 1), 2.0, value), create_union(1, "a", struct(2, "b")) FROM src LIMIT 2;

{0:"238"}	{1:"val_238"}	{1:{"col1":2,"col2":"b"}}
{0:"86"}	{0:2.0}	{1:{"col1":2,"col2":"b"}}

Hope this information will helps you getting the answer?

Re: How to get specified datatype in union type in hive???

Rising Star

I already have table with union type column in hive..my question is to how access the column so that i can access different datatypes in it.Suppose if we have struct we can access like s.x .Like that how to access datatypes inside union type.