Hive UDF開發

    Hive進行UDF開發十分簡單,此處所說UDF爲Temporary的function,所以需要hive版本在0.4.0以上纔可以。
    Hive的UDF開發只需要重構UDF類的evaluate函數即可。例:
package com.hrj.hive.udf;

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

public class helloUDF extends UDF {

        public String evaluate(String str) {

                try {

                        return "HelloWorld " + str;

                } catch (Exception e) {

                        return null;

                }

        }

}

將該java文件編譯成helloudf.jar
hive> add jar helloudf.jar;

hive> create temporary function helloworld as 'com.hrj.hive.udf.helloUDF';

hive> select helloworld(t.col1) from t limit 10;

hive> drop temporary function helloworld;

注:
  1. helloworld爲臨時的函數,所以每次進入hive都需要add jar以及create temporary操作
  2. UDF只能實現一進一出的操作,如果需要實現多進一出,則需要實現UDAF
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章