Home Forums Pig UDF in Pig

This topic contains 5 replies, has 2 voices, and was last updated by  Jianyong Dai 11 months, 3 weeks ago.

  • Creator
    Topic
  • #41701

    Dan Sadler
    Member

    Hi all,

    I am trying to get a UDF to run in pig however I am facing some issues in that when I try and run the pig script it errors saying cannot instantiate mathPow with values ‘null’, if any body can help that would be great.

    Thanks

    The pig script is as follows:

    REGISTER MathPower.jar
    A = load ‘input’ using PigStorage(‘,’);
    C = foreach A generate $0 as x, $1 as z;
    B = foreach A generate powUDF.mathUDF(x, z);
    dump B;

    The input file contains:
    2,3
    4,5

    The java is as follows:

    package powUDF;
    import java.io.IOException;
    import org.apache.pig.EvalFunc;
    import org.apache.pig.data.Tuple;

    public class mathUDF extends EvalFunc {
    /**
    * A simple UDF that takes a value and raises it to the power of a second
    * value. It can be used in a Pig Latin script as Pow(x, y), where x and y
    * are both expected to be ints.
    */
    //public class Pow extends EvalFunc {

    public Long exec(Tuple input) throws IOException {
    try {
    /* Rather than give you explicit arguments, UDFs are always handed
    * a tuple. The UDF must know the arguments it expects and pull
    * them out of the tuple. These next two lines get the first and
    * second fields out of the input tuple that was handed in. Since
    * Tuple.get returns Objects, we must cast them to Integers. If
    * the case fails, an exception will be thrown.
    */
    int base = (Integer)input.get(0);
    int exponent = (Integer)input.get(1);
    long result = 1;

    /* Probably not the most efficient method…*/
    for (int i = 0; i result) {
    // We overflowed. Give a warning, but do not throw an
    // exception.
    warn(“Overflow!”, PigWarning.TOO_LARGE_FOR_INT);
    // Returning null will indicate to Pig that we failed but
    // we want to continue execution.
    return null;
    }
    }
    return result;
    } catch (Exception e) {
    // Throwing an exception will cause the task to fail.
    throw new IOException(“Something bad happened!”, e);
    }
    }
    }

Viewing 5 replies - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

  • Author
    Replies
  • #43084

    Jianyong Dai
    Participant

    public class mathUDF extends EvalFunc < Long >

    Collapse
    #43074

    Jianyong Dai
    Participant

    Another try :)

    public class mathUDF extends EvalFunc

    Collapse
    #43071

    Jianyong Dai
    Participant

    I still don’t know how to show angle brackets:
    public class mathUDF extends EvalFunc (angle brackets) Long (angle brackets)

    Collapse
    #43070

    Jianyong Dai
    Participant

    Sorry, the angle brackets does not show up.
    public class mathUDF extends EvalFunc\

    Collapse
    #43069

    Jianyong Dai
    Participant

    You need to declare a return type in your UDF:
    public class mathUDF extends EvalFunc

    Collapse
Viewing 5 replies - 1 through 5 (of 5 total)