Hive自定义函数

一、Hive自定义函数

① UDF(User-Defined-Function) 一进一出
② UDAF(User- Defined Aggregation Funcation) 聚和函数,多进一出,如count/max/min
③ UDTF(User-Defined Table-Generating Functions) 一进多出,如lateral view explode()

使用方式 :在HIVE会话中add 自定义函数的jar文件,然后创建function继而使用函数
① 打包上传:把程序打成jar包后上传到服务器
② 添加jar包:进入hive客户端,添加jar包:
hive> add jar /path/to/the/hive_udf.jar
③ 创建临时函数:选择某个库,创建临时函数,该函数会与库名绑定,如果是切换到其他库中需要使用,得加上“库名.”:
hive> create temporary function getLen as ‘com.raphael.len.GetLength’;
其中:
getLen是自己定义的函数名,可随意
com.raphael.len.GetLength是程序的全类名
④ 查询测试:查询HQL语句:
hive> select getLen(info) from apachelog;
⑤ 销毁临时函数:选择之前绑定的库,即use db_name,销毁临时函数:
hive> DROP TEMPORARY FUNCTION getLen;

二、自定义UDF函数
参考:https://www.cnblogs.com/raphael5200/p/5215337.html

编写UDF函数:
a)自定义UDF需要先extends继承GenericUDF类。
b)初始化,实现evaluate函数,evaluate函数支持重载。

pox.xml文件
导入依赖org.apache.hive.hive-exec

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>UDF</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>3.1.2</version>
        </dependency>
    </dependencies>

</project>
package com.fromguigu.hive;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

/*
    自定义UDF函数:
    1.自定义一个类并继承GenericUDF类
    2.重写方法
 */
public class MyLen extends GenericUDF {
    /**
     * 初始化:用来检查参数的个数和类型等操作
     * @param arguments
     * @return 函数的返回值的类型
     * @throws UDFArgumentException
     */
    public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
        //判断参数的个数
        if (arguments.length != 1){
            throw new UDFArgumentLengthException("arguments number is error!!!");
        }
        //判断参数的类型是否是Hive支持的基本类型
        if (!arguments[0].getCategory().equals(ObjectInspector.Category.PRIMITIVE)){
            /**
             * UDFArgumentTypeException(int argumentId, String message) {
             * 第一个参数 :第几个实参
             * 第二个参数:异常的信息
             */
            throw new UDFArgumentTypeException(0,"arguments type is error!!!");
        }
        return PrimitiveObjectInspectorFactory.javaIntObjectInspector;
    }

    /**
     * 具体函数的功能的体现
     * @param arguments
     * @return 函数的返回值
     * @throws HiveException
     */
    public Object evaluate(DeferredObject[] arguments) throws HiveException {
        //获取参数
        Object obj = arguments[0].get();
        int len = obj.toString().length();
        return len;
    }

    /**
     * 返回显示字符串方法,这个方法不用管,直接返回一个空字符串
     * @param children
     * @return
     */
    public String getDisplayString(String[] children) {
        return "";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值