我有一个猪脚本。
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
用于计算幂函数的java UDF。
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.txt
1,2,Vijay
3,4,Ram
运行脚本时出现以下错误
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
请帮我解决这个问题,也是Apache pig的新手。
答案 0 :(得分:1)
B = foreach A generate A.a1,A.a2,Pow(A.a1,A.a2);
应该是
B = foreach A generate a1,a2,Pow(a1,a2);