UDF(user defined function)
- 背景
- 系统内置函数无法解决所有的实际业务问题,需要开发者自己编写函数实现自身的业务实现诉求。
- 应用场景非常多,面临的业务不同导致个性化实现很多,故udf很需要。
- 意义
- 函数扩展得到解决,极大丰富了可定制化的业务需求。
- IO要求-要解决的问题
- in:out=1:1,只能输入一条记录当中的数据,同时返回一条处理结果。
- 属于最常见的自定义函数,像cos,sin,substring,instr等均是如此要求。
- 实现步骤(Java创建自定义UDF类)
- 自定义一个java类
- 继承UDF类
- 约定俗成的重写evaluate方法
- 打包类所在项目成一个all-in-one的jar包并上传到hive所在机器
- 在hive中执行add jar操作,将jar加载到classpath中。
- 在hive中创建模板函数,使得后边可以使用该函数名称调用实际的udf函数
- hive sql中像调用系统函数一样使用udf函数
- 代码实现
- 功能要求:实现当输入字符串超过2个字符的时候,多余的字符以"..."来表示。
- 如“12”则返回“12”,如“123”返回“12..."
- 自定义类、继承UDF、约定俗成的“重写”evaluate方法已在代码中体现
package com.tianliangedu.hive.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
/*
* 功能:实现当输入字符串超过2个字符的时候,多余的字符以"..."来表示。
* 输入/输出:* 如“12”则返回“12”,如“123”返回“12..."
*/
public class ValueMaskUDF extends UDF{
public String evaluate(String input,int maxSaveStringLength,String replaceSign) {
if(input.length()<=maxSaveStringLength){
return input;
}
return input.substring(0,maxSaveStringLength)+replaceSign;
}
public static void main(String[] args) {
System.out.println(new ValueMaskUDF().evaluate("河北省",2,"..."));
- 布署步骤
- maven管理
采用maven管理打包的方式,maven pom配置项为:
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd";>
<modelVersion>4.0.0</modelVersion>
<groupId>com.tianliangedu.course</groupId>
<artifactId>TlHadoopCore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 设置编码为 UTF-8 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
<!-- 首先配置仓库的服务器位置,首选阿里云,也可以配置镜像 -->
<repositories>
<repository>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
</repositories>
<dependencies>
<!-- 引入hadoop-cli-2.7.4依赖 -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-cli</artifactId>
<version>1.2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>TlHadoopCore</finalName>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
- maven打包操作
- 上传jar包至hive操作环境中
进入到自己的所操作的hive环境目录
- rz命令上传至服务器上
- 加载jar包、声明函数、使用函数
- 加载jar包(输入hive进入到hive cli)
- 将jar包加入hive 交互中
add jar hdfs:///home/hive/tianliangedu_course/04_udf/TlHadoopCore-jar-with-dependencies.jar;
- 声明函数
create temporary function mask
as
'com.tianliangedu.hive.udf.ValueMaskUDF';
UDAF(user defined aggregation function)
- 自定义udaf函数self_count,实现系统udaf count的功能
- in:out=n:1,即输入N条数据,返回一条处理结果,即列转行。
- 最常见的系统聚合函数,如count,sum,avg,max等
- 实现步骤
- 自定义一个java类
- 继承UDAF类
- 内部定义一个静态类,实现UDAFEvaluator接口
- 实现方法init,iterate,terminatePartial,merge,terminate共5个方法.
- 在hive中执行add jar操作,将jar加载到classpath中。
- 在hive中创建模板函数,使得后边可以使用该函数名称调用实际的udf函数
- hive sql中像调用系统函数一样使用udaf函数
- 代码实现
实现与hive原生的count相似的计数功能。
- 如:select count(1) from tablename 或者select key,count(1) from tablename group by key;
package com.tianliangedu.hive.udaf;
import org.apache.hadoop.hive.ql.exec.UDAF;
import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;
import org.apache.log4j.Logger;
/**
* 自行实现sql的count操作
*/
//主类继承UDAF
public class DIYCountUDAF extends UDAF {
//日志对象初始化,使访类有输出日志的能力
public static Logger logger=Logger.getLogger(DIYCountUDAF.class);
//静态类实现UDAFEvaluator
public static class Evaluator implements UDAFEvaluator {
//设置成员变量,存储每个统计范围内的总记录数
private int totalRecords;
//初始化函数,map和reduce均会执行该函数,起到初始化所需要的变量的作用
public Evaluator() {
init();
}
//初始化,初始值为0,并日志记录下相应输出
public void init() {
totalRecords = 0;
logger.info("init totalRecords="+totalRecords);
}
//map阶段,返回值为boolean类型,当为true则程序继续执行,当为false则程序退出
public boolean iterate(String input) {
//当input输入不为空的时候,即为有值存在,即为存在1行,故做+1操作
if (input != null) {
totalRecords += 1;
}
//输出当前组处理到第多少条数据了
logger.info("iterate totalRecords="+totalRecords);
return true;
}
/**
* 类似于combiner,在map范围内做部分聚合,将结果传给merge函数中的形参mapOutput
* 如果需要聚合,则对iterator返回的结果处理,否则直接返回iterator的结果即可
*/
public int terminatePartial() {
logger.info("terminatePartial totalRecords="+totalRecords);
return totalRecords;
}
// reduce 阶段,用于逐个迭代处理map当中每个不同key对应的 terminatePartial的结果
public boolean merge(int mapOutput) {
totalRecords +=mapOutput;
logger.info("merge totalRecords="+totalRecords);
return true;
}
//处理merge计算完成后的结果,此时的count在merge完成时候,结果已经得出,无需再进一次对整体结果做处理,故直接返回即可
public int terminate() {
logger.info("terminate totalRecords="+totalRecords);
return totalRecords;
}
}
}
- 布署步骤
跟udf完全一致
- 加载jar包、声明函数、使用函数
跟udf完全一致
- 测试运行
与count一样,使用前边定义的临时udaf函数。