【发布时间】:2016-03-28 10:16:57
【问题描述】:
我想定义多个 Pig UDF。他们每个人都将提取数据的不同部分。在我的例子中,数据是具有复杂结构的 JSON 文档,包括许多嵌套的 JSON 对象。
问题是现在我已经为我需要的每个函数创建了一个不同的 Eval 类。这些类中的每一个都实现了 exec()。 有没有办法将所有函数放到同一个 UDF 类中并从 pig 调用它们?
我的一个 UDF 示例:
public class PigGetTimestamps extends EvalFunc<Tuple>{
public Tuple exec(org.apache.pig.data.Tuple input) throws IOException {
if (input == null || input.size() == 0 ){
return null;
}
try {
String inputString = DataType.toString(input.get(0));
try
{
String[] tokens=inputString.split("\t");
if (tokens.length<1)
return null;
Document document=new Document(tokens[0], true, false);
long timestamp_fetch=document.getTimestamp_fetch();
long timestamp_pub=document.getTimestampPub();
Tuple output = TupleFactory.getInstance().newTuple(2);
output.set(0,timestamp_pub);
output.set(1,timestamp_fetch);
return output;
}
catch(Exception e)
{
return null;
}
} catch (Exception e) {
System.out.println("Can't extract field; error = " + e.getMessage());
return null;
}
}
【问题讨论】:
标签: hadoop apache-pig udf