Applying TRIM() in Pig for all fields in a tuple

孤街醉人 提交于 2019-12-12 01:55:08

问题


I am loading a CSV file with 56 fields. I want to apply TRIM() function in Pig for all fields in the tuple.

I tried:

B = FOREACH A GENERATE TRIM(*);

But it fails with below error-

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.

Please help. Thank you.


回答1:


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);
        }
    }
}


来源:https://stackoverflow.com/questions/29413674/applying-trim-in-pig-for-all-fields-in-a-tuple

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!