Search code examples
if-statementhdfsapache-pig

If else condition in Pig to name output file


I am using pig to parse a file, I want to apply if-else condition to check if a statement is present or not. If the statemnet is not there i make the output file name as No otherwise i keep the output file name as it is of the statement. How can i do that.

Example:

amit|COD|12543|nagpur|MH|India

Now i want to check if my $1 is empty or not if it is empty i want to make my output file name to be N.txt or COD.txt(In this case $1 is COD it may be different in other situations, i want $1 value to be output name).

I checked and found that pig has conditional operator but no If-else can anyone please suggest some work around over here.


Solution

  • Bincond operator/ Ternary Opertor ( ?:) should help.

    output = FOREACH input_data GENERATE $0, ($1 IS NULL ? 'N.txt' : $1) AS output_file_name, $2..;
    

    Refer Pig Conditional Operators for an example.