在Pig Latin中加载UDF时出现ClassCastException错误

时间:2014-02-12 12:15:15

标签: apache hadoop apache-pig

我是Pig latin的新手,并尝试实现UDF,但却出现了以下错误。

错误

[main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1200: Pig script failed to parse: 
<line 5, column 28> Failed to generate logical plan. Nested exception: java.lang.ClassCastException: power cannot be cast to org.apache.pig.EvalFunc
Details at logfile: /home/training/pig_1392253062989.log

脚本

REGISTER 'hdfs://cloudera-vm:8020/user/training/pig/pow.jar';

base = load 'hdfs://cloudera-vm:8020/user/training/pig/base' using PigStorage(',') as (id:int,base:int);

exponent = load 'hdfs://cloudera-vm:8020/user/training/pig/exponents' using 
PigStorage(',') as (id:int,exp:int);

tab = join base by id, exponent by id;

tab2 = foreach tab generate $1 as base, $3 as exp;

fin = foreach tab2 generate power(tab2.$0,tab2.$1);

UDF

import java.io.IOException;

import org.apache.pig.EvalFunc;
import org.apache.pig.data.Tuple;


public class power extends EvalFunc<Integer>
{

    public Integer exec(Tuple arg0) throws IOException {
        int base = (Integer)arg0.get(0);
        int exponent = (Integer)arg0.get(1);
        int result=1;

        for(int i=1;i<=exponent;i++)
            result=result*base;

        return result;
    }

}

数据文件

Base

1,2    
2,3
3,4    
4,5
5,6
6,7
7,8
8,9
9,10

exponents

1,2
2,3
3,3
4,4
5,2
6,5
7,7
8,4
9,5

2 个答案:

答案 0 :(得分:0)

如果您的目标是计算tab2.$0 ^ tab2.$1,则可能需要考虑使用内置POW。否则,如果您真的想使用自己的UDF,我建议将该类放在包中,在PIG脚本中使用完全限定名,并检查您的类路径。您可能在类路径中的某处有错误的类pow

答案 1 :(得分:0)

好的问题出现在我的脚本命令中,因为我没有传递两个输入,导致出现“越过第二个字段时越界访问”错误

注册'hdfs://0.0.0.0:8020 / user / training / pig / pow.jar';

base = load'hdfs://0.0.0.0:8020 / user / training / pig / base'使用PigStorage(',')as(id:int,base:int);

exponent = load'hdfs://0.0.0.0:8020 / user / training / pig / exponents'使用PigStorage(',')as(id:int,exp:int);

tab =由id加入base,以id为指数;

tab2 = foreach选项卡生成$ 1作为基础,$ 3作为exp;

fin = foreach tab2生成abc.power(base,exp)为res;

dump fin;

最终Java代码

enter code here

package abc;

import java.io.IOException;

import org.apache.pig.EvalFunc;
import org.apache.pig.data.Tuple;
import org.apache.pig.data.TupleFactory;


public class power extends EvalFunc<Tuple> {

    public Tuple exec(Tuple input) throws IOException {

        int base = (Integer)input.get(0);
        int exponent = (Integer)input.get(1);
        double result=1.0;
        Tuple out = TupleFactory.getInstance().newTuple(3);

        for(int i=1;i<=exponent;i++)
                {result=result*base;}
        out.set(0, input.get(0));
        out.set(1, input.get(1));
        out.set(2, result);

        return out;

    }

}