Last week I needed to do a quick compile of a sample Hive UDF for testing purpose. I didn’t want to install all Maven stuff and create a pom.xml file just for this simple task. Thanks to my colleague YiBing, who is an expert in Hive, I got it working in just a few commands in the shell.
[root@localhost hive-udf]$ export JAVA_HOME=/usr/java/jdk1.7.0_67-cloudera/
[root@localhost hive-udf]$ export PATH=$PATH:$JAVA_HOME/bin
[root@localhost hive-udf]$ javac -cp `hadoop classpath`:"/opt/cloudera/parcels/CDH/lib/hive/lib/*" GenericUDFCurrentUser.java

[root@localhost hive-udf]$ ls
GenericUDFCurrentUser.class GenericUDFCurrentUser.java

[root@localhost hive-udf]$ mkdir -p com/elin
[root@localhost hive-udf]$ mv GenericUDFCurrentUser.class com/elin
[root@localhost hive-udf]$ jar cvf elin-udf.jar com
added manifest
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/elin/(in = 0) (out= 0)(stored 0%)
adding: com/elin/GenericUDFCurrentUser.class(in = 2505) (out= 1145)(deflated 54%)
Then I could access the UDF in Hive
ADD JAR /user/ericlin/hive-udf/elin-udf.jar;
CREATE TEMPORARY FUNCTION get_user AS com.elin.GenericUDFCurrentUser';
SELECT get_user() from test;
It is so simple without the need of any third party thingy.

    Leave a Reply

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