在猪中左填充一个字符串 [英] Left padding a string in pig

查看:17
本文介绍了在猪中左填充一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 0-s 左填充字符串数据类型字段.有没有办法做到这一点?我需要固定长度 (40) 值.

I would like to left pad a string data type field with 0-s. Is there any way to do that? I need to have fixed length (40) values.

提前致谢,千里眼

推荐答案

零的个数需要根据剩余字符串的长度动态生成,所以我认为在原生猪中是不可能的.
这在 UDF 中很有可能.

The number of zeros needs to be generate dynamically based on the length of the remaining string, so i don't think its possible in native pig.
This is very much possible in UDF.

input.txt

11111
222222222
33
org.apache.hadoop.util.NativeCodeLoader
apachepig

PigScript:

REGISTER leftformat.jar;

A = LOAD 'input.txt' USING PigStorage() AS(f1:chararray);
B = FOREACH A GENERATE format.LEFTPAD(f1);
DUMP B;

输出:

(0000000000000000000000000000000000011111)
(0000000000000000000000000000000222222222)
(0000000000000000000000000000000000000033)
(0org.apache.hadoop.util.NativeCodeLoader)
(0000000000000000000000000000000apachepig)

UDF代码:下面的java类文件被编译并生成为leftformat.jar
LEFTPAD.java

UDF code: The below java class file is compiled and generated as leftformat.jar
LEFTPAD.java

package format;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.apache.pig.EvalFunc;
import org.apache.pig.data.Tuple;

public class LEFTPAD extends EvalFunc<String> {
@Override
public String exec(Tuple arg) throws IOException {
       try
        {
            String input = (String)arg.get(0);
            return StringUtils.leftPad(input, 40, "0");
        }
        catch(Exception e)
        {
            throw new IOException("Caught exception while processing the input row ", e);
        }
    }
}

更新:

1.Download 4 jar files from the below link(apache-commons-lang.jar,piggybank.jar, pig-0.11.0.jar and hadoop-common-2.6.0-cdh5.4.5)
http://www.java2s.com/Code/Jar/a/Downloadapachecommonslangjar.htm
http://www.java2s.com/Code/Jar/p/Downloadpiggybankjar.htm
http://www.java2s.com/Code/Jar/p/Downloadpig0110jar.htm

2. Set all the 3 jar files to your class path
  >> export CLASSPATH=/tmp/pig-0.11.1.jar:/tmp/piggybank.jar:/tmp/apache-commons-lang.jar

3. Create directory name format 
    >>mkdir format

4. Compile your LEFTPAD.java and make sure all the three jars are included in the class path otherwise compilation issue will come
    >>javac LEFTPAD.java

5. Move the class file to format folder
    >>mv  LEFTPAD.class format

6. Create jar file name leftformat.jar
    >>jar -cf leftformat.jar format/

7. jar file will be created, include into your pig script

Example from command line:
$ mkdir format
$ javac LEFTPAD.java 
$ mv LEFTPAD.class format/
$ jar -cf leftformat.jar format/
$ ls
LEFTPAD.java    format      input.txt   leftformat.jar  script.pig

这篇关于在猪中左填充一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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