Apache pig 在java中将自定义参数传递给pig udf函数
这就是我处理数据的方式。。来自猪Apache pig 在java中将自定义参数传递给pig udf函数,apache-pig,Apache Pig,这就是我处理数据的方式。。来自猪 A = Load 'data' ... B = FOREACH A GENERATE my.udfs.extract(*); or B = FOREACH A GENERATE my.udfs.extract('flag'); 所以基本上extract要么没有参数,要么接受参数…'旗帜' 在我的udf方面 @Override public DataBag exec(Tuple input) throws IOException {
A = Load 'data' ...
B = FOREACH A GENERATE my.udfs.extract(*);
or
B = FOREACH A GENERATE my.udfs.extract('flag');
所以基本上extract要么没有参数,要么接受参数…'旗帜'
在我的udf方面
@Override
public DataBag exec(Tuple input) throws IOException {
//if flag == true
//do this
//else
// do that
}
现在我如何在pig中实现这一点?首选的方法是使用 、在以下情况下,使用“定义”指定自定义项函数:
…
的构造函数 函数接受字符串参数。如果需要使用不同的 构造函数参数,用于将要调用的函数的不同调用 需要创建多个定义–每个参数集一个“ 例如: 鉴于以下UDF:
public class Extract extends EvalFunc<String> {
private boolean flag;
public Extract(String flag) {
//Note that a boolean param cannot be passed from script/grunt
//therefore pass it as a string
this.flag = Boolean.valueOf(flag);
}
public Extract() {
}
public String exec(Tuple input) throws IOException {
if (input == null || input.size() == 0) {
return null;
}
try {
if (flag) {
...
}
else {
...
}
}
catch (Exception e) {
throw new IOException("Caught exception processing input row ", e);
}
}
}
另一种选择(黑客?):
在这种情况下,UDF将使用其noarg构造函数实例化,您将在其exec方法中传递要计算的标志。由于此方法将元组作为参数,因此您需要首先检查第一个字段是否为布尔标志
public class Extract extends EvalFunc<String> {
public String exec(Tuple input) throws IOException {
if (input == null || input.size() == 0) {
return null;
}
try {
boolean flag = false;
if (input.getType(0) == DataType.BOOLEAN) {
flag = (Boolean) input.get(0);
}
//process rest of the fields in the tuple
if (flag) {
...
}
else {
...
}
}
catch (Exception e) {
throw new IOException("Caught exception processing input row ", e);
}
}
}
我宁愿坚持第一个解决方案,因为这很难闻。罗兰:你能看看我的问题吗,这是一个类似的问题。谢谢
public class Extract extends EvalFunc<String> {
public String exec(Tuple input) throws IOException {
if (input == null || input.size() == 0) {
return null;
}
try {
boolean flag = false;
if (input.getType(0) == DataType.BOOLEAN) {
flag = (Boolean) input.get(0);
}
//process rest of the fields in the tuple
if (flag) {
...
}
else {
...
}
}
catch (Exception e) {
throw new IOException("Caught exception processing input row ", e);
}
}
}
...
B = foreach A generate Extract2(true,*); --use flag
C = foreach A generate Extract2();