在 Pig 中为元组中的所有字段应用 TRIM() [英] Applying TRIM() in Pig for all fields in a tuple
问题描述
我正在加载一个包含 56 个字段的 CSV 文件.我想在 Pig 中为元组中的所有字段应用 TRIM() 函数.
I am loading a CSV file with 56 fields. I want to apply TRIM() function in Pig for all fields in the tuple.
我试过了:
B = FOREACH A GENERATE TRIM(*);
但它失败并出现以下错误-
But it fails with below error-
错误 org.apache.pig.tools.grunt.Grunt - 错误 1045:无法推断匹配org.apache.pig.builtin.TRIM 的函数为多个或没有一个合身.请使用显式转换.
ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1045: Could not infer the matching function for org.apache.pig.builtin.TRIM as multiple or none of them fit. Please use an explicit cast.
请帮忙.谢谢.
推荐答案
要在 Pig 中修剪元组,您应该创建一个 UDF.注册 UDF 并将带有 Foreach
语句的 UDF 应用于要修剪的元组的字段.下面是使用 UDF 修剪元组的代码.
To Trim a tuple in the Pig, you should create a UDF. Register the UDF and apply the UDF with Foreach
statement to the field of the tuple you want to trim. Below is the code for trimming the tuple with UDF.
public class StrTrim extends EvalFunc<String> {
public String exec(Tuple input) throws IOException {
if (input == null || input.size() == 0)
return null;
try {
String str = (String)input.get(0);
return str.trim();
}
catch(Exception e) {
throw WrappedIOException.wrap("Caught exception processing input row ", e);
}
}
}
这篇关于在 Pig 中为元组中的所有字段应用 TRIM()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!