Hive中自定義函數的實現

1、Hive自定義函數的實現細節

1).自定義UDF需要繼承:org.apache.hadoop.hive.ql.UDF
2).需要evaluate函數,evaluate函數支持重載。

2、Hive自定義函數的部署運行

1).把程序打包放到目標機器上去
2).進入hive客戶端,添加jar包:
hive> add jar /home/sfd/udf_test.jar
3).創建臨時函數:
hive> create temporary function <函數名>
    > as 'java全類名';
4).銷燬臨時函數:
hive> drop temporary function <函數名>;

3、Hive自定義函數的使用

hive> select <函數名> from table;

實例:實現函數content(’hello‘,’world‘)結果爲:hellow*****world。

1.函數代碼很簡單:

package com.sfd.UDF;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

public class ConcatString extends UDF{

    public Text evaluate(Text a,Text b){
        return new Text(a.toString()+"*****"+b.toString());
    }   
}

2.打成jar包(我起的名字爲UTFC.jar);並添加到hive中去;

hive> add jar /home/sfd/UDFC.jar; 

3.定義臨時函數content:

hive> create temporary function content as 'com.sfd.UDF.ConcatString';

4.使用:

hive> select content('hello','world');

結果爲:
hello*****world

發佈了56 篇原創文章 · 獲贊 31 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章