pig tutorial - apache pig tutorial - Apache Pig - Order By - pig latin - apache pig - pig hadoop
What is ORDER BY operator in Apache Pig ?
- The ORDER BY operator is used to display the contents of a relation in a sorted order based on one or more fields. (or) ORDER BY operator is used to sort the data in ascending or descending order, based on one or more columns.
- Some data sort the results in an ascending order by default.
- Sorts relations by a specific criteria

Syntax
grunt> Relation_name2 = ORDER Relation_name1 BY (ASC|DESC);
Example
- Ensure that we have a file named wikitechy_employee_details.txt in the HDFS directory /pig_data/ as given below.
Wikitechy_employee_details.txt
111,Anu,Shankar,23,9876543210,Chennai
112,Barvathi,Nambiayar,24,9876543211,Chennai
113,Kajal,Nayak,24,9876543212,Trivendram
114,Preethi,Antony,21,9876543213,Pune
115,Raj,Gopal,21,9876543214,Hyderabad
116,Yashika,Kannan,22,9876543215,Delhi
117,siddu,Narayanan,22,9876543216,Kolkata
118,Timple,Mohanthy,23,9876543217,Bhuwaneshwar
- And we have loaded this file into Pig with the relation name wikitechy_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);
- Here sort the relation in a descending order based on the age of the employee and store it into another relation named order_by_data using the ORDER BY operator as given below.
grunt> order_by_data = ORDER wikitechy_employee_details BY age DESC;
Verification:
- Now verify the relation order_by_data using the DUMP operator as given below.
grunt> Dump order_by_data;
Output
- The following output, display the contents of the relation order_by_data.
113,Kajal,Nayak,24,9876543212,Trivendram
112,Barvathi,Nambiayar,24,9876543211,Chennai
118,Timple,Mohanthy,23,9876543217,Bhuwaneshwar
111,Anu,Shankar,23,9876543210,Chennai
117,siddu,Narayanan,22,9876543216,Kolkata
116,Yashika,Kannan,22,9876543215,Delhi
115,Raj,Gopal,21,9876543214,Hyderabad
114,Preethi,Antony,21,9876543213,Pune