[Solved-1 Solution] Pig Conditional Operators ?



What is foreach

  • The FOREACH operator is used to generate specified data transformations based on the column data.

Syntax

  • Given below is the syntax of FOREACH operator.
grunt> Relation_name2 = FOREACH Relatin_name1 GENERATE (required data);

Problem :

Consider the below relation

test = LOAD 'input' USING PigStorage(',') as (a:chararray, b:chararray);

Is there a way to achieve the following Consider the below relation

if (b == 1) {
    a = 'abc';
else if (b == 2) {
    a = 'xyz';
else 
    // retain whatever is there in the column 'a'

Solution 1:

  • We can do a FOREACH and use the ternary operator as follows.
test2 = FOREACH test GENERATE (b=='1' ? 'abc' : (b=='2' ? 'xyz' : a)) AS a, b;

Related Searches to Pig Conditional Operators