How To Write Hive UDF Example In Java

Our HadoopTpoint App is now available in google play store,please rate and comment it in play store : W3Schools

How To Write Hive UDF Example In Java

What is Hive UDF ?

Generally Hive having some Built-in functions,we can use that Built-in functions for our Hive program with out adding any extra code but some times 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 Hive UDF Example In Java.

Steps to create Hive UDF

Step 1 :-

Open your Eclipse and Create a java Class Name like replace.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 Hive Lib folder Jars files and Add other Jars files In Hadoop folder —–> Click Ok.

Also Read Accenture hadoop interview questions and answers for experienced

How To Write Hive UDF Example In Java

Step 3 :-

Now your Hive java program is supported in your eclipse with out any errors.The basic step in Hive UDF is

public class classname extends UDF and you return the value.

Step 4 :-

we should create an object with Text value like private Text result = new Text();

Step 5 :-

evaluate This method is called once for every row of data being processed.we can assign the value as String str.,that means str having the every row value of hive table.

Step 6 :-

After Initialize evaluate function now you can place the same java working code after  evaluate function

Step 7 :-

finally should be mention the return value,that return value is the output of UDF.

 How to Execute this code In Hive UDF ?

Step 1 :-

Right click on program —> Export —> create Jar

Step 2 :-

Open Hive Terminal—-> add jar to your current Hive location path

Add jar jarname;

Step 3 :-

Create a temporary function In hive terminal

CREATE TEMPORARY FUNCTION replaceword as ‘dateudf1.ReplaceNum1′;

dateudf1 represents the package name of program and ReplaceNum1 represents the program name and both are should be case sensitive.

Step 4 :-

create a Database and load related matching data into Hive table.

create table names(fname string,lname string) row format delimited fields terminated by '\t' stored as textfile

 

Input Data

mahesh chimmiri
rajesh chimmiri

Load the input file from local to table

load data local inpath ‘names.txt’ into table names;

Step 5 :-

select replaceword(fname,”rajesh”,”suresh”) from names;

fname represents the table column name rajesh is original data in table and suresh is replacing word  names represents the table name.

Comments

  1. well explained, thanks

  2. M Sandeep says:

    Thank you so much sir!!!!

  3. chirag Khetani says:

    Nice

  4. Thank you. Very well explained.

  5. fine examples for learn hive UDF

  6. Open Hive Terminal—-> add jar to your current Hive location path

    I have my .jar in local on my pc, where it has to be located to being added after?

Speak Your Mind

*