在Pig中将TRIM()应用到元组中的所有字段 [英] Applying TRIM() in Pig for all fields in a tuple

查看:78
本文介绍了在Pig中将TRIM()应用到元组中的所有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载包含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并将UDF带Foreach语句应用于要修剪的元组的字段.下面是使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆