Wednesday, 16 December 2015

HIVE in Hadoop

HIVE in Hadoop
By Abhishek Sengupta (B14003)

     Hive is a component of Hortonworks Data Platform(HDP). Hive provides a SQL-like interface to data stored in HDP. Hive is considered to be more user friendly and is designed for users who are used to using SQL for querying data.
      To start, we first download the zip file containing the data sets from the following link.
http://seanlahman.com/files/database/lahman591-csv.zip

Once the zip file is downloaded, next step will be to unzip it. We will be using the Master.csv and Batting.csv files for this example.

The first step would be to upload the files. Since there was an access issue with ambari, we have used the HUE interface to upload the files. On clicking the file browser icon in hue we get the following landing page.



In the user directory, we create another directory by selecting the new directory option from the drop-down. The new directory created will be named admin. Both the Master.csv and Batting.csv files will be uploaded to in the admin directory.


Once we have uploaded the files via the hue interface, we log onto ambari. The landing page of ambari will look like this.


We can view the uploaded files in ambari by selecting the HDFS files option in the tool bar as shown in the following screenshot.


We migrate to the /user/admin directory and we can see that the two csv files are uploaded there.


Now as seen in the screenshot above, we open the hive view by clicking on the hive button. The Hive view provides a user interface to the Hive data warehouse system for Hadoop.


On right is a query editor. A query may span multiple lines. At the bottom there are buttons to Execute the query, Explain the query, Save the query with a name and to open a new Worksheet window for another query.
The first task we will do is create a table to hold the data. We will type the query into the composition area on the right handside. Once you have typed in the query hit the Execute button at the bottom. We use the following query.

create table temp_batting (col_value STRING);


The query does not return any results because at this point we just created an empty table. Once the query has executed we can see the Database Explorer at the left of the composition area and when folding down the default database we will see we have a new table called temp_batting.


The next step would be to load the data file Batting.csv into the table temp_batting. The query for this is

LOAD DATA INPATH '/user/admin/Batting.csv' OVERWRITE INTO TABLE temp_batting;

The above query is execute in the hue as a pig script.

After the loading is complete, we can use the select query to view the data in the table temp_batting.


Now that we have read the data in we can start working with it. The next thing is to extract the data. So first we will type in a query to create a new table called batting to hold the data. That table will have three columns for player_id, year and the number of runs.

create table batting (player_id STRING, year INT, runs INT);



Then we have to extract the data we want from temp_batting and copy it into batting. We will do this with a regexp pattern. A multi line query is required to do this job. The first line of the query creates the table batting. The three regexp_extract calls are going to extract the player_id, year and run fields from the table temp_batting. There should be no spaces in the regular expression pattern.

insert overwrite table batting  
SELECT  
  regexp_extract(col_value, '^(?:([^,]*)\,?){1}', 1) player_id,  
  regexp_extract(col_value, '^(?:([^,]*)\,?){2}', 1) year,  
  regexp_extract(col_value, '^(?:([^,]*)\,?){9}', 1) run  
from temp_batting;

The following screenshot demonstrates this in the query editor.


Now we can execute a select query and look at the Batting data.

SELECT year, max(runs) FROM batting GROUP BY year;

This query groups the data by year so we can find the highest score for each year. The query first groups all the records by year and then selects the player with the highest runs from each year. The output is shown below.



Now we need to go back and get the player_id(s) so we know who the player(s) was. We know that for a given year we can use the runs to find the player(s) for that year. So we can take the previous query and join it with the batting records to get the final table.

SELECT a.year, a.player_id, a.runs from batting a  
JOIN (SELECT year, max(runs) runs FROM batting GROUP BY year ) b  
ON (a.year = b.year AND a.runs = b.runs) ;



So this was how we obtained the results using hive.

Saturday, 5 December 2015

Executing PIG Scripts in Hadoop

Executing PIG Scripts in Hadoop
By - Abhishek Sengupta (B14003)

      Pig scripts are translated into a series of map reduce jobs that are run on the apache hadoop cluster. As part of the translation the Pig interpreter does perform optimizations to speed execution on Apache Hadoop. We are going to write a Pig script that will do our data analysis task.
      In this example we are going to read a baseball statistics file and the output will be the highest runs by a player for each year. This file has all the statistics from 1871–2011 and it contains over 90,000 rows.

The first step would be to download the data using the following link.
http://hortonassets.s3.amazonaws.com/pig/lahman591-csv.zip
You will have a zip file downloaded and you need to unzip it. We will be using the master.csv and batting.csv files.

The next step would be to start hortonworks virtual machine and log into HUE with the given credentials. The home page of HUE will be as follows.


We then click the file browser button on the top pane. We get the following screen.



We see the new button on the right hand side. Using that we create a new folder called pig_prac. We will see the new folder created by us in the file browser.


Next step is to upload the tho csv files in the pig_prac folder. The two files to be uploaded are batting.csv and master.csv.

Now that are the data files are uploaded, we can start writing our pig script. On the top pane we must click on the pig button. Upon landing on the pug user interface, we must click on the new script button in order to write a new pig script. Clicking on the new script button, we will be asked for a script name . We give our script name as t2. Clicking on the create button we get a text area in which we write the pig script. The entire pig script is shown in the following screenshot.


The load statement is used for loading the data.
batting = load 'Batting.csv' using PigStorage(',');
The PigStorage function does the loading and the argument passed is the comma (which acts as the data delimiter).

To filter out the first row of the data we have to add this line:
raw_runs = FILTER batting BY $1>0;

The next thing that is required is naming the fields. The FOREACH statement iterates through the batting data object. It will pull out the selected fields and assign them names. The code for doing this is:
runs = FOREACH raw_runs GENERATE $0 as playerID, $1 as year, $8 as runs;

The next statement is a GROUP statement that is used to group the elements in the runs field by the year field thus enabling the grp_data object to be indexed by year.
grp_data = GROUP runs by (year);

In the next FOREACH statement, we find the maximum runs for each year.
max_runs = FOREACH grp_data GENERATE group as grp,MAX(runs.runs) as max_runs;

Now that we have the maximum runs we need to join this with the runs data object so we can pick up the player id. The result will be a dataset with Year, PlayerID and Max Run. At the end we DUMP the data to the output.
join_max_run = JOIN max_runs by ($0, max_runs), runs by (year,runs);  
join_data = FOREACH join_max_run GENERATE $0 as year, $2 as playerID, $1 as runs;  
DUMP join_data;

Notice that in the previous screenshot there are save and execute buttons at the end of the text area where the pig script is typed. We first save the script and then click on execute.


We notice that the job has been started successfully. We select the job browser option on the top pane. This will land us in the job browser page.


In the job browser, we can see that the pig latin script has completed execution successfully. We now go back to our pig script and check the output. The output appears as follows.


Along with the output we can also check logs and error messages.
So we have created a simple Pig script that reads in some comma separated data. Once we have that set of records in Pig we pull out the playerID, year and runs fields from each row. We then sort them by year with one statement, GROUP. Then we find the maximum runs for each year.
This is finally mapped to the playerID and we produce our final dataset.