[Solved-2 Solutions] Flatten tuple like a bag in pig ?
Why Flatten
- flatten can also be applied to a tuple. In this case, it does not produce a cross product; instead, it elevates each field in the tuple to a top-level field. Again, empty tuples will remove the entire record. If the fields in a bag or tuple that is being flattened have names, Pig will carry those names along.
Problem:
- Dataset looks like the following:
- We would like to "flatten" the tuples in Pig, basically repeating each record for each value found in the inner-tuple, such that the expected output is:
We know this is possible when the tuples (1,2) and (2,9) are bags instead.
Solution 1:
Initially, we used STRSPLIT that generates a tuple resulting in the similar input as above but was unsuccessful.
Output :
- We can also use tokenize and that’s produces the desired result
Solution 2:
- The TOBAG() function of Pig Latin converts one or more expressions to individual tuples. And these tuples are placed in a bag.
Syntax
- Given below is the syntax of the TOBAG() function.
Here is the solution that use the TOBAG operator ?