gpt4 book ai didi

apache-pig - 创建 pig udf 架构时遇到问题

转载 作者:行者123 更新时间:2023-12-05 01:04:27 24 4
gpt4 key购买 nike

试图解析 xml,但我的 UDF 返回元组时遇到问题。遵循 http://verboselogging.com/2010/03/31/writing-user-defined-functions-for-pig 中的示例

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;
}
}

最佳答案

您需要添加您的 s模式实例变量到另一个 Schema 对象。

尝试返回 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
;

并告诉我错误?

关于apache-pig - 创建 pig udf 架构时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23144209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com