hive if函数_HIVE之UDF函数开发

1

为什么要写UDF函数

       有时候hive自带的函数不能满足当前需要,需要自定义函数来解决问题

2

UDF,UDAF,UDTF的比较

  • UDF操作作用于单个数据行,并且产生一个数据行作为输出。大多数函数都属于这一类(比如数学函数和字符串函数)。

  • UDAF 接受多个输入数据行,并产生一个输出数据行。像COUNT和MAX这样的函数就是聚集函数。

  • UDTF 操作作用于单个数据行,并且产生多个数据行,一个表作为输出。lateral view explore()

简单来说:

UDF:返回对应值,一对一

UDAF:返回聚类值,多对一

UDTF:返回拆分值,一对多

3

UDF函数开发

1 代码

pom.xml

"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">    4.0.0    com.test    udf_demo    1.1    jar    udf_demo            UTF-8        2.6.0-cdh5.8.2        1.1.0-cdh5.8.2                            org.apache.hadoop            hadoop-common            2.6.0                            org.apache.hive            hive-exec            1.1.0                            org.apache.hive            hive-jdbc            1.1.0                            org.apache.hadoop            hadoop-hdfs            2.6.0                            log4j            log4j            1.2.17                        udf_demo                                    maven-assembly-plugin                                                                                                                                                                                jar-with-dependencies                                                                            org.apache.maven.plugins                maven-compiler-plugin                                    7                    7                                        

base64加密函数

package com.wedoctor;import org.apache.commons.lang.StringUtils;import org.apache.hadoop.hive.ql.exec.UDF;import sun.misc.BASE64Encoder;import java.io.UnsupportedEncodingException;/** * Created by Liuzuochang on 2020/10/07. * base64加密UDF */public class Base64Encrypt extends UDF {    public  String evaluate(String msg) throws Exception {        //判断传进来的参数是否为空        if(StringUtils.isBlank(msg)){            return "";        }        //base64加密        byte[]  bt = null;        String newMsg = null;        try {            bt = msg.getBytes("utf-8");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }        if( bt != null){            newMsg = new BASE64Encoder().encode(bt);        }        if(newMsg.contains("\r\n")){            newMsg = newMsg.replace("\r\n","");        }else if(newMsg.contains("\r")){            newMsg = newMsg.replace("\r","");        }else if(newMsg.contains("\n")){            newMsg = newMsg.replace("\n","");        }        return newMsg;    }}

base64解密函数

package com.wedoctor;import org.apache.commons.lang.StringUtils;import org.apache.hadoop.hive.ql.exec.UDF;import sun.misc.BASE64Decoder;/** * Created by Liuzuochang on 2020/10/07. * base64解密UDF */public class Base64Decrypt extends UDF {    public  String evaluate(String msg) throws Exception {        //判断传进来的参数是否为空        if(StringUtils.isBlank(msg)){            return "";        }        //base64解密        byte[] bt = null;        String result = null;        if(msg != null){            BASE64Decoder decoder = new BASE64Decoder();            try {                bt = decoder.decodeBuffer(msg);                result = new String(bt, "utf-8");            } catch (Exception e) {                e.printStackTrace();            }        }        return result;    }}
2 打包上传到运行hive所在的服务器
hive> add jar /usr/local/udf_demo.jar;
3 创建临时函数
hive> create temporary function test_en as 'com.wedoctor.Base64Encrypt';hive> create temporary function test_de as 'com.wedoctor.Base64Decrypt';
4 加解密临时函数测试
hive> select test_en('1234');MTIzNA==
hive> select test_de('MTIzNA==');1234
50a64baea414fe6567095a563d73f880.gif

2020大数据面试题真题总结(附答案)

微信交流群

如何优化整个数仓的执行时长(比如7点所有任务跑完,如何优化到5点)

从0-1建设数仓遇到什么问题?怎么解决的?

多值维度及交叉维度最佳解决方案

深入探究order by,sort by,distribute by,cluster by

Hive调优,数据工程师成神之路

数据质量那点事

简述元数据管理

你真的了解全量表,增量表及拉链表吗?

缓慢变化维(SCD)常见解决方案

全方位解读星型模型,雪花模型及星座模型

Sqoop or Datax

left join(on&where)

ID-Mapping

你们公司还在用SparkOnYan吗?

大厂高频面试题-连续登录问题

朋友面试数据研发岗遇到的面试题

数据仓库分层架构

简单聊一聊大数据学习之路

朋友面试数据专家岗遇到的面试题

HADOOP快速入门

数仓工程师的利器-HIVE详解

9b83d0dee8442705d38e9c40ffdafb5b.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值