The Pig tutorial shows you how to run two Pig scripts in local mode and hadoop mode.

The Pig tutorial file (pigtutorial.tar.gz) or the tutorial/pigtutorial.tar.gz file in the pig distribution) includes the Pig JAR file (pig.jar) and the tutorial files (tutorial.jar, Pigs scripts, log files). These files work with Hadoop 0.18 and provide everything you need to run the Pig scripts. To get started, follow these basic steps:

  1. Install Java.
  2. Download the Pig tutorial file and install Pig.
  3. Run the Pig scripts - in local mode or on a Hadoop cluster.

Java Installation

Make sure your run-time environment includes the following:

  1. Java 1.6 or higher (preferably from Sun)
  2. The JAVA_HOME environment variable is set the root of your Java installation.

Pig Installation

To install Pig, do the following:

  1. Download the Pig tutorial file to your local directory (pigtutorial.tar.gz)
  2. Unzip the Pig tutorial file (the files are stored in a newly created directory, pigtmp).
    $ tar -xzf pigtutorial.tar.gz
    
  3. Move to the pigtmp directory.
  4. Review the contents of the Pig tutorial file.
  5. Copy the pig.jar file to the appropriate directory on your system. For example: /home/me/pig.
  6. Create an environment variable, PIGDIR, and point it to your directory. For example: export PIGDIR=/home/me/pig (bash, sh) or setenv PIGDIR /home/me/pig (tcsh, csh).

Pig Scripts: Local Mode

To run the Pig scripts in local mode, do the following:

  1. Move to the pigtmp directory.
  2. Review Pig Script 1 and Pig Script 2.
  3. Execute the following command (using either script1-local.pig or script2-local.pig).
    $ java -cp $PIGDIR/pig.jar org.apache.pig.Main -x local script1-local.pig
    
  4. Review the result file (either script1-local-results.txt or script2-local-results.txt):
    $ ls -l script1-local-results.txt
    $ cat script1-local-results.txt
    

Pig Scripts: Hadoop Mode

To run the Pig scripts in hadoop (mapreduce) mode, do the following:

  1. Move to the pigtmp directory.
  2. Review Pig Script 1 and Pig_Script_2.
  3. Copy the excite.log.bz2 file from the pigtmp directory to the HDFS directory.
    $ hadoop fs -copyFromLocal excite.log.bz2 .
    
  4. Set the HADOOPSITEPATH environment variable to the location of your hadoop-site.xml file.
  5. Execute the following command (using either script1-hadoop.pig or script2-hadoop.pig):
    $ java -cp $PIGDIR/pig.jar:$HADOOPSITEPATH org.apache.pig.Main script1-hadoop.pig
    
    1.#6 Review the result files (located in either the script1-hadoop-results or script2-hadoop-results HDFS directory):
$ hadoop fs -ls script1-hadoop-results
$ hadoop fs -cat 'script1-hadoop-results/*' | less

Pig Tutorial File

The contents of the Pig tutorial file (pigtutorial.tar.gz) are described here.

File

Description

pig.jar

Pig JAR file

tutorial.jar

User-defined functions (UDFs) and Java classes

script1-local.pig

Pig Script 1, Query Phrase Popularity (local mode)

script1-hadoop.pig

Pig Script 1, Query Phrase Popularity (Hadoop cluster)

script2-local.pig

Pig Script 2, Temporal Query Phrase Popularity (local mode)

script2-hadoop.pig

Pig Script 2, Temporal Query Phrase Popularity (Hadoop cluster)

excite-small.log

Log file, Excite search engine (local mode)

excite.log.bz2

Log file, Excite search engine (Hadoop cluster)

A better-documented version of script1-local.pig can be found at https://cwiki.apache.org/confluence/download/attachments/27822259/script1-local-with-added-documentation.pig . It includes comments showing samples from each intermediate relation.

The user-defined functions (UDFs) are described here.

UDF

Description

ExtractHour

Extracts the hour from the record.

NGramGenerator

Composes n-grams from the set of words.

NonURLDetector

Removes the record if the query field is empty or a URL.

ScoreGenerator

Calculates a "popularity" score for the n-gram.

ToLower

Changes the query field to lowercase.

TutorialUtil

Divides the query string into a set of words.

Pig Script 1: Query Phrase Popularity

The Query Phrase Popularity script (script1-local.pig or script1-hadoop.pig) processes a search query log file from the Excite search engine and finds search phrases that occur with particular high frequency during certain times of the day.

The script is shown here:

Pig Script 2: Temporal Query Phrase Popularity

The Temporal Query Phrase Popularity script (script2-local.pig or script2-hadoop.pig) processes a search query log file from the Excite search engine and compares the occurrence of frequency of search phrases across two time periods separated by twelve hours.

The script is shown here: