4

Does pig script support if-else statement

here is what I want to do:

if($NAME=='Joey') Do something else Do something

is that doable?

Thanks

RhysJ
  • 153
  • 1
  • 3
  • 19
  • Duplicate http://stackoverflow.com/questions/17669522/is-there-any-conditional-if-like-operator-in-apache-pig – nobody Mar 03 '16 at 16:22
  • Duplicate of http://stackoverflow.com/questions/18913764/pig-split-lack-of-default-or-if-else – rahulbmv Mar 04 '16 at 00:22

3 Answers3

4

Its Called a "Bincond" Operator

Statements Like:

(Price > 75 ? 'High':'Low')  

are also valid

For Handling Null Records:

((Name is null or IsEmpty(Name)) ? {('unknown')} : Name)

Use them in a foreach statement with alias along other fields i.e:

A = load 'x/y/Price.csv' as (Name, Product, Price);
B = foreach A generate Name, Product, Price, (Price > 75 ? 'High':'Low') as Indicator;

dump B;
kayess
  • 3,384
  • 9
  • 28
  • 45
Varun Tandra
  • 281
  • 2
  • 4
2

You can use the conditional operator. For example

(Name=='Joey'? 'Yes':'No')
Ran Locar
  • 561
  • 2
  • 6
0

If I understand correctly (I started pig latin yesterday), pig doesn't have if-else or for statement, you have to use python or java to do so, see here : http://chimera.labs.oreilly.com/books/1234000001811/ch09.html

jlkrdy
  • 1