Apache Pig Tutorial PDF
Apache Pig Tutorial PDF
Audience
This tutorial is meant for all those professionals working on Hadoop who would like to
perform MapReduce operations without having to type complex codes in Java.
Prerequisites
To make the most of this tutorial, you should have a good understanding of the basics of
Hadoop and HDFS commands. It will certainly help if you are good at SQL.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected]
i
Apache Pig
Table of Contents
About the Tutorial ............................................................................................................................................ i
Audience........................................................................................................................................................... i
Prerequisites..................................................................................................................................................... i
Copyright & Disclaimer..................................................................................................................................... i
Table of Contents ............................................................................................................................................ ii
PART 1: INTRODUCTION..............................................................................................................1
5. Grunt Shell.............................................................................................................................................18
Shell Commands ............................................................................................................................................ 18
Utility Commands .......................................................................................................................................... 19
ii
Apache Pig
iii
Apache Pig
v
Apache Pig
vi
Apache Pig
Part 1: Introduction
1
1. Apache Pig – Overview
To write data analysis programs, Pig provides a high-level language known as Pig Latin.
This language provides various operators using which programmers can develop their own
functions for reading, writing, and processing data.
To analyze data using Apache Pig, programmers need to write scripts using Pig Latin
language. All these scripts are internally converted to Map and Reduce tasks. Apache Pig
has a component known as Pig Engine that accepts the Pig Latin scripts as input and
converts those scripts into MapReduce jobs.
Using Pig Latin, programmers can perform MapReduce tasks easily without having
to type complex codes in Java.
Apache Pig uses multi-query approach, thereby reducing the length of codes.
For example, an operation that would require you to type 200 lines of code (LoC)
in Java can be easily done by typing as less as just 10 LoC in Apache Pig. Ultimately
Apache Pig reduces the development time by almost 16 times.
Pig Latin is SQL-like language and it is easy to learn Apache Pig when you are
familiar with SQL.
Apache Pig provides many built-in operators to support data operations like joins,
filters, ordering, etc. In addition, it also provides nested data types like tuples,
bags, and maps that are missing from MapReduce.
Features of Pig
Apache Pig comes with the following features:
Ease of programming: Pig Latin is similar to SQL and it is easy to write a Pig
script if you are good at SQL.
2
Apache Pig
Extensibility: Using the existing operators, users can develop their own functions
to read, process, and write data.
Handles all kinds of data: Apache Pig analyzes all kinds of data, both structured
as well as unstructured. It stores the results in HDFS.
Any novice programmer with a basic Exposure to Java is must to work with
knowledge of SQL can work conveniently MapReduce.
with Apache Pig.
Pig SQL
The data model in Apache Pig is nested The data model used in SQL is flat
relational. relational.
Apache Pig provides limited opportunity There is more opportunity for query
for Query optimization. optimization in SQL.
Apache Pig uses a language called Pig Hive uses a language called HiveQL. It
Latin. It was originally created at Yahoo. was originally created at Facebook.
4
Apache Pig
5
2. Apache Pig – Architecture
The language used to analyze data in Hadoop using Pig is known as Pig Latin. It is a high-
level data processing language which provides a rich set of data types and operators to
perform various operations on the data.
To perform a particular task Programmers using Pig, programmers need to write a Pig
script using the Pig Latin language, and execute them using any of the execution
mechanisms (Grunt Shell, UDFs, Embedded). After execution, these scripts will go through
a series of transformations applied by the Pig Framework, to produce the desired output.
Internally, Apache Pig converts these scripts into a series of MapReduce jobs, and thus, it
makes the programmer’s job easy. The architecture of Apache Pig is shown below.
6
Apache Pig
Parser
Initially the Pig Scripts are handled by the Parser. It checks the syntax of the script, does
type checking, and other miscellaneous checks. The output of the parser will be a DAG
(directed acyclic graph), which represents the Pig Latin statements and logical operators.
In the DAG, the logical operators of the script are represented as the nodes and the data
flows are represented as edges.
Optimizer
The logical plan (DAG) is passed to the logical optimizer, which carries out the logical
optimizations such as projection and pushdown.
Compiler
The compiler compiles the optimized logical plan into a series of MapReduce jobs.
Execution engine
Finally the MapReduce jobs are submitted to Hadoop in a sorted order. Finally, these
MapReduce jobs are executed on Hadoop producing the desired results.
Atom
Any single value in Pig Latin, irrespective of their data, type is known as an Atom. It is
stored as string and can be used as string and number. int, long, float, double, chararray,
and bytearray are the atomic values of Pig.
Tuple
A record that is formed by an ordered set of fields is known as a tuple, the fields can be
of any type. A tuple is similar to a row in a table of RDBMS.
Bag
A bag is an unordered set of tuples. In other words, a collection of tuples (non-unique) is
known as a bag. Each tuple can have any number of fields (flexible schema). A bag is
represented by ‘{}’. It is similar to a table in RDBMS, but unlike a table in RDBMS, it is
not necessary that every tuple contain the same number of fields or that the fields in the
same position (column) have the same type.
Relation
A relation is a bag of tuples. The relations in Pig Latin are unordered (there is no
guarantee that tuples are processed in any particular order).
Map
A map (or data map) is a set of key-value pairs. The key needs to be of type chararray
and should be unique. The value might be of any type. It is represented by ‘[]’
8
Apache Pig
Part 2: Environment
9
3. Apache Pig – Installation
This chapter explains the how to download, install, and set up Apache Pig in your system.
Prerequisites
It is essential that you have Hadoop and Java installed on your system before you go for
Apache Pig. Therefore, prior to installing Apache Pig, install Hadoop and Java by following
the steps given in the following link:
http://www.tutorialspoint.com/hadoop/hadoop_enviornment_setup.htm
DownloadApache Pig
First of all, download the latest version of Apache Pig from the website
https://pig.apache.org/.
Step 1
Open the homepage of Apache Pig website. Under the section News, click on the link
release page as shown in the following snapshot.
10
Apache Pig
Step 2
On clicking the specified link, you will be redirected to the Apache Pig Releases page.
On this page, under the Download section, you will have two links, namely, Pig 0.8 and
later and Pig 0.7 and before. Click on the link Pig 0.8 and later, then you will be
redirected to the page having a set of mirrors.
11
Apache Pig
Step 3
Choose and click any one of these mirrors as shown below.
Step 4
These mirrors will take you to the Pig Releases page. This page contains various versions
of Apache Pig. Click the latest version among them.
12
Apache Pig
Step 5
Within these folders, you will have the source and binary files of Apache Pig in various
distributions. Download the tar files of the source and binary files of Apache Pig 0.15,
pig-0.15.0-src.tar.gz and pig-0.15.0.tar.gz.
After downloading the Apache Pig software, install it in your Linux environment
by following the steps given below.
Step 1
Create a directory with the name Pig in the same directory where the installation
directories of Hadoop, Java, and other software were installed. (In our tutorial, we
have created the Pig directory in the user named Hadoop).
$ mkdir Pig
13
End of ebook preview
If you liked what you saw…
Buy it from our store @ https://store.tutorialspoint.com
14