Pig UDF User Custom function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.