HIVE自定义函数 UDF

时间:2021-10-08 17:30:41

自定义my_md5hash

具体hive源码怎么调用看我另外一篇博客

package udf.hive.myudf;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF; @Description(name="my_md5hash", value="md5")
public class MyUdf extends UDF{
public String evaluate(String in) {
return DigestUtils.md5Hex(in);
} }

  mvn打包

add JAR /home/cloudera/Desktop/myudf-0.0.3-SNAPSHOT.jar;

CREATE TEMPORARY FUNCTION my_md5hash AS 'udf.hive.myudf.MyUdf';

数据

hive> add JAR /home/cloudera/Desktop/myudf-0.0.3-SNAPSHOT.jar;
Added [/home/cloudera/Desktop/myudf-0.0.3-SNAPSHOT.jar] to class path
Added resources: [/home/cloudera/Desktop/myudf-0.0.3-SNAPSHOT.jar]

hive> CREATE TEMPORARY FUNCTION my_md5hash AS 'udf.hive.myudf.MyUdf';
OK
Time taken: 0.06 seconds

hive> select * from prouct
> ;
OK
1 coco 3.0
2 hamburg 18.0
3 chips 8.0
4 tea 10.0

hive> select my_md5hash(name) from prouct;
OK
ac0ddf9e65d57b6a56b2453386cd5db5
f23bb5e2f7d35c767b40eddd42ac0e08
19136e394ab695f9b071eb24e88ab14d
7239ea2b5dc943f61f3c0a0276c20974
Time taken: 0.433 seconds, Fetched: 4 row(s)