[Solved-2 Solutions] How to convert fields to rows in Pig ?



Problem:

How to convert fields to rows in Pig ?

Solution 1:

  • Here is an example to convert fields into row
> A = load 'input.txt';
> dump A
(0,1,2,3,4,5,6,7,8,9)
> B = foreach A generate FLATTEN(TOBAG(*));
> dump B
(0)
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)

Solution 2:

  • For each of the fields in the tuple it would create a new tuple with the field value and add it to a databag.

Example

 public DataBag exec(Tuple tuple) throws IOException {
    DataBag db = BagFactory.getInstance().newDefaultBag();
    for(int i = 0; i < tuple.size(); ++i){
        DefaultTuple dt = new DefaultTuple();
        dt.append(tuple.get(i));
        db.add(dt);
    }
    return db;
}
  • In your script you could 'FLATTEN' the results and put the single values back into individual tuples.

Related Searches to How to convert fields to rows in Pig ?