I have a pig script.
Script.pig:
register /home/cloudera/Desktop/Pow.jar # registering the jar file
A = LOAD '/input.txt' using PigStorage(',') as (a1:int,a2:int,name:chararray); # loading the relation
B = foreach A generate A.a1,A.a2,Pow(A.a1,A.a2); # just generating field1,field2
dump B;# dumping the result
A java UDF to calculate power function.
import java.io.IOException;
import org.apache.pig.EvalFunc;
import org.apache.pig.PigWarning;
import org.apache.pig.data.Tuple;
// Pow function to calculate the power of two numbers
public class Pow extends EvalFunc<Long> {
public Long exec(Tuple input) throws IOException {
try {
int base = (Integer)input.get(0);# Getting the base value from tuple.
int exponent = (Integer)input.get(1);# Getting the second value from tuple.
long result = 1;
/* Probably not the most efficient method...*/
for (int i = 0; i < exponent; i++) {
long preresult = result;
result *= base;
if (preresult > 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);
}
}
}
Input file
Input.txt
1,2,Vijay
3,4,Ram
I am getting following error when I run the script
ERROR 1066: Unable to open iterator for alias
B. Backend error : Scalar has more than one row in the output.
1st : (1,2,Vijay), 2nd :(3,4,Ram)# Error at this point
Please help me to solve this problem and also am new to Apache pig.