In this post, I am going to explain an example that cover following data types in Hive.
a. Array
b. Map
c. Struct
Array: Represent an ordered collection of elements. Elements in the array must be of same type and dynamic in size.
Example: hobbies ARRAY<STRING>
Map: It is a collection of <key, value> pairs.
Struct: It is similar to a structure in C, class in Java. Struct is a collection of different data types.
Example
CREATE TABLE emp ( id INT, name STRING, hobbies ARRAY<STRING>, experience MAP<STRING,STRING>, gender_age STRUCT<gender:STRING,age:INT> ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' COLLECTION ITEMS TERMINATED BY ',' MAP KEYS TERMINATED BY ':' STORED AS TEXTFILE;
In the above example,
a. ‘hobbies’ is of type array
b. ‘experience’ is of type map
c. ‘gender_age’ is of type struct.
Let’s experiment with the above example.
Step 1: Create emp table.
hive> CREATE TABLE emp ( > id INT, > name STRING, > hobbies ARRAY<STRING>, > technology_experience MAP<STRING,STRING>, > gender_age STRUCT<gender:STRING,age:INT> > ) > ROW FORMAT DELIMITED > FIELDS TERMINATED BY '|' > COLLECTION ITEMS TERMINATED BY ',' > MAP KEYS TERMINATED BY ':' > STORED AS TEXTFILE; OK Time taken: 0.045 seconds hive> ; hive> ; hive> DESC emp; OK id int name string hobbies array<string> technology_experience map<string,string> gender_age struct<gender:string,age:int> Time taken: 0.082 seconds, Fetched: 5 row(s)
Step 2: Create empInfo.txt file with below content.
empInfo.txt
1|Hari|Football,Cricket|Java:3.4Yrs,C:4.5Yrs|Male,30 2|Chamu|Trekking,Watching movies|Selenium:5.6Yrs|Feale,38 3|Sailu|Chess,Listening to music|EmbeddedC:9Yrs|Femle,32 4|Gopi|Cricket|Datastage:11Yrs|Male,32
Step 3: load empInfo.txt file content to emp table.
LOAD DATA LOCAL INPATH '/home/cloudera/examples/hive/empInfo.txt' into table emp;
hive> LOAD DATA LOCAL INPATH '/home/cloudera/examples/hive/empInfo.txt' into table emp; Loading data to table default.emp Table default.emp stats: [numFiles=1, totalSize=207] OK Time taken: 0.619 seconds
Print all the records from emp table.
hive> SELECT * FROM emp; OK 1 Hari ["Football","Cricket"] {"Java":"3.4Yrs","C":"4.5Yrs"} {"gender":"Male","age":30} 2 Chamu ["Trekking","Watching movies"] {"Selenium":"5.6Yrs"} {"gender":"Feale","age":38} 3 Sailu ["Chess","Listening to music"] {"EmbeddedC":"9Yrs"} {"gender":"Femle","age":32} 4 Gopi ["Cricket"] {"Datastage":"11Yrs"} {"gender":"Male","age":32} Time taken: 0.293 seconds, Fetched: 4 row(s)
Previous Next Home
No comments:
Post a Comment