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



What is MIN Function in Apache Pig ?

    • The MIN() function used in Apache Pig is used to get the minimum value for a certain column in a single-column bag
    • The MIN() function will ignore the NULL values while calculating the minimum values
    • The MIN() function will requires a preceding GROUP ALL statement for the global minimums and a GROUP BY statement for the group minimums.
    • The MIN() function return the minimum value which is given in a set of values and also will return the smallest value of the column which is given in the table.
    • The MIN() function will return the smallest numeric value in a range of values and it will ignores the empty cells.

    Learn apache-pig - apache-pig tutorial - - apache-pig examples - apache-pig programs

    Syntax

      grunt> MIN(expression)
      

      Example

        wikitechy_employee_details.txt

          001,Guru,Reddy,21,9848022337,Hyderabad,89 
          002,sabrina,Battacharya,22,9848022338,Kolkata,78 
          003,Ramesh,Khanna,22,9848022339,Delhi,90 
          004,Preethi,Agarwal,21,9848022330,Pune,93 
          005,Sruthi,Mohanthy,23,9848022336,Bhuwaneshwar,75 
          006,Vanitha,Mishra,23,9848022335,Chennai,87 
          007,Kamal,Nayak,24,9848022334,trivendram,83 
          008,Sharath,Nambiayar,24,9848022333,Chennai,72
          
          • We have loaded this file into Pig with the relation name 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 Minimum GPA

            • We can group the relation wikitechy_employee_details by using the Group All operator, and we can store the result in the relation called employee_group_all which is given below
            grunt> employee_group_all = Group wikitechy_employee_details All;
            
            • It will produce a relation for calculating the minimum gpa as shown below.
            <b>grunt> Dump employee_group_all;</b>
            (all,{(8,Sharath,Nambiayar,24,9848022333,Chennai,72),
            (7,Kamal,Nayak,24,9848022 334,trivendram,83),
            (6,Vanitha,Mishra,23,9848022335,Chennai,87),
            (5,Sruthi,Mohan thy,23,9848022336,Bhuwaneshwar,75),
            (4,Preethi,Agarwal,21,9848022330,Pune,93),
            (3 ,Ramesh,Khanna,22,9848022339,Delhi,90),
            (2,sabrina,Battacharya,22,9848022338,Ko lkata,78),
            (1,Guru,Reddy,21,9848022337,Hyderabad,89)})
            
            • Now, we need to calculate the global minimum of GPA, of all the employees by using the MIN() function which is given below:
            grunt> employee_gpa_min = foreach employee_group_all  Generate
               (employee_details.firstname, wikitechy_employee_details.gpa), MIN(wikitechy_employee_details.gpa);
            

            Verification

              grunt> Dump employee_gpa_min;
              

              Output

                (({(Sharath),(Kamal),(Vanitha),(Sruthi),(Preethi),(Ramesh),(sabrina),(Guru) } , 
                   {    (72)    ,   (83)  ,     (87)   ,    (75)   ,   (93)   ,   (90)   ,     (78)   ,  (89)    }) ,72)
                

                Related Searches to Apache Pig - MIN() Function