其实hive的udf 是比较容易实现的,只需要继承UDF,实现其evaluate()方法,代码如下。
@Description(name = "decoder_url", value = "_FUNC_(url [,code][,count]) - decoder a URL from a String for count times using code as encoding scheme ", extended = ""
+ "if count is not given ,the url will be decoderd for 2 time,"
+ "if code is not given ,GBK is used")
public class UDFDecoderUrl extends UDF {
private String url = null;
private int times = 2;
private String code = "GBK";
public UDFDecoderUrl() {
}
public String evaluate(String urlStr, String srcCode, int count) {
if (urlStr == null) {
return null;
}
if (count <= 0) {
return urlStr;
}
if (srcCode != null) {
code = srcCode;
}
url = urlStr;
times = count;
for (int i = 0; i < times; i++) {
url = decoder(url, code);
}
return url;
}
public String evaluate(String urlStr, String srcCode) {
if (urlStr == null) {
return null;
}
url = urlStr;
code = srcCode;
return evaluate(url, code,times);
}
public String evaluate(String urlStr, int count) {
if (urlStr == null) {
return null;
}
if (count <= 0) {
return urlStr;
}
url = urlStr;
times = count;
return evaluate(url, code,times);
}
public String evaluate(String urlStr) {
if (urlStr == null) {
return null;
}
url = urlStr;
return evaluate(url, code,times);
}
private String decoder(String urlStr, String code) {
if (urlStr == null || code == null) {
return null;
}
try {
urlStr = URLDecoder.decode(urlStr, code);
} catch (Exception e) {
return null;
}
return urlStr;
}
}
在类中org.apache.hadoop.hive.ql.exec.FunctionRegistry中添加
registerUDF("decoder_url", UDFDecoderUrl.class, false);
编译hive ,或者通过配置文件方式,让其读取,以后新加的函数配置到配置文件中一劳永逸。
上面的类UDFDecoderUrl需要打成jar包加载到hive中,需要再hive-site.xml配置如下加载jar包
<property>
<name>hive.aux.jars.path</name>
<value>file:///opt/hive/sohu/hive-udf-0.0.1.jar</value>
<description>These JAR file are available to all users for all jobs</description>
</property>
分享到:
相关推荐
hive的udf函数实现
hive编写 udf 至少需要引入的jar包:hive-exec-xxx.jar 和 hadoop-core-xxx.jar
大数据的hive资源的详细代码设计以及分享,望博友相互交流
udf开发–做个简单脱敏udf保留前5位,后面全部替换成*****
主要介绍了大数据 java hive udf函数(手机号码脱敏),的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
Hive UDF 说明书,官方指定文档。Hive_LanguageManual_UDF
NULL 博文链接:https://superlxw1234.iteye.com/blog/1586377
详细介绍如何开发hive自定义永久函数,配套有测试数据
一、UDF相关概念 ...如果udf嵌套复杂,可以重写一个嵌套层数较少且可以实现相同功能的udf,使性能成倍提升 针对过滤类的udf,将过滤率高的放在前面,减少中间结果,避免不必要的计算 二、UDF的使用 1、建hive表
地址转换成经纬度+两地址间距离计算+省市区位置解析(Java代码) Hive自定义函数的封装
Hive UDF UDF 聚合 UDF Finds MIN, MAX and SUM from array of Struct Objects based on a field. 排序 UDF Returns sorted array of Struct objects for an array of Struct Objects based on a field. 日期 ...
NexR Hive UDF 关于 NexR Hive UDF是Hive用户定义功能的集合。 执照 快速开始 $ git clone https://github.com/nexr/hive-udf.git $ cd hive-udf $ mvn clean package or $ ant -Dhive.install.dir=../hive/build/...
Spark Hive UDF示例 建立项目 mvn clean package 将spark-hive-udf-1.0.0-SNAPSHOT.jar复制到边缘节点临时目录 spark-hive-udf]# cp target/spark-hive-udf-1.0.0-SNAPSHOT.jar /tmp 通过提供罐子来启动火花壳 spark...
16. 空格字符串函数:space 27 17. 重复字符串函数:repeat 27 18. 首字符ascii函数:ascii 28 19. 左补足函数:lpad 28 20. 右补足函数:rpad 28 21. 分割字符串函数: split 28 22. 集合查找函数: find_in_set 29 ...
通用hive udf 源码,想要开发hadoop hive 的开发者 可以下载试看
Oracle字符相似度函数。在sql语句中直接用该函数来计算2个字符串相似度。
udf函数,包含日期计算,ip,useragent解析函数,加密解密等. 注意: jet-hive-udf支持hive-0.11.0或更高版本. 其中useragent解析需要使用到java8+,其他需要java6+ 编译 1. 安装依赖 本项目仅需要maven环境;不需要...
hiveUDF-1.0-SNAPSHOT.jar
08.hive内置函数--时间-日期-字符串--函数.mp4
strstr()函数搜索一个字符串在另一个字符串中的第一次出现。 该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。 语法复制代码 代码如下:strstr(string,search)输出结果”@exe.com...