Learn Geek languages like Big data,Hadoop,Hive,Pig,Sqoop ,flume,Cassandra,Hbase,Ruby On Rails,Python,Java and many more.

Showing posts with label big data. Show all posts
Showing posts with label big data. Show all posts

Thursday 30 November 2017


                                how to write Pig UDF example in JAVA

What is Pig UDF?
Pig having some Built-in functions, we can use that Built-in functions for our Pig Script without adding any extra code but sometimes user requirement is not available in that built-in functions at that time user can write some own custom user-defined functions called UDF (user defined function).Here is the simple steps of How To Write Pig UDF Example In Java.

Steps to create Pig UDF
Step 1:-
Open your Eclipse and Create a Java Class Name like first.java
Step 2:-
You should add jar files to that Project folder like
Right Click on project —> Build Path —> Configure Build Path —> Libraries —> Add External Jars —> Select Hadoop and Pig Lib folder Jars files and Add other Jars files In Hadoop folder —–> Click Ok.
Step 3:-
Now your Pig java program is supported in your eclipse without any errors.The basic step in Pig UDF is
public class first extends EvalFunc<Class DataType> and you return the value.
----------------------------------------CODE:----------------------------------------------
package mypro;

import java.io.IOException;
import org.apache.pig.EvalFunc;
import org.apache.pig.data.Tuple;
import org.apache.pig.impl.util.WrappedIOException;

public class Ucfirst extends EvalFunc<String> {
    public String exec(Tuple input) throws IOException {
        if (input.size() == 0)
            return null;
        try {
            String str = (String) input.get(0);
            char ch = str.toUpperCase().charAt(0);
            String str1 = String.valueOf(ch);
            return str1;

        } catch (Exception e) {
        
            throw WrappedIOException.wrap(
                    "Caught exception processing input row ", e);
        }
    }
}
-----------------------------------------------------------------------------------------------------------
Step 4:-
public String exec(Tuple input) throws IOException {
if (input.size() == 0)
return null;
Class Name String and The entire row in the text file is considered as Tuple and first of all, it will check the input is zero or not if the input is zero then it returns null.
Step 5 :-
Try Catch Block,we have to write the logic in Try Block
try {
String str = (String) input.get(0);
char ch = str.toUpperCase().charAt(0);
String str1 = String.valueOf(ch);
return str1;
Step 6 :-
Catch Block only for exception Handling
How to Execute this code In Pig UDF ?
Step 1 :-
Right click on program —> Export —> create Jar
Step 2 :-
Register Jarname;
Step 3 :-
Write The Pig Script as first.pig
REGISTER first.jar;
A = LOAD ‘sample.txt’ as (logid:chararray);
B = FOREACH A GENERATE mypro.first(logid);
DUMP B;
In the above Script mypro is Package name and first is class name
pig -x local first.pig
Output
(W)
(C)
(X)
(U)