hadoop pig bag subtraction

I'm using Pig to parse my application logs to know which exposed methods have been called by a user that wasn't called the last month (by the same user).

I have managed to get methods called grouped by users before last month and after last month :

BEFORE last month relation sample

u1      {(m1),(m2)}
u2      {(m3),(m4)}

AFTER last month relation sample

u1      {(m1),(m3)}
u2      {(m1),(m4)}

What I want is to find, by users, which methods are in AFTER that are not in BEFORE, that is

NEWLY_CALLED expected result

u1      {(m3)}
u2      {(m1)}

Question : how can I do that in Pig ? is it possible to subtract bags ?

I have tried DIFF function but it does not perform the expected subtraction.




I think you need to write a UDF, then you can use

Set<T> setA ...
Set<T> setB ...
Set<T> setAminusB = setA.subtract(setB);

For those who might be interested, here is the subtract function I wrote the class below and proposed it to Pig (PIG-2881) :

 * Subtract takes two bags as arguments returns a new bag composed of tuples of first bag not in the second bag.<br>
 * If null bag arguments are replaced by empty bags. 
 * <p>
 * The implementation assumes that both bags being passed to this function will fit entirely into memory simultaneously.
 * </br>
 * If that is not the case the UDF will still function, but it will be <strong>very</strong> slow.
public class Subtract extends EvalFunc<DataBag> {

   * Compares the two bag fields from input Tuple and returns a new bag composed of elements of first bag not in the second bag.
   * @param input a tuple with exactly two bag fields.
   * @throws IOException if there are not exactly two fields in a tuple or if they are not {@link DataBag}.
  public DataBag exec(Tuple input) throws IOException {
    if (input.size() != 2) {
      throw new ExecException("Subtract expected two inputs but received " + input.size() + " inputs.");
    DataBag bag1 = toDataBag(input.get(0));
    DataBag bag2 = toDataBag(input.get(1));
    return subtract(bag1, bag2);

  private static String classNameOf(Object o) {
    return o == null ? "null" : o.getClass().getSimpleName();

  private static DataBag toDataBag(Object o) throws ExecException {
    if (o == null) {
      return BagFactory.getInstance().newDefaultBag();
    if (o instanceof DataBag) {
      return (DataBag) o;
    throw new ExecException(format("Expecting input to be DataBag only but was '%s'", classNameOf(o)));

  private static DataBag subtract(DataBag bag1, DataBag bag2) {
    DataBag subtractBag2FromBag1 = BagFactory.getInstance().newDefaultBag();
    // convert each bag to Set,  this does make the assumption that the sets will fit in memory.
    Set<Tuple> set1 = toSet(bag1);
    // remove elements of bag2 from set1 
    Iterator<Tuple> bag2Iterator = bag2.iterator();
    while (bag2Iterator.hasNext()) {
    // set1 now contains all elements of bag1 not in bag2 => we can build the resulting DataBag.
    for (Tuple tuple : set1) {
    return subtractBag2FromBag1;

  private static Set<Tuple> toSet(DataBag bag) {
    Set<Tuple> set = new HashSet<Tuple>();
    Iterator<Tuple> iterator = bag.iterator();
    while (iterator.hasNext()) {
    return set;


Need Your Help

Drop-Down Menu behind other elements or not working

html css drop-down-menu styling

So I am making a drop down-menu for my site and all the looks are fine, but when I hover over it, it appears but when I move my mouse over-top it disappears. When I alter the line height under "ul....

<noindex> tag for Google

seo googlebot yandex noindex

I would like to tell Google not to index certain parts of the page. In Yandex (russian SE) there's a very useful tag called &lt;noindex&gt;. How can it be done with Google?