Apache Pig Tutorial on Apache Pig Order By

the order by operator is used to display the contents of a relation in a sorted order based on one or more fields.

syntax

given below is the syntax of the order by operator.

grunt> relation_name2 = order relatin_name1 by (asc|desc);

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 sort the relation in a descending order based on the age of the student and store it into another relation named order_by_data using the order by operator as shown below.

grunt> order_by_data = order student_details by age desc;

verification

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

grunt> dump order_by_data; 

output

it will produce the following output, displaying the contents of the relation order_by_data.

(8,bharathi,nambiayar,24,9848022333,chennai)
(7,komal,nayak,24,9848022334,trivendram)
(6,archana,mishra,23,9848022335,chennai) 
(5,trupthi,mohanthy,23,9848022336,bhuwaneshwar)
(3,rajesh,khanna,22,9848022339,delhi) 
(2,siddarth,battacharya,22,9848022338,kolkata)
(4,preethi,agarwal,21,9848022330,pune) 
(1,rajiv,reddy,21,9848022337,hyderabad)