创建猪udf架构时出现问题

时间:2014-04-17 22:01:03

标签: apache-pig

尝试解析xml,我的UDF返回元组时出现问题。按照http://verboselogging.com/2010/03/31/writing-user-defined-functions-for-pig

中的示例进行操作

猪脚本

titles = FOREACH programs GENERATE (px.pig.udf.PARSE_KEYWORDS(program))
    AS (root_id:chararray, keyword:chararray);

这是输出模式代码:

 override def outputSchema(input: Schema): Schema = {
    try {
      val s: Schema = new Schema
      s.add(new Schema.FieldSchema("root_id", DataType.CHARARRAY))
      s.add(new Schema.FieldSchema("keyword", DataType.CHARARRAY))
      return s
    }
    catch {
      case e: Exception => {
        return null
      }
    }
  }

我收到此错误

pig script failed to validate: org.apache.pig.impl.logicalLayer.FrontendException: 
ERROR 0: Given UDF returns an improper Schema. 
Schema should only contain one field of a Tuple, Bag, or a single type. 
Returns: {root_id: chararray,keyword: chararray}

更新最终解决方案:

在java中

public Schema outputSchema(Schema input) {
    try {
        Schema tupleSchema = new Schema();
        tupleSchema.add(input.getField(1));
        tupleSchema.add(input.getField(0));
        return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(),  input),tupleSchema, DataType.TUPLE));
    } catch (Exception e) {
        return null;
    }
}

1 个答案:

答案 0 :(得分:5)

您需要将s架构实例变量添加到另一个架构对象。

尝试在下面的模板中返回new Schema(new FieldSchema(..., input), s, DataType.TUPLE));

以下是我在Java中的答案(填写变量名称):

@Override
    public Schema outputSchema(Schema input) {
        Schema tupleSchema = new Schema();
        try {

            tupleSchema.add(new FieldSchema("root_id", DataType.CHARARRAY));
            tupleSchema.add(new FieldSchema("keyword", DataType.CHARARRAY));

            return new Schema(new FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), tupleSchema, DataType.TUPLE));
        } catch (FrontendException e) {
            e.printStackTrace();
            return null;
        }
    }

你会尝试:

titles = FOREACH programs GENERATE (px.pig.udf.PARSE_KEYWORDS(program));

如果没有错误,请尝试:

titles = FOREACH TITLES GENERATE
    $0 AS root_id
    ,$1 AS keyword
;

告诉我错误?