Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

how to handle null or empty scenarios in foreach in pigscript?

avatar
Expert Contributor
 
1 ACCEPTED SOLUTION

avatar
Guru

@Mahesh Deshmukh I'm not sure what your need is, but null values should be filtered first. The general rule of thumb in Pig is to "filter early and often" to minimize the amount of data that gets shuffled and sorted, so before the foreach:

For example:

a = "some Pig relation"
b = filter a by $1 is not null;   //filter out tuples where the $1 field is null
c = foreach b generate ...	  //no need to worry about $1 being null

The term "empty" refers to bags typically, and in particular you can use the isEmpty function to check if a bag is empty. You normally do this after a GROUP command:

a = "some Pig relation"
b = group a by $3;
c = filter b by not IsEmpty(group);

What are you trying to accomplish?

View solution in original post

2 REPLIES 2

avatar
Master Mentor

Please see the official docs. Both null and IsEmpty is addressed https://pig.apache.org/docs/r0.15.0/basic.html#nulls

avatar
Guru

@Mahesh Deshmukh I'm not sure what your need is, but null values should be filtered first. The general rule of thumb in Pig is to "filter early and often" to minimize the amount of data that gets shuffled and sorted, so before the foreach:

For example:

a = "some Pig relation"
b = filter a by $1 is not null;   //filter out tuples where the $1 field is null
c = foreach b generate ...	  //no need to worry about $1 being null

The term "empty" refers to bags typically, and in particular you can use the isEmpty function to check if a bag is empty. You normally do this after a GROUP command:

a = "some Pig relation"
b = group a by $3;
c = filter b by not IsEmpty(group);

What are you trying to accomplish?