2.idea创建maven项目C:/Soft/maven/repository mirror central,jcenter,!rdc-releases,!rdc-snapshots mirror https://maven.aliyun.com/nexus/content/groups/public alimaven aliyun maven http://maven.aliyun.com/nexus/content/repositories/central/ central junit junit Address/ http://jcenter.bintray.com/ central
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oqMxRICy-1660039059927)(D:/Soft/Typora/images/image-20220802112753789.png)]
1.添加依赖2.如果依赖无法下载,到 https://mvnrepository.com/ 中央仓库下载所需依赖,并使用mvn命令添加org.apache.hadoop hadoop-common 3.0.0-cdh6.3.3 org.apache.hive hive-exec 2.1.1-cdh6.3.3
call mvn install:install-file -DgroupId=org.apache.hadoop -DartifactId=hadoop-common -Dversion=3.0.0-cdh6.3.3 -Dpackaging=jar -Dfile=D:Downloadshadoop-common-3.0.0-cdh6.3.3.jar3.打包插件添加
4.编写udf代码org.apache.maven.plugins maven-shade-plugin 3.2.0 package shade
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包
-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