Lab objectives

 Date 29.01.2017 Size 0.53 Mb.
Lab 3 – Pig, Hive, and JAQL

Lab objectives

In this lab you will practice what you have learned in this lesson, specifically you will practice with Pig, Hive, and Jaql languages.

Lab instructions

This lab has been developed as a tutorial. Simply execute the commands provided, and analyze the results.

BigInsights should be already started before working on this lab (Refer to “Lab 0 – Setup” for instructions on starting BigInsights)

Part 1: Pig

In this tutorial, we are going to use Apache Pig to process the 1988 subset of the Google Books 1-gram records to produce a histogram of the frequencies of words of each length. A subset of this database (0.5 million records) has been stored in the file googlebooks-1988.csv under /BigDataUniversity/input directory.

1. Let us examine the format of the Google Books 1-gram records:

The columns these data represent are the word, the year, the number of occurrences of that word in the corpus, the number of pages on which that word appeared, and the number of books in which that word appeared.

2. Copy the data file into HDFS.

3. Start pig.

> pig
grunt>

4. We are going to use a Pig UDF to compute the length of each word. The UDF is located inside the piggybank.jar file. We use the REGISTER command to load this jar file:

grunt> REGISTER
/opt/ibm/biginsights/pig/contrib/piggybank/java/piggybank.jar;

The first step in processing the data is to LOAD it.

year:int, wordcount:int, pagecount:int, bookcount:int);

This returns instantly. The processing is delayed until the data needs to be reported.

To produce a histogram, we want to group by the length of the word:

grunt> grouped = GROUP records by
org.apache.pig.piggybank.evaluation.string.LENGTH(word);

Sum the word counts for each word length using the SUM function with the FOREACH GENERATE command.

grunt> final = FOREACH grouped GENERATE group,
SUM(records.wordcount);

Use the DUMP command to print the result to the console. This will cause all the previous steps to be executed.

grunt> DUMP final;

This should produce output like the following:

5. Quit pig.

grunt> quit

Part 2: Hive

In this tutorial, we are going to use Hive to process the 1988 subset of the Google Books 1-gram records to produce a histogram of the frequencies of words of each length. A subset of this database (0.5 million records) has been stored in the file googlebooks-1988.csv under /BigDataUniversity/input directory.

1. Start hive.

> hive
hive>

1. Create a table called wordlist.

hive> CREATE TABLE wordlist (word STRING, year INT, wordcount INT, pagecount INT, bookcount INT) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

1. Load the data from the googlebooks-1988.csv file into the wordlist table.

2. Create a table named wordlengths to store the counts for each word length for our histogram.

hive> CREATE TABLE wordlengths (wordlength INT, wordcount INT);

3. Fill the wordlengths table with word length data from the wordlist table calculated with the length function..

hive> INSERT OVERWRITE TABLE wordlengths SELECT length(word), wordcount FROM wordlist;

4. Produce the histogram by summing the word counts grouped by word length.

hive> SELECT wordlength, sum(wordcount) FROM wordlengths group by wordlength;

1. Quit hive.

hive> quit;

Part 3: Jaql

In this tutorial, we are going to use Jaql to process the 1988 subset of the Google Books 1-gram records to produce a histogram of the frequencies of words of each length. A subset of this database (0.5 million records) has been stored in the file googlebooks-1988.csv under /BigDataUniversity/input directory.

1. Let us examine the format of the Google Books 1-gram records:

The columns these data represent are the word, the year, the number of occurrences of that word in the corpus, the number of pages on which that word appeared, and the number of books in which that word appeared.

2. Copy the googlebooks-1988.del file to HDFS.

1. Start the Jaql shell using the -c option to use the running Hadoop cluster.

> jaqlshell -c
jaql>

1. Read the comma delimited file from HDFS.

jaql> \$wordlist = read(del("googlebooks-1988.del", { schema: schema { word: string, year: long, wordcount: long, pagecount: long, bookcount: long } }));

2. Transform each word into its length by applying the strLen function.

jaql> \$wordlengths = \$wordlist -> transform { wordlength: strLen(\$.word), wordcount: \$.wordcount };

3. Produce the histogram by summing the word counts grouped by word length.

jaql> \$wordlengths -> group by \$word = {\$.wordlength} into { \$word.wordlength, counts: sum(\$[*].wordcount) };

This should produce output like the following:

4. Quit Jaql.

jaql> quit;

------ This is the end of this lab ------