Apache pig-Eval function-Size() - Apache Pig

What is size function()?

The SIZE() function of Pig Latin is used to compute the number of elements based on any Pig data type.

Syntax

Given below is the syntax of the SIZE() function.
The return values vary according to the data types in Apache Pig.

Data typeValue

Data type
Value
int, long, float, double
For all these types, the size function returns 1.
Char array
For a char array, the size() function returns the number of characters in the array.
Byte array
For a bytearray, the size() function returns the number of bytes in the array.
Tuple
For a tuple, the size() function returns number of fields in the tuple.
Bag
For a bag, the size() function returns number of tuples in the bag.
Map
For a map, the size() function returns the number of key/value pairs in the map.

Example

Assume that we have a file named employee.txt in the HDFS directory /pig_data/ as shown below.
employee.txt
And we have loaded this file into Pig with the relation name employee_data as shown below.
Calculating the Size of the Type
To calculate the size of the type of a particular column, we can use the SIZE() function. Let’s calculate the size of the name type as shown below.
Verification
Verify the relation size using the DUMP operator as shown below.
Output
It will produce the following output, displaying the contents of the relation size as follows. In the example, we have calculated the size of the name column. Since it is of varchar type, the SIZE() function gives you the number of characters in the name of each employee.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Apache Pig Topics