Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Data Analysis Using Apache Hive and Apache Pig

Overview

Apache Hive, an open-source data warehouse system, is used with Apache Pig for loading and transforming unstructured, structured, or semi-structured data for data analysis and getting better business insights. Pig, a standard ETL scripting language, is used to export and import data into Apache Hive and to process large number of datasets. Pig can be used for ETL data pipeline and iterative processing.

In this blog, let us discuss about loading and storing data in Hive with Pig Relation using HCatalog.

Pre-requisites

Download and configure the following:

  • Hadoop Version 2.6.5: https://dist.apache.org/repos/dist/release/hadoop/common/
  • Hive 1.2.1: http://www-us.apache.org/dist/hive/
  • Apache Pig version 0.17.0-SNAPSHOT: http://www-eu.apache.org/dist/pig/

Use Case

In this blog, let us discuss about the below use case:

  • Loading unstructured data into Hive
  • Processing, transforming, and analyzing data in Pig
  • Loading structured data into a different table in Hive using Pig

Data Description

Two cricket data files with Indian Premier League data from 2008 to 2016 is used as a data source. The files are as follows:

  • matches.csv – Provides details about each match played
  • deliveries.csv – Provides details about consolidated deliveries of all the matches

These files are extracted and loaded into Hive. The data is further processed, transformed, and analyzed to get the winner for each season and the top 5 batsmen with maximum run in each season and overall season.

Synopsis

  • Create database and database tables in Hive
  • Import data into Hive tables
  • Call Hive SQL in Shell Script
  • View database architecture
  • Load and store Hive data into Pig relation
  • Call Pig script in Shell Script
  • Apply Pivot concept in Hive SQL
  • View Output

Creating Database and Database Tables in Hive

To create databases and database tables in Hive, save the below query as a SQL file (database_table_creation.sql):

Importing Data into Hive Tables

To load data from both the CSV files into Hive, save the below query as a SQL file (data_loading.sql):

Calling Hive SQL in Shell Script

To automatically create databases & database tables and to import data into Hive, call both the SQL files (database_table_creation.sql and data_loading.sql) using Shell Script.

Viewing Database Architecture

The database schema and tables created are as follows:

The raw matches.csv file loaded into Hive schema (ipl_stats.matches) is as follows:

The raw deliveries.csv file loaded into Hive schema (ipl_stats.deliveries) is as follows:

Loading and Storing Hive Data into Pig Relation

To load and store data from Hive into Pig relation and to perform data processing and transformation, save the below script as Pig file (most_run.pig):

Note: Create a Hive table before calling Pig file.

To write back the processed data into Hive, save the below script as a SQL file (most_run.sql):

Calling Pig Script in Shell Script

To automate ETL process, call files (most_run.pig, most_run.sql) using Shell Script.

The data loaded into Hive using Pig script is as follows:

Applying Pivot Concept in Hive SQL

As the data loaded into Hive is in rows, SQL Pivot concept is used to convert rows into columns for more data clarity and gaining better insights. User Defined Aggregation Function (UDAF) technique is used to perform pivot in Hive. In this use case, the pivot concept is applied to season and run rows alone.

To use Collect UDAF, add Brickhouse jar file into Hive class path.

The top 5 most run scored batsmen data for each season before applying pivot is shown as follows:

The top 5 most run scored batsmen data for each season after applying pivot is shown as follows:

Viewing Output

Viewing Winners of a Season

To view winners of each season, use the following Hive Sql query:

Viewing Top 5 Most Run Scored Batsmen

To view Top 5 most run scored batsmen, use the following Hive SQL query:

The top 5 most run scored batsmen is shown graphically using MS Excel as follows:

Viewing Year-wise Runs of Top 5 Batsmen

To view year-wise runs of the top 5 batsmen, use the following Hive SQL query:

The year-wise runs of the top 5 batsmen are shown graphically using MS Excel as follows:

References

  • Loading and Storing Hive Data into Pig:
    https://acadgild.com/blog/loading-and-storing-hive-data-into-pig/
  • Stack Overflow Community:
    https://stackoverflow.com/questions/23025380/how-to-transpose-pivot-data-in-hive
  • Sample Data Files and Code in GitHub:
    https://github.com/treselle-systems/data_analysis_using_apache_hive_and_apache_pig


This post first appeared on Front-end Code Review & Validation Tools | Treselle Systems, please read the originial post: here

Share the post

Data Analysis Using Apache Hive and Apache Pig

×

Subscribe to Front-end Code Review & Validation Tools | Treselle Systems

Get updates delivered right to your inbox!

Thank you for your subscription

×