【问题标题】:Applying TRIM() in Pig for all fields in a tuple在 Pig 中对元组中的所有字段应用 TRIM()
【发布时间】:2015-04-02 13:02:50
【问题描述】:

我正在加载一个包含 56 个字段的 CSV 文件。我想在 Pig 中为元组中的所有字段应用 TRIM() 函数。

我试过了:

B = FOREACH A GENERATE TRIM(*);

但它失败并出现以下错误-

错误 org.apache.pig.tools.grunt.Grunt - 错误 1045:无法推断匹配 org.apache.pig.builtin.TRIM 的函数为多个或没有 合身。请使用显式转换。

请帮忙。谢谢。

【问题讨论】:

  • 你不能这样使用TRIM函数。你能粘贴一些示例输入吗?

标签: apache-pig


【解决方案1】:

要在 Pig 中修剪元组,您应该创建一个 UDF。注册 UDF 并将带有 Foreach 语句的 UDF 应用于要修剪的元组的字段。下面是使用 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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多