Apache Pig : Writing a User Defined Function (UDF)

Shrikant Bang's Notes

Shrikant Bang's Notes

In this post we will write a basic/demo custom function for Apache Pig, called as UDF (User Defined Function).
Pig’s Java UDF extends functionalities of EvalFunc. This abstract class have an abstract method “exec” which user needs to implement in concrete class with appropriate functionality.

Problem Statement:

Lets write a simple Java UDF which takes input as Tuple of two DataBag and check whether second databag(set) is subset of first databag(set).
For example, Assume you have been given tuple of two databags. Each DataBag contains elements(tuples) as number.

Input:
Databag1 :
Databag2 :
Output:
True

Then function should return true as Databag2 is subset of Databag1.

From implemetation point of view

As we are extending abstract class EvalFucn, we will be implementing exec function. In this function we’ll write logic to find is given set is subset of other or not. We will also override function outputSchema to specify output schema ( boolean : true or false ).

import java.io.IOException; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import org.apache.pig.EvalFunc; import org.apache.pig.data.DataBag; import org.apache.pig.data.DataType; import org.apache.pig.data.Tuple; import org.apache.pig.impl.logicalLayer.schema.Schema; import org.apache.pig.impl.logicalLayer.schema.Schema.FieldSchema; /** * Find the whether given SetB is subset of SetA. * 

* input: *
setA : *
setB : * * output: *
true
*

* @author https://shrikantbang.wordpress.com * */ public class IsSubSet extends EvalFunc < @Override public Schema outputSchema(Schema input) < if(input.size()!=2)< throw new IllegalArgumentException("input should contains two elements!"); >List fields = input.getFields(); for(FieldSchema f : fields) < if(f.type != DataType.BAG)< throw new IllegalArgumentException("input fields should be bag!"); >> return new Schema(new FieldSchema("isSubset",DataType.BOOLEAN)); > private Set populateSet(DataBag dataBag) < HashSet set = new HashSet(); Iterator iter = dataBag.iterator(); while(iter.hasNext())< set.add(iter.next()); >return set; > @Override public Boolean exec(Tuple input) throws IOException < Set setA = populateSet((DataBag) input.get(0)); Set setB = populateSet((DataBag) input.get(1)); return setA.containsAll(setB) ? Boolean.TRUE : Boolean.FALSE; >>

A Quick Test

Lets test our UDF to find whether given set is subset of other set or not.

-- Register jar which contains UDF. register '/home/hadoop/udf.jar'; -- Define function for use. define isSubset IsSubSet(); -- lets assume we have dataset as following : dump datset; --(,) --(,) --(,) -- lets check subset function result = foreach dataset generate $0,$1,isSubset($0,$1); dump result; --(,,true) --(,,false) --(,,false)