Register UDF
The contents of Do.pig are as follows:
Register/xx/yy.jardata = Load ' data '; result = foreach data generate Aa.bb.Upper ($);d UMP result;
The path of the register can be either a local path or an HDFS path
Register Hdfs://pig/xx/yy.jar
If it's pig-d,
udf.import.list=AA.BB, you can not use the package path when referencing UDFs:
Register/xx/yy.jardata = Load ' data '; result = foreach data generate Upper ($);d UMP result;
If it's pig-d,
Pig.additional.jars=/xx/yy.jar, can not register:
data = Load ' data '; result = foreach data generate Aa.bb.Upper ($);d UMP result;
You can use define to alias a UDF:
Register/xx/yy.jardefine UPPER aa.bb. Upper ();d ata = load ' data '; result = foreach data generate Upper ($);d UMP result;
If you need parameters to construct a UDF, you can pass in define, or you can define multiple overloaded constructors
Register/xx/yy.jardefine UPPER1 aa.bb. Upper ();d efine UPPER2 aa.bb. Upper (' abc ');d ata = load ' data '; result = foreach data generate UPPER1 ($), UPPER2 ($);d UMP result;
calling static Java functionsThe callable function must meet the criteria: 1) the static function 2) The return value of the basic data type, STIRNG, Array3) is the base data type, string multiple parameters are separated by a space invokeforint, Invokeforlong, Invokeforfloat, Invokefordouble, invokeforstring
Define Hex invokeforstring (' java.lang.Integer.toHexString ', ' int ');d ata = load ' data '; result = foreach data generate hex ( (int) $ A);
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);
Custom UDFEval function (operator)
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> {@Overridepublic 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.jardefine 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;
Aggregate function (aggregate function) filter function (filter functions) load function (load functions) store function (stored functions)
Pig UDF User Custom function