Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any Conditional IF like operator in Apache PIG?

Actually I am writing PIG Script and want to execute some set of statements if one of the condition is satisfied.

I have set one variable and checking for some value of that variable. Suppose

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

Can I do this in PIG Script? If yes, then how can I do this?

Thanks.

like image 825
Bhavesh Shah Avatar asked Jul 16 '13 06:07

Bhavesh Shah


People also ask

What are the Apache Pig operators?

The Apache Pig Operators is a high-level procedural language for querying large data sets using Hadoop and the Map Reduce Platform. A Pig Latin statement is an operator that takes a relation as input and produces another relation as output.

What are the exception handling operators in Pig script?

We can use following operators, for exception handling of Pig script. DUMP- “DUMP” operator displays the results on screen. DESCRIBE- “DESCRIBE” operator displays the schema of a particular relation. ILLUSTRATE- “ILLUSTRATE” operator displays step by step execution of a sequence of Pig statements.

What are valid identifiers in Pig?

Identifiers. Identifiers include the names of relations (aliases), fields, variables, and so on. In Pig, identifiers start with a letter and can be followed by any number of letters, digits, or underscores. Valid identifiers: A A123 abc_123_BeX_


3 Answers

Yes, Pig does offer an if-then-else construction, but it is not used in the way you're asking.

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));

You have to already have loaded the table A to do this. To execute control flow around entire Pig statements you'll need something like oozie, as suggested by Fakrudeen.

like image 141
Will High Avatar answered Oct 20 '22 03:10

Will High


You can create a Python wrapper around your Pig script. See Embedded Pig in the docs.

like image 42
Lorand Bendig Avatar answered Oct 20 '22 04:10

Lorand Bendig


Pig is data flow language not control flow. Only construct which comes close is PIG split, but it is very limited.

You can use oozie and its decision construct with two pig scripts.

like image 23
Fakrudeen Avatar answered Oct 20 '22 05:10

Fakrudeen