Search code examples
hadoopapache-pig

Mathematical Equation in Apache Pig


Can anyone help me convert the below equation to Pig?

((score + score2)/3)*(1+(5/(10*sqrt(power((score - score2),2)+1))))​

Thank you.


Solution

  • Ya you can do this in Pig. You'll need the path to your piggybank.jar. There are a bunch of math functions in there. It is usually in the lib directory wherever you have pig installed. So, mine is in /usr/local/pig/lib/piggybank.jar. Here are the docs for all the functions (I think that is the link to the older version of Pig but it should be okay for reference to this question). I guess you'll have some relation with the variables score and score2 in it. We'll call the relation A.

    REGISTER '/path/to/pig/piggybank.jar'
    DEFINE SQRT org.apache.pig.piggybank.evaluation.math.SQRT;
    DEFINE POW org.apache.pig.piggybank.evaluation.math.POW;
    
    /**
     * You'll probably want to import some data here
     */
    
    B = FOREACH A GENERATE ((score + score2)/3)*(1+(5/(10*SQRT(1+POW((score - score2), 2)))));