pig tutorial - apache pig tutorial - Calculate Average using PIG ? - pig latin - apache pig - pig hadoop

What is Avg()

  • The Pig-Latin AVG() function is used to compute the average of the numerical values within a bag. While calculating the average value, the AVG() function ignores the NULL values.
  • To get the global average value, we need to perform a Group All operation, and calculate the average value using the AVG() function.
  • To get the average value of a group, we need to group it using the Group By operator and proceed with the average function.


  • Here is the syntax of the AVG() function.
grunt> AVG(expression) 


  • Assume that we have a file named employees_details.txt in the HDFS directory /pig_data/ as shown below.


  • We have loaded this file into Pig with the relation name employees_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);

Calculating the Average GPA

  • The built-in function AVG() (case-sensitive) is helps to calculate the average of a set of numerical values. Let’s group the relation employee_details using the Group All operator, and store the result in the relation named employee_group_all as shown below.
grunt> student_group_all = Group student_details All;
  • This gives the relationship as below.
grunt> Dump student_group_all;
(7,Komal,Nayak,24,9848022 334,trivendram,83),
(5,Trupthi,Mohan thy,23,9848022336,Bhuwaneshwar,75),
(3 ,Rajesh,Khanna,22,9848022339,Delhi,90),
(2,siddarth,Battacharya,22,9848022338,Ko lkata,78),

Related Searches to Calculate Average using PIG

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.