优点:
1、建hive表
create table xcj_udf_test(jsonStr string);
2、准备数据
{"movie":"1193","rate":"5","timeStamp":"978300760","uid":"1"}
{"movie":"661","rate":"3","timeStamp":"978302109","uid":"1"}
{"movie":"914","rate":"3","timeStamp":"978301968","uid":"1"}
{"movie":"3408","rate":"4","timeStamp":"978300275","uid":"1"}
{"movie":"2355","rate":"5","timeStamp":"978824291","uid":"1"}
{"movie":"1197","rate":"3","timeStamp":"978302268","uid":"1"}
{"movie":"1287","rate":"5","timeStamp":"978302039","uid":"1"}
3、加载数据
load data local inpath '/data/xuecj02/hive/data.txt' into table xcj_udf_test;
4、查询数据
select * from xcj_udf_test;
5、查看表结构
desc xcj_udf_test;
6、将 jar 包添加到 hive 的 classpath
add jar hdfs://HDFS****/data/xuecj02/hive/hive-udf-test.jar;
7、创建临时函数
create temporary function udf_test as 'com.missfresh.udf.MyUdfTest';
8、使用
select udf_test(jsonStr,'rate') from tmp.xcj_udf_test;
9、udf代码
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;
public class MyUdfTest extends UDF {
public String evaluate(String jsonStr, String objName) throws IOException {
/**
*isEmpty isBlank的区别
* 1、isEmpty没有忽略空格参数,是以是否为空和是否存在为判断依据
* 2、isBlank是在isEmpty的基础上进行了为空(字符串都为空格,制表符,tab的情况)判断,比较常用
*/
if (StringUtils.isBlank(jsonStr) || StringUtils.isBlank(objName)) {
return null;
}
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
Object objValue = jsonObject.get(objName);
if (objValue == null) {
return null;
}
return objValue.toString();
}
}
临时函数
add jar hdfs://HDFS****/data/xuecj02/hive/hive-udf-test.jar;
语法:CREATE TEMPORARY FUNCTION function_name AS class_name;
function_name函数名
class_name 类路径,包名+类名
create temporary function udf_test as 'com.missfresh.udf.MyUdfTest';
Show functions;
select udf_test(jsonStr,'rate') from tmp.xcj_udf_test;
永久函数
CREATE FUNCTION [db_name.]function_name AS class_name [USING JAR|FILE|ARCHIVE 'file_uri' [, JAR|FILE|ARCHIVE 'file_uri'] ];
file_uri:是hdfs上的jar包目录
CREATE FUNCTION sayhello AS 'com.missfresh.udf.MyUdfTest' USING JAR 'hdfs://*****/data/xuecj02/hive/hive-udf-test.jar';
3、查看函数列表
Show functions;
4、测试使用
select udf_test(jsonStr,'rate') from tmp.xcj_udf_test;