- Apache Pig Tutorial
- Apache Pig - Home
- Apache Pig Introduction
- Apache Pig - Overview
- Apache Pig - Architecture
- Apache Pig Environment
- Apache Pig - Installation
- Apache Pig - Execution
- Apache Pig - Grunt Shell
- Pig Latin
- Pig Latin - Basics
- Load & Store Operators
- Apache Pig - Reading Data
- Apache Pig - Storing Data
- Diagnostic Operators
- Apache Pig - Diagnostic Operator
- Apache Pig - Describe Operator
- Apache Pig - Explain Operator
- Apache Pig - Illustrate Operator
- Grouping & Joining
- Apache Pig - Group Operator
- Apache Pig - Cogroup Operator
- Apache Pig - Join Operator
- Apache Pig - Cross Operator
- Combining & Splitting
- Apache Pig - Union Operator
- Apache Pig - Split Operator
- Pig Latin Built-In Functions
- Apache Pig - Eval Functions
- Load & Store Functions
- Apache Pig - Bag & Tuple Functions
- Apache Pig - String Functions
- Apache Pig - date-time Functions
- Apache Pig - Math Functions
- Other Modes Of Execution
- Apache Pig - User-Defined Functions
- Apache Pig - Running Scripts
- Apache Pig Useful Resources
- Apache Pig - Quick Guide
- Apache Pig - Useful Resources
- Apache Pig - Discussion
Apache Pig - TOP()
The TOP() function of Pig Latin is used to get the top N tuples of a bag. To this function, as inputs, we have to pass a relation, the number of tuples we want, and the column name whose values are being compared. This function will return a bag containing the required columns.
Syntax
Given below is the syntax of the function TOP().
grunt> TOP(topN,column,relation)
Example
Assume we have a file named employee_details.txt in the HDFS directory /pig_data/, with the following content.
employee_details.txt
001,Robin,22,newyork 002,BOB,23,Kolkata 003,Maya,23,Tokyo 004,Sara,25,London 005,David,23,Bhuwaneshwar 006,Maggy,22,Chennai 007,Robert,22,newyork 008,Syam,23,Kolkata 009,Mary,25,Tokyo 010,Saran,25,London 011,Stacy,25,Bhuwaneshwar 012,Kelly,22,Chennai
We have loaded this file into Pig with the relation name emp_data as shown below.
grunt> emp_data = LOAD 'hdfs://localhost:9000/pig_data/ employee_details.txt' USING PigStorage(',') as (id:int, name:chararray, age:int, city:chararray);
Group the relation emp_data by age, and store it in the relation emp_group.
grunt> emp_group = Group emp_data BY age;
Verify the relation emp_group using the Dump operator as shown below.
grunt> Dump emp_group; (22,{(12,Kelly,22,Chennai),(7,Robert,22,newyork),(6,Maggy,22,Chennai),(1,Robin, 22,newyork)}) (23,{(8,Syam,23,Kolkata),(5,David,23,Bhuwaneshwar),(3,Maya,23,Tokyo),(2,BOB,23, Kolkata)}) (25,{(11,Stacy,25,Bhuwaneshwar),(10,Saran,25,London),(9,Mary,25,Tokyo),(4,Sara, 25,London)})
Now, you can get the top two records of each group arranged in ascending order (based on id) as shown below.
grunt> data_top = FOREACH emp_group { top = TOP(2, 0, emp_data); GENERATE top; }
In this example we are retriving the top 2 tuples of a group having greater id. Since we are retriving top 2 tuples basing on the id, we are passing the index of the column name id as second parameter of TOP() function.
Verification
You can verify the contents of the data_top relation using the Dump operator as shown below.
grunt> Dump data_top; ({(7,Robert,22,newyork),(12,Kelly,22,Chennai)}) ({(5,David,23,Bhuwaneshwar),(8,Syam,23,Kolkata)}) ({(10,Saran,25,London),(11,Stacy,25,Bhuwaneshwar)})