| Forum Home > Bigdata Learnings( Hadoop, HBase,Hive and other bigdata technologies) > Hive : Custom User Defined Function (UDF) | ||
|---|---|---|
|
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 $ hive -f /pathto/check.hql
| |
| ||