Apache Pig Tutorial on Apache Pig Filter Operator

the filter operator is used to select the required tuples from a relation based on a condition.

syntax

given below is the syntax of the filter operator.

grunt> relation2_name = filter relation1_name by (condition);

example

assume that we have a file named student_details.txt in the hdfs directory /pig_data/ as shown below.

student_details.txt

001,rajiv,reddy,21,9848022337,hyderabad
002,siddarth,battacharya,22,9848022338,kolkata
003,rajesh,khanna,22,9848022339,delhi 
004,preethi,agarwal,21,9848022330,pune 
005,trupthi,mohanthy,23,9848022336,bhuwaneshwar 
006,archana,mishra,23,9848022335,chennai 
007,komal,nayak,24,9848022334,trivendram 
008,bharathi,nambiayar,24,9848022333,chennai

and we have loaded this file into pig with the relation name student_details as shown below.

grunt> student_details = load 'hdfs://localhost:9000/pig_data/student_details.txt' using pigstorage(',')
   as (id:int, firstname:chararray, lastname:chararray, age:int, phone:chararray, city:chararray);

let us now use the filter operator to get the details of the students who belong to the city chennai.

filter_data = filter student_details by city == 'chennai';

verification

verify the relation filter_data using the dump operator as shown below.

grunt> dump filter_data;

output

it will produce the following output, displaying the contents of the relation filter_data as follows.

(6,archana,mishra,23,9848022335,chennai)
(8,bharathi,nambiayar,24,9848022333,chennai)