Pig UDF没有找到WritableComparable

时间:2014-01-23 15:49:20

标签: java maven hadoop apache-pig

尝试使用maven(或使用我的IDE IntelliJ)编译Pig UDF时出现以下错误:

cannot access org.apache.hadoop.io.WritableComparable
class file for org.apache.hadoop.io.WritableComparable not found

所以我想我将hadoop-core的依赖项添加到我的POM文件中,但仍然没有变化,尽管我检查过并且WritableComparable类在jar中。

我的UDF类看起来像这样:

public class INCREMENTAL_UPDATE extends EvalFunc<DataBag> {

    TupleFactory tupleFactory = TupleFactory.getInstance();
    BagFactory bagFactory = BagFactory.getInstance();


    public DataBag exec(Tuple input) throws IOException {
        if (null == input || input.size() != 0) {
            return null;
        }

        try {
            DataBag inputbag = (DataBag) input.get(0);
            Iterator it = inputbag.iterator();

            DataBag outputbag = bagFactory.newDefaultBag();

            Tuple previousTuple = null;
            while (it.hasNext()) {
                Tuple currentTuple = (Tuple) it.next();
                Tuple outputTuple = tupleFactory.newTuple();

                for (int i = 0; i < currentTuple.size(); i++) {
                    Object currentvalue = currentTuple.get(i);
                    if (currentvalue == null) {
                        outputTuple.append(currentvalue);
                    } else {
                        outputTuple.append(previousTuple.get(i));
                    }
                }

                previousTuple = outputTuple;
                outputbag.add(outputTuple);
           }
            return outputbag;
        } catch (Exception e) {
           throw new IOException("UDF INCREMENTAL_UPDATE failed");
        }
    }
}

我还没弄清楚这是否真的有效,因为我不确定是否分发了UDF。如果是的话,之前的元组可能不会起作用。但是我甚至无法测试,因为我无法编译,因为上面的错误。

2 个答案:

答案 0 :(得分:7)

我也收到了这个错误,最后能够通过将Hadoop类路径和pig.jar文件一起编译来编译。

javac -cp pig-0.11.0.jar:\`hadoop classpath\`  UPPER.java

如果这不起作用,请在没有斜线的情况下尝试:

javac -cp pig-0.11.0.jar:`hadoop classpath`  UPPER.java

答案 1 :(得分:2)

包括hadoop-common-2.3.0.jar应解决该异常。