Apache Pig - MAX()

The Pig Latin MAX() function is used to calculate the highest value for a column (numeric values or chararrays) in a single-column bag. While calculating the maximum value, the Max() function ignores the NULL values.
Note −
To get the global maximum value, we need to perform a Group All operation, and calculate the maximum value using the MAX() function.
To get the global maximum value, we need to perform a Group All operation, and calculate the maximum value using the MAX() function.
To get the maximum value of a group, we need to group it using the Group By operator and proceed with the maximum function.
To get the maximum value of a group, we need to group it using the Group By operator and proceed with the maximum function.
Given below is the syntax of the Max() function.
grunt> Max(expression)

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,89 
002,siddarth,Battacharya,22,9848022338,Kolkata,78 
003,Rajesh,Khanna,22,9848022339,Delhi,90 
004,Preethi,Agarwal,21,9848022330,Pune,93 
005,Trupthi,Mohanthy,23,9848022336,Bhuwaneshwar,75 
006,Archana,Mishra,23,9848022335,Chennai,87 
007,Komal,Nayak,24,9848022334,trivendram,83 
008,Bharathi,Nambiayar,24,9848022333,Chennai,72 

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, gpa:int);
We can use the built-in function MAX() (case-sensitive) to calculate the maximum value from a set of given numerical values. Let us group the relation student_details using the Group All operator, and store the result in the relation named student_group_all as shown below.
grunt> student_group_all = Group student_details All;
This will produce a relation as shown below.
grunt> Dump student_group_all;
  
(all,{(8,Bharathi,Nambiayar,24,9848022333,Chennai,72),
(7,Komal,Nayak,24,9848022 334,trivendram,83),
(6,Archana,Mishra,23,9848022335,Chennai,87),
(5,Trupthi,Mohan thy,23,9848022336,Bhuwaneshwar,75),
(4,Preethi,Agarwal,21,9848022330,Pune,93),
(3,Rajesh,Khanna,22,9848022339,Delhi,90),
(2,siddarth,Battacharya,22,9848022338,Ko lkata,78),
(1,Rajiv,Reddy,21,9848022337,Hyderabad,89)})

Let us now calculate the global maximum of GPA, i.e., maximum among the GPA values of all the students using the MAX() function as shown below.
grunt> student_gpa_max = foreach student_group_all  Generate
   (student_details.firstname, student_details.gpa), MAX(student_details.gpa);
Verify the relation student_gpa_max using the DUMP operator as shown below.
grunt> Dump student_gpa_max;
It will produce the following output, displaying the contents of the relation student_gpa_max.
(({(Bharathi),(Komal),(Archana),(Trupthi),(Preethi),(Rajesh),(siddarth),(Rajiv) } , 
    {    (72)    ,   (83)  ,     (87)   ,    (75)   ,   (93)   ,   (90)   ,     (78)   ,  (89)    }) ,93)


    46 Lectures 
     3.5 hours 

 Arnab Chakraborty

    23 Lectures 
     1.5 hours 

 Mukund Kumar Mishra

    16 Lectures 
     1 hours 

 Nilay Mehta

    52 Lectures 
     1.5 hours 

 Bigdata Engineer

    14 Lectures 
     1 hours 

 Bigdata Engineer

    23 Lectures 
     1 hours 

 Bigdata Engineer
 Print
 Add Notes
 Bookmark this page