Apache Pig –Eval Function- BagToString() - Apache Pig

What is Apache Pig - BagToString()?

The Pig Latin BagToString() function is used to concatenate the elements of a bag into a string. While concatenating, we can place a delimiter between these values (optional).
Generally bags are disordered and can be arranged by using ORDER BY operator.

Syntax

Given below is the syntax of the BagToString() function.

Example

Assume that we have a file named dateofbirth.txt in the HDFS directory /pig_data/ as shown below. This file contains the date-of-births.
dateofbirth.txt
And we have loaded this file into Pig with the relation name dob as shown below.

Converting Bag to String

Use the bagtostring() function to convert the data in the bag to string. Let us group the dob relation. The group operation will produce a bag containing all the tuples of the relation.
Group the relation dob using the Group All operator, and store the result in the relation named group_dob as shown below.
It will produce a relation as shown below.
Here, we can observe a bag having all the date-of-births as tuples of it. Now, let’s convert the bag to string using the function BagToString().

Verification

Verify the relation dob_string using the DUMP operator as shown below.

Output

It will produce the following output, displaying the contents of the relation dob_string.

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

Apache Pig Topics