【问题标题】:trouble creating a pig udf schema创建 pig udf 架构时遇到问题
【发布时间】:2014-04-17 22:01:02
【问题描述】:

尝试解析 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;
    }
}

【问题讨论】:

  • 您能提供program 别名的示例输出吗?
  • 它是 xml,例如"foo"
  • 你能检查我答案底部的更新吗

标签: apache-pig


【解决方案1】:

您需要将 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
;

然后告诉我错误?

【讨论】:

  • 现在我收到错误 ERROR 1031: Incompatable schema: left is "root_id:chararray,name:chararray", right is "px.pig.udf.parse_keywords_program_1:tuple(root_id:chararray,keyword:字符数组)"
  • 按照您的建议从 pig 语句中删除架构会返回一个不同的错误:org.apache.pig.impl.logicalLayer.FrontendException: ERROR 1066: Unable to open iterator for alias Title
  • 好的,通过将模式语句更改为 tupleSchema.add(input.getField(0));而不是 new FieldSchema... 并在 pig 脚本中取出模式声明
  • 你能用一个完整的例子来更新你的代码吗?我无法让它工作。我得到“左边是......右边是”错误
猜你喜欢
  • 1970-01-01
  • 2016-12-04
  • 1970-01-01
  • 2013-05-31
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多