pig tutorial - apache pig tutorial - Apache Pig - MAX() Function - pig latin - apache pig - pig hadoop



What is Max() function?

  • MAX() function is used to calculate the highest value for a column in a single-column bag
  • The Max() function will ignores the NULL values while calculating the maximum value
  • The Max() function will return the maximum value in a set of values.
  • MAX() requires a preceding GROUP ALL statement for global maximums and a GROUP BY statement for group maximums.
  • The MAX() function is a built-in function which can be called as Statistical Function.
 max function in apache pig

Learn apache pig - apache pig tutorial - max function in apache pig - apache pig examples - apache pig programs

Syntax

grunt> Max(expression)

Example

wikitechy_employee_details.txt

001,Suresh,Reddy,21,9848022337,Hyderabad,89 
002,Ramesh,Battacharya,22,9848022338,Kolkata,78 
003,Harish,Khanna,22,9848022339,Delhi,90 
004,Preethi,Agarwal,21,9848022330,Pune,93 
005,Sruthi,Mohanthy,23,9848022336,Bhuwaneshwar,75 
006,Keerthana,Mishra,23,9848022335,Chennai,87 
007,Kamal,Nayak,24,9848022334,trivendram,83 
008,Vanitha,Nambiayar,24,9848022333,Chennai,72 
  • We have loaded this file into Pig with the relation name called wikitechy_employee_details which is given below:
grunt> wikitechy_employee_details = LOAD 'hdfs://localhost:9000/pig_data/wikitechy_employee_details.txt' USING PigStorage(',')
   as (id:int, firstname:chararray, lastname:chararray, age:int, phone:chararray, city:chararray, gpa:int);

Calculating the Maximum GPA

  • We need to group the relation wikitechy_employee_details using the Group All operator, and we need to store the result in the relation called employee_group_all which is given below:
grunt> employee_group_all = Group wikitechy_employee_details All;

This will produce a relation for calculating the maximum GPA as shown below.

grunt> Dump employee_group_all;
(all,{(8,Vanitha,Nambiayar,24,9848022333,Chennai,72),
(7,Kamal,Nayak,24,9848022 334,trivendram,83),
(6,Keerthana,Mishra,23,9848022335,Chennai,87),
(5,Sruthi,Mohan thy,23,9848022336,Bhuwaneshwar,75),
(4,Preethi,Agarwal,21,9848022330,Pune,93),
(3,Harish,Khanna,22,9848022339,Delhi,90),
(2,Ramesh,Battacharya,22,9848022338,Ko lkata,78),
(1,Suresh,Reddy,21,9848022337,Hyderabad,89)})
  • Now, we will calculate the global maximum of GPA, of all the employees which is done using the MAX() function which is shown below.
grunt> employee_gpa_max = foreach employee_group_all  Generate
(wikitechy_employee_details.firstname,wikitechy_employee_details.gpa), MAX(wikitechy_employee_details.gpa);

Verification

grunt> Dump employee_gpa_max;

Output

(({(Vanitha),(Kamal),(Keerthana),(Sruthi),(Preethi),(Harish),(Ramesh),(Suresh) } , 
    {    (72)    ,   (83),     (87)   ,    (75)   ,   (93)   ,   (90)   ,     (78)   , (89)    }), 93)

Related Searches to Apache Pig - MAX() Function