Hive Complex Data Types Example

Our HadoopTpoint App is now available in google play store,please rate and comment it in play store : W3Schools

Generally in hive and other all databases almost same data types like primitive data types,string data types,Date/Time data types and Miscellaneous Data Type but in hive data types there is also an another data type feature is called complex data types.This complex data types are very useful and powerful in our hive.here is Hive Complex Data Types Example in understanding way.

Generally Hive supports 4 types of complex data types

  • ARRAY
  • MAP
  • STRUCT
  • UNIONTYPE

Hive Complex Data Types Example

hive array data type example

Hive Array behavior is same as Java Array.It is an ordered collection of elements.The all elements in the array is must be same data type.

 

In the above example we are using hive array data type for storing all persons age by using that hive array in a single element.We should use only one data type in array(if data type is int all elements will int,if data type is string all elements will string).

hive map data type example

Hive Map data type  is one type of Hive complex data types example It is an unordered collection of key-value pairs.Keys must be of primitive types.Values can be of any type.

if you observed the above query in map function first element is string (that means primitive data types)  and second element is any type (here we used Boolean (Miscellaneous Data Type)) . In query we used map keys terminated by syntax.

hive struct data type example

It is similar to STRUCT in C language. It is a collection of elements of different types.we can use any data type to specify this struct data type.Elements in STRUCT type are accessed using the DOT (.) notation.

Example – For a column c of type STRUCT {a INT; b INT} the a field is accessed by the expression c.a

if you observed the above query in struct function first element is string and second element is also string (There is no restrictions we can use any type of data types for struct) . In query we used collection items terminated by terminated by syntax.

hive union data type example

UNIONTYPE is collection of Heterogeneous data types.It is similar to Unions in C. At any point of time, an Union Type can hold any one (exactly one) data type from its specified data types

Example  

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

Hive Complex Data Types Example

Hive Complex Data Types Example

Real Time Examples for Hive complex Data types

 

 

Hive Table Describe

 

Hive Table Query

 

OK

1000001     {"fname":"mahesh","lname":"chimmiri"}     {"city":"hyd","state":"ap"}      [40,29,39]  {"happy":true}

1000002     {"fname":"ramesh","lname":"chimmili"}     {"city":"ogl","state":"ap"}      [20,39,19]  {"sad":false}
hive> select cadd.city from empmahidata;

Total MapReduce jobs = 1

Launching Job 1 out of 1

Number of reduce tasks is set to 0 since there’s no reduce operator

Starting Job = job_201312230051_0004, Tracking URL = http://localhost:50030/jobdetails.jsp?jobid=job_201312230051_0004

Kill Command = /usr/lib/hadoop/bin/hadoop job  -Dmapred.job.tracker=localhost:8021 -kill job_201312230051_0004

2013-12-25 01:36:55,425 Stage-1 map = 0%,  reduce = 0%

2013-12-25 01:36:57,451 Stage-1 map = 100%,  reduce = 0%

2013-12-25 01:36:59,483 Stage-1 map = 100%,  reduce = 100%

Ended Job = job_201312230051_0004

OK

hyd

ogl

 

Time taken: 8.016 seconds

by using struct data type we can access the sub element of main element  by using select cadd.city from empmahidata . This is all about Hive Complex Data Types Example.Please do subscribe for more updates from us and please comment your opinion about this post and share this post

Comments

  1. Hi,

    I have some requirement like this

    source:
    I m getting below data from xml’s after conversion
    colum1, column2, colum3, colum4
    1, sai, chaitanya, [‘1′,’2′], [‘3′,’4′]

    output: I want to maintain tightly couple relationship between array’s,my output should be like below
    output:
    1,sai,chaitanya,[[‘1′,’3′],[‘2′,[‘4′]]]

    can you please help me
    Thanks In advance

    Sai chaitanya

Speak Your Mind

*