Hive UDF implementation and HiveUDF implementation

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.