Hive 自定义函数

hive 支持自定义UDF,UDTF,UDAF函数

以自定义UDF为例:

使用一个名为evaluate的方法

package com.hive.custom;

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

public class XiaoUDF extends UDF {

    /**
     *  值加1000
     * @param i
     * @return val
     */
    public IntWritable evaluate(final IntWritable i) {
     int val= i.get();
     val+=1000;
        return new IntWritable(val);
      }
}

将写好的代码打为jar包,上传到服务器,或者hdfs

add jar /root/udfxiao.jar;
//add jar you.jar

注册函数

 注册一个临时函数

create temporary function fei  as 'com.hive.custom.XiaoUDF';
//fei:注册的函数名
//com.hive.custom.XiaoUDF 注册函数的全类名

使用函数

select fei(id) from test;

注册永久函数

create  function testdb.peng  as 'com.hive.custom.XiaoUDF';
//testdb 注册永久函数的数据库

从HDFS上注册函数

CREATE FUNCTION fei AS 'com.hive.custom.XiaoUDF' USING JAR 'hdfs:///udfxiao.jar';
// fei 注册的函数名
//com.hive.custom.XiaoUDF  函数的全内名
//hdfs:///udfxiao.jar   hdfs上根目录下的jar

删除函数

drop temporary function if exists fei;

 

原文地址:https://www.cnblogs.com/jottings/p/8387352.html