Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Archives of Support Questions (Read Only)

This is an archived board for historical reference. Information and links may no longer be available or relevant
Announcements
This board is archived and read-only for historical reference. To ask a new question, please post a new topic on the appropriate active board.

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?