Hive UDF 函数
1.安装maven,修改settings.xml
<localRepository>C:/Soft/maven/repository</localRepository>
<mirrors>
<mirror>
<id>mirror</id>
<mirrorOf>central,jcenter,!rdc-releases,!rdc-snapshots</mirrorOf>
<name>mirror</name>
<url>https://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!-- junit镜像地址 -->
<mirror>
<id>junit</id>
<name>junit Address/</name>
<url>http://jcenter.bintray.com/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
2.idea创建maven项目
1.添加依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.0.0-cdh6.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>2.1.1-cdh6.3.3</version>
</dependency>
</dependencies>
2.如果依赖无法下载,到 https://mvnrepository.com/ 中央仓库下载所需依赖,并使用mvn命令添加
call mvn install:install-file -DgroupId=org.apache.hadoop -DartifactId=hadoop-common -Dversion=3.0.0-cdh6.3.3 -Dpackaging=jar -Dfile=D:\Downloads\hadoop-common-3.0.0-cdh6.3.3.jar
3.打包插件添加
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
4.编写udf代码
public class udf_replace extends UDF {
private String str = null;
public String evaluate(String urlStr){
if (urlStr == null){
return "";
}
String strs = urlStr.replace("(","(")
.replace(")",")")
.replace("。",".")
.replace("φ","Φ")
.replace("","")
;
return strs;
}
}
5.打jar包
6.上传jar包到服务器
-rw-r–r–. 1 root root 39788030 8月 2 11:13 hive_udf_replace_chars.jar
put到HDFS
hdfs dfs -mkdir /user/hive/udf
hdfs dfs -put hive_udf_replace_chars.jar /user/hive/udf/
3.hive创建自定义函数
--创建临时函数
CREATE temporary FUNCTION udf_replace AS 'udf_replace' using jar 'hdfs://cdh-01.com:8020/user/hive/udf/hive_udf_replace_chars.jar';
--创建永久函数
CREATE FUNCTION udf_replace AS 'com.hzjt.UdfReplace' using jar 'hdfs:///user/hive/udf/hive_udf_replace_char-1.0-SNAPSHOT.jar';
--删除函数
drop [temporary] function [if exists] [dbname.]函数名;
例:删除永久函数(临时的就没必要了,因为一退出就没了)
drop function replace_chars;
--永久函数需要add jar
add jar hdfs://cdh-01.com:8020/user/hive/udf/hive_udf_replace_chars.jar;
--删除jar
delete jar hive_udf_replace_chars.jar
add jar
add jar hdfs://cdh-01.com:8020/user/hive/udf/hive_udf_replace_chars.jar;
--删除jar
delete jar hive_udf_replace_chars.jar