[Solved-2 Solutions] Is there any Conditional IF like operator in Apache PIG ?



What is Conditional IF

  • Conditional Sentences are also known as Conditional Clauses or If Clauses. They are used to express that the action in the main clause (without if) can only take place if a certain condition (in the clause with if) is fulfilled.

Problem :

If you want to execute some set of statements in pig script if one of the condition is satisfied. You have set one variable and checking for some value of that variable.

if flag==0 then
  A = LOAD 'file' using PigStorage() as (f1:int, ....);
  B = ...;
  C = ....;
else 
  again some Pig Latin statements
if flag==0 then
  A = LOAD 'file' using PigStorage() as (f1:int, ....);
  B = ...;
  C = ....;
else 
  again some Pig Latin statements

Can you do this in PIG Script ?

Solution 1:

  • Yes, Pig has if-then-else construction,.
  • Pig's if-then-else is an arithmetic operator invoked with the shorthand "condition ? true_value : false valueā€¯ as part of an expression such as:
X = FOREACH A GENERATE f2, (f2==1?1:COUNT(B));

Solution 2:

  • Pig is data flow language not control flow. Only construct which comes close is PIG split but it is very limited.
  • We can use oozie and its decision construct with two pig scripts.

Oozie

  • Oozie is a workflow scheduler system to manage Apache Hadoop jobs.
  • Oozie Workflow jobs are Directed Acyclical Graphs (DAGs) of actions.
  • To execute control flow around entire Pig statements we need something like oozie

Related Searches to Is there any Conditional IF like operator in Apache PIG?