Hive UDF in 1 minute!

Hive UDF in 1 minute!

Apache Hive comes with a lot of built-in UDFs, but what happens when you need a “special one”? This post is about how to get started with a custom Hive UDF from compilation to execution in no time.

 

 

Let’s go!

Our goal is to create a UDF that transforms its input to upper case. All the code is available in our public repository of Hadoop examples and tutorials.

If you want to go even faster, the UDF is already precompiled here.

If not, checkout the code:

git clone https://github.com/romainr/hadoop-tutorials-examples.git
cd hive-udf

And compile the UDF (Java and Hive need to be installed):

javac -cp $(ls /usr/lib/hive/lib/hive-exec*.jar):/usr/lib/hadoop/hadoop-common.jar org/hue/udf/MyUpper.java

jar -cf myudfs.jar  -C . .

 

Or use Maven with our pom.xml that will automatically pull the dependent jars

mvn install

 

Register the UDF in the Hive Editor

Then open up Beeswax in the Hadoop UI Hue, click on the ‘Settings’ tab.

In File Resources, upload myudfs.jar, pick the jar file and point to it, e.g.:

/user/hue/myudf.jar

Make the UDF available by registering a UDF (User Defined Function ):

Name

myUpper

Class

org.hue.udf.MyUpper

 

That’s it! Just test it on one of the Hue example tables:

select myUpper(description) FROM sample_07 limit 10

Summary

We are using the most common type of UDF. If you want to learn more in depth about the other ones, some great resources like the Hadoop Definitive guide are available. Notice that adding a jar loads it for the entirety of the session so you don’t need to load it again. Next time we will demo how to create a Python UDF for Hive!

 

Have any questions? Feel free to contact us on hue-user or @gethue!

 

Note:

If you did not register the UDF as explained above, you will get this error:

error while compiling statement: failed: parseexception line 1:0 cannot recognize input near 'myupper' '' ''

11 Comments

  1. khaled 3 years ago

    There is no ok pr submit button after inserting these parameters and function still unregistered, using hue 3.7.0

  2. Danny 2 years ago

    Where is the Python UDF tutorial?

    • Hue Team 2 years ago

      Not here, sorry 🙂 But I’m sure there are some around, have you tried to look it up?

  3. pratice 1 year ago

    Where is the Python UDF tutorial?

  4. Patorn 12 months ago

    When I run the query, I got “Insufficient privileges to execute ADD”? How can I fix this issue? Is it related to Sentry? My system is on CDH-5.8

Leave a reply

Your email address will not be published. Required fields are marked *

*