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.
Syntax
- Here is the syntax of the AVG() function.
Example
- Assume that we have a file named employees_details.txt in the HDFS directory /pig_data/ as shown below.
employees_details.txt
- We have loaded this file into Pig with the relation name employees_details as shown below.
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.
- This gives the relationship as below.