Hive UDF implementation and HiveUDF implementation
Hive's own Query Language HQL can complete most of the functions, but in special needs, you need to write your own UDF implementation. The following is a complete case.
1. Compile UDF in eclipse
① Add all jar packages under the lib of hive in the project and share hadoop-common-2.5.1.jar under Hadoop (Hadoop is the latest version 2.5.1 ).
The UDF class must inherit the org.apache.hadoop.hive.ql.exe c. UDF class, which must implement evaluate. When we use a custom UDF in hive, hive will call the evaluate Method in the class to implement specific functions.
③ Export the project as a jar file.
Note: The jdk of the project must be consistent with the jdk of the cluster.
Example:
package com.zx.hive.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
public class UdfTestLength extends UDF{ public Integer evaluate(String s) { if(s==null) { return null; }else{ return s.length(); } }}
I typed the above class into jar form, I used eclipse to export directly as a test-udf.jar package, and then put it in the/root directory.
(Reprinted please note, for more information see: http://blog.csdn.net/hwwn2009/article/details/41289197)
2. Custom function call Process:
① Add a jar package (execute it in the hive command line)
Hive> add jar/root/test-udf.jar;
② Create a temporary function. After the hive command line is closed, it becomes invalid.
Hive> create temporary function testlength as 'com. zx. hive. udf. udftestlength ';
③ Call
Hive> select id, name, testlength (name) from student;
④ Save the query result to HDFS
Hive> create table result row format delimited fields terminated by '\ t' as select id, testlength (nation) from student;
(Reprinted please note, for more information see: http://blog.csdn.net/hwwn2009/article/details/41289197)