【发布时间】: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