zoukankan      html  css  js  c++  java
  • Hive(9)-自定义函数

    一. 自定义函数分类

    当Hive提供的内置函数无法满足你的业务处理需要时,此时就可以考虑使用用户自定义函数.

    根据用户自定义函数类别分为以下三种:

    1. UDF(User-Defined-Function) 一进一出

    2. UDAF(User-Defined Aggregation Function) 聚集函数,多进一出, 类似于:count/max/min

    3. UDTF(User-Defined Table-Generating Functions) 一进多出 如lateral view explore()

    二.自定义步骤

    官方文档地址: https://cwiki.apache.org/confluence/display/Hive/HivePlugins

    1. 继承org.apache.hadoop.hive.ql.UDF

    2. 需要实现evaluate函数

    evaluate函数支持重载;

    3. 在hive的命令行窗口创建函数

      1). 添加jar:  add jar linux_jar_path

      2). 创建function:  create [temporary] function [dbname.]function_name AS class_name;

    删除: Drop [temporary] function [if exists] [dbname.]function_name;

    tips: UDF必须要有返回类型,可以返回null,但是返回类型不能为void;

    三. 自定义函数实例

    1. 创建Maven工程

    2.导入依赖

    <dependencies>
            <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
            <dependency>
                <groupId>org.apache.hive</groupId>
                <artifactId>hive-exec</artifactId>
                <version>1.2.1</version>
            </dependency>
    </dependencies>

    3. 创建自定义类

    package com.nty.udf;
    
    import org.apache.hadoop.hive.ql.exec.UDF;
    
    /**
     * author nty
     * date time 2018-12-21 19:45
     */
    public class Lower extends UDF {
    
        //返回str的小写  
        public String evaluate(String str) {
            if( null == str){
                return  null;
            }
            return str.toLowerCase();
        }
    
    }

    4. 使用maven打成jar包,并上传至/opt/module/jars目录下

    5. 将jar包添加到hive的classpath

    add jar /opt/module/datas/udf.jar;

    6. 创建[临时]函数与开发好的java class关联

    create temporary function ntylower as "com.nty.udf.Lower";

    7. 使用

    select ename, ntylower(ename) lowername from emp;
  • 相关阅读:
    命名mangling(压榨)
    Redis的源代码分析
    开源数据库 Sharding 技术 (Share Nothing)
    Python中time模块详解
    关于海量数据的数据模型
    字符数组,字符指针,Sizeof总结
    ConfigParser模块的使用
    分享懒人张RDLC报表(七、八)
    VS中提示:未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值。
    VS用正则表达式统计代码行数
  • 原文地址:https://www.cnblogs.com/duoduotouhenying/p/10158653.html
Copyright © 2011-2022 走看看