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
1条答案
按热度按时间jdg4fx2g1#
零的数量需要根据剩余字符串的长度动态生成,所以我认为在本机pig中不可能。
这在自定义项中是非常可能的。
输入文件
Pig手稿:
输出:
udf代码:下面的java类文件编译并生成为leftformat.jar
左键盘.java
更新: