gpt4 book ai didi

hadoop - pig :在一类中包含多个UDF

转载 作者:行者123 更新时间:2023-12-02 21:25:18 27 4
gpt4 key购买 nike

我想定义多个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;
}
}

最佳答案

您可以使用相同的类,创建它的多个实例-在构造函数中时,您将声明它将执行的功能

因此,在您的 pig 中,您将定义不同的实例:

define udf1 my.package.udf.MultiFunc('1');
define udf2 my.package.udf.MultiFunc('2');

您的UDF类将如下所示:
    public class MultiFunc  extends EvalFunc<String> {

private String operation;
public MultiFunc(String operation){
this.data = operation;

}

@Override
public String exec(Tuple tuple) throws IOException {
switch (operation){
case "+":
//your code here;
break;

case "-":
//your code here;
break;

break;

}
}

关于hadoop - pig :在一类中包含多个UDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36260267/

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