TECH SOLUTIONS

Click here to edit subtitle

Forums

Post Reply
Forum Home > Bigdata Learnings( Hadoop, HBase,Hive and other bigdata technologies) > Hive : Custom User Defined Function (UDF)

Sourav Gulati
Site Owner
Posts: 83

Hive provides many inbuilt functions. However, you can also create your custom functions and use it like inbuilt functions. Following is an example custom function that converts the string to uppercase


package com.test.hive;


import org.apache.hadoop.hive.ql.exec.Description;

import org.apache.hadoop.hive.ql.exec.UDF;


@Description(name = "uppercase",

value = "uppercase(string) - converts string to uppercase ",

extended = "Example:\n"

+ " > SELECT uppercase(string) FROM src;\n")


public class UpperCase extends UDF{

    

    public String evaluate(String input)

    {

        String value=input;

        value=value.toUpperCase();

        return value;

    }

    }


Here, you need to extend "org.apache.hadoop.hive.ql.exec.UDF" class and override "evaluate" function.

The @Description(...) is an optional Java annotation. This is how function documentation is defined and you should use these annotations to document your own UDFs.

When a user invokes DESCRIBE FUNCTION ..., this desription gets displayed


Create jar file of the project and add it in hive as follows:

hive>add jar /pathto/jar;

Then you create your custom function as follows:

hive>create temporary function uppercase as 'com.test.hive.UpperCase';

Now , your function is available to use

hive> select uppercase("test function") from dummy;

TEST FUNCTION

hive>

Also, you create an hql file with all these commands and run it from prompt as follows:

$cat check.hql
add jar /pathto/jar;
create temporary function uppercase as 'com.test.hive.UpperCase';
select uppercase("test") from dummy;

$ hive -f /pathto/check.hql





Click here for Other topics of BigData Technologies

 

February 1, 2013 at 7:06 AM Flag Quote & Reply

You must login to post.