Pig Latin â Basics 

Pig Latin is the language used to analyze data in Hadoop using Apache Pig.  In this chapter, we are going to discuss the basics of Pig Latin such as Pig Latin statements, data types, general and relational operators, and Pig Latin UDF’s.
As discussed in the previous chapters, the data model of Pig is fully nested. A Relation is the outermost structure of the Pig Latin data model. And it is a bag where −
A bag is a collection of tuples.
A tuple is an ordered set of fields.
A field is a piece of data.
While processing data using Pig Latin, statements are the basic constructs.
These statements work with relations. They include expressions and schemas.
These statements work with relations. They include expressions and schemas.
Every statement ends with a semicolon (;).
Every statement ends with a semicolon (;).
We will perform various operations using operators provided by Pig Latin, through statements.
We will perform various operations using operators provided by Pig Latin, through statements.
Except LOAD and STORE, while performing all other operations, Pig Latin statements take a relation as input and produce another relation as output.
Except LOAD and STORE, while performing all other operations, Pig Latin statements take a relation as input and produce another relation as output.
As soon as you enter a Load statement in the Grunt shell, its semantic checking will be carried out. To see the contents of the schema, you need to use the Dump operator. Only after performing the dump operation, the MapReduce job for loading the data into the file system will be carried out.
As soon as you enter a Load statement in the Grunt shell, its semantic checking will be carried out. To see the contents of the schema, you need to use the Dump operator. Only after performing the dump operation, the MapReduce job for loading the data into the file system will be carried out.
Given below is a Pig Latin statement, which loads data to Apache Pig.
grunt> Student_data = LOAD 'student_data.txt' USING PigStorage(',')as 
   ( id:int, firstname:chararray, lastname:chararray, phone:chararray, city:chararray );
Given below table describes the Pig Latin data types.
Represents a signed 32-bit integer.
Example : 8
Represents a signed 64-bit integer.
Example : 5L
Represents a signed 32-bit floating point.
Example : 5.5F
Represents a 64-bit floating point.
Example : 10.5
Represents a character array (string) in Unicode UTF-8 format.
Example : ‘tutorials point’
Represents a Byte array (blob).
Represents a Boolean value.
Example : true/ false.
Represents a date-time.
Example : 1970-01-01T00:00:00.000+00:00
Represents a Java BigInteger.
Example : 60708090709
Represents a  Java BigDecimal
Example : 185.98376256272893883
A tuple is an ordered set of fields.
Example : (raja, 30)
A bag is a collection of tuples.
Example : {(raju,30),(Mohhammad,45)}
A Map is a set of key-value pairs.
Example : [ ‘name’#’Raju’, ‘age’#30]
Values for all the above data types can be NULL. Apache Pig treats null values in a similar way as SQL does.
A null can be an unknown value or a non-existent value. It is used as a placeholder for optional values. These nulls can occur naturally or can be the result of an operation.
The following table describes the arithmetic operators of Pig Latin. Suppose a = 10 and b = 20.
Addition − Adds values on either side of the operator
Subtraction − Subtracts right hand operand from left hand operand
Multiplication − Multiplies values on either side of the operator
Division − Divides left hand operand by right hand operand
Modulus − Divides left hand operand by right hand operand and returns remainder
Bincond − Evaluates the Boolean operators. It has three operands as shown below.
variable x = (expression) ? value1 if true : value2 if false.
b = (a == 1)? 20: 30;
if a = 1 the value of b is 20.
if a!=1 the value of b is 30.
CASE
WHEN
THEN
ELSE END
Case − The case operator is equivalent to nested bincond operator.
CASE f2 % 2
WHEN 0 THEN 'even'
WHEN 1 THEN 'odd'
END
The following table describes the comparison operators of Pig Latin.
Equal − Checks if the values of two operands are equal or not; if yes, then the condition becomes true.
Not Equal − Checks if the values of two operands are equal or not. If the values are not equal, then condition becomes true.
Greater than − Checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition becomes true.
Less than − Checks if the value of the left operand is less than the value of the right operand. If yes, then the condition becomes true.
Greater than or equal to − Checks if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition becomes true.
Less than or equal to − Checks if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition becomes true.
Pattern matching − Checks whether the string in the left-hand side matches with the constant in the right-hand side.
The following table describes the Type construction operators of Pig Latin.
Tuple constructor operator − This operator is used to construct a tuple.
Bag constructor operator − This operator is used to construct a bag.
Map constructor operator − This operator is used to construct a tuple.
The following table describes the relational operators of Pig Latin.

    46 Lectures 
     3.5 hours 

 Arnab Chakraborty

    23 Lectures 
     1.5 hours 

 Mukund Kumar Mishra

    16 Lectures 
     1 hours 

 Nilay Mehta

    52 Lectures 
     1.5 hours 

 Bigdata Engineer

    14 Lectures 
     1 hours 

 Bigdata Engineer

    23 Lectures 
     1 hours 

 Bigdata Engineer
 Print
 Add Notes
 Bookmark this page