Apache Pig Tutorial on Apache Pig Limit Operator

the limit operator is used to get a limited number of tuples from a relation.

syntax

given below is the syntax of the limit operator.

grunt> result = limit relation_name required number of tuples;

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);

now, let’s sort the relation in descending order based on the age of the student and store it into another relation named limit_data using the order by operator as shown below.

grunt> limit_data = limit student_details 4; 

verification

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

grunt> dump limit_data; 

output

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

(1,rajiv,reddy,21,9848022337,hyderabad) 
(2,siddarth,battacharya,22,9848022338,kolkata) 
(3,rajesh,khanna,22,9848022339,delhi) 
(4,preethi,agarwal,21,9848022330,pune)