标签:
注册UDF
register /xx/yy.jar data = load ‘data‘; result = foreach data generate aa.bb.Upper($0); dump result;
register hdfs://pig/xx/yy.jar
register /xx/yy.jar data = load ‘data‘; result = foreach data generate Upper($0); dump result;
data = load ‘data‘; result = foreach data generate aa.bb.Upper($0); dump result;
register /xx/yy.jar define UPPER aa.bb. Upper(); data = load ‘data‘; result = foreach data generate UPPER($0); dump result;
register /xx/yy.jar define UPPER1 aa.bb. Upper(); define UPPER2 aa.bb. Upper(‘abc‘); data = load ‘data‘; result = foreach data generate UPPER1($0), UPPER2($1); dump result;
define hex InvokeForString(‘java.lang.Integer.toHexString‘, ‘int‘); data = load ‘data‘; result = foreach data generate hex((int)$0);
define stdev InvokeForDouble(‘com.acme.stats.stdev‘, ‘double[]‘); a = load ‘data‘ as (id:int, dp:double); b = group a by id; c = foreach b generate stdev(a.dp);
package com.test.pig.udf; import java.io.IOException; import org.apache.pig.EvalFunc; import org.apache.pig.data.Tuple; public class CustomReplaceFunc extends EvalFunc<String> { @Override public String exec(Tuple tuple) throws IOException { if(tuple == null || tuple.size() == 0 || tuple.isNull(0) ) { return null; } String original = (String)tuple.get(0); String oldChar = (String)tuple.get(1); String newChar = (String)tuple.get(2); return original.replaceAll(oldChar, newChar); } }
register /home/pig/myfunc.jar define MyReplace com.test.pig.udf.CustomReplaceFunc(); users = load ‘/users.data‘ as (name:chararray, age, address); result = foreach users generate MyReplace(name, ‘l‘, ‘L‘), age, address; dump result;
标签:
原文地址:http://www.cnblogs.com/lishouguang/p/4559619.html