Is there any Conditional IF like operator in Apache PIG?
Asked Answered
V

5

16

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.

Vraisemblance answered 16/7, 2013 at 6:31 Comment(2)
I came across conditional operator in Pig Latin like (a == b ? c1 : c2);. But How can I insert bulk of Pig Statements in between that?Vraisemblance
Bhavesh, You need to write a wrapper for that The recommended approach for writing programs that have conditional logic or loop constructs is to embed Pig Latin in another language like Python, JavaScript or Java, and manage the control flow from there.Stickseed
U
22

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.

Uranometry answered 30/9, 2013 at 17:10 Comment(1)
This works for conditions based on the data being processed, but not conditions/flags based on external parameters.Bluecoat
R
6

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

Repent answered 16/7, 2013 at 7:44 Comment(0)
A
5

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.

Aaron answered 17/7, 2013 at 11:36 Comment(0)
M
1

Create a UDF (say, in Java) and then embed that into your PIG script. You will need to 'register' the jar file that you generate after writing the UDF.

//(something like this), say your Java UDF class is UDFCondition & the generated jar file is PigUDFCondition.jar, then in your PIG Code

register PigUDFCondition.jar

X = foreach A generate UDFCondition(..flag...)
Mealtime answered 2/12, 2013 at 7:27 Comment(0)
H
0

There is a CASE Statement available from version 0.12 onwards.

Humbert answered 3/8, 2015 at 20:20 Comment(1)
And how would this help? Could you extend your answer by an example?Chook

© 2022 - 2024 — McMap. All rights reserved.