Wednesday, March 29, 2017

Lateral view explode() function in Hive

INTRODUCTION

In this article, we are going see how lateral view explode() function works in Hive. The explode() function will help to display the array or list of data into individual items in the RDBMS.

I am going to cover the below topics in this article.
  1. explode() function
  2. Lateral view explode function
  3. Use explode() function for a UDF returns List
  4. Use explode() function for a UDF returns Map
  5. Lateral view explode function for UDF
For all the below examples, I have used the table STUDENT table from my previous post on how to insert values into hive table directly

1. explode() function

If we wanted to display the subject of a student into a separate line, we can use explode function as below.

Query
SELECT explode(student) from student where id='100';

Output
Mathematics
Biology
Physics
Chemistry

2. Lateral View explode() function

If we wanted to display the subject along with the student id, we can use explode function with the lateral view.
Query
hive> SELECT s.id, ex.subject
   > FROM student s
   > LATERAL VIEW explode(subjects) ex
   > AS subject;

Output

Note: soon will cover the other topics.

No comments :

Post a Comment