Steps to Create UDF in Apache Pig

This post contains the necessary step required to  create UDF in Apache Pig.  All UDF should extend a Filter function and has to contain a method called exec, which contains a Tuple. The logic applied here is that if the Tuple is null or zero, it will give you a Boolean value: True or False. And ‘IsofAge’ is for checking if the age given is correct or not. The logic of the User Defined Function is written in Java  codes, where the JAR file will be created and then exported. The JAR file is later on registered. These JAR files are found in the library files of Apache Pig at the time of loading.

publicclassIsOfAgeextendsFilterFunc{
@Override
publicBoolean exec(Tuple tuple) throwsIOException{
if(tuple == null|| tuple.size() == 0) {
returnfalse;
}
try{
Object object= tuple.get(0);
if(object == null) {
returnfalse;
}
inti = (Integer) object;
if(i == 18 || i == 19 || i == 21 || i == 23 || i == 27) {
returntrue;
} else{
returnfalse;
}
} catch(ExecExceptione) {
thrownewIOException(e);
}
}
}

How to Call a Pig UDF?

Once a UDF is created, the following command has to be used to register the JAR file.

register myudf.jar;
X = filter A by IsOfAge(age);

 Steps to Create UDF in Pig:

There are multiple predefined functions in Apache Pig. We also have the feature to create our own function that is User Defined Function (UDF). Pig UDF is written in Java and this requires Pig Library to use the predefined classes. The Apache Pig library pig-0.8.0-cdh3u0-core.jar can be downloaded from internet.

CLICK FOR GETTING THE STEPS TO WORK ON :-

PIG -UDF [USER DEFINED FUNCTION]

Pig Programming: Create Your First Apache Pig Script

Advertisements
Categories:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s