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.
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.


seen
ReplyDelete