package com.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
public class MyConcatUdf extends UDF{
public String evaluate(String word) {
if(word == null) {
return "NULL";
}
return word+"_class";
}
public static void main(String[] args) {
}
}
导出jar后
添加自定义的jar包
add jar /home/MyUDF/MyConcat.jar;
创建临时函数
create temporary function myconcat as 'com.udf.MyConcatUdf';
show functions;
退出以后就不行用了,当前session有效
第二个案例
根据key获取value
package com.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.json.JSONException;
import org.json.JSONObject;
public class KeyToValue extends UDF{
public String evaluate(String allValue,String key) throws JSONException {
if(allValue == null|| key==null) {
return "NULL";
}
//正常取值
//sex=1&weight=128&height&sla=30000
allValue = allValue.replaceAll("&", ",");
allValue = allValue.replaceAll("=", ":");
allValue="{"+allValue+"}";
JSONObject js = new JSONObject(allValue);
String result = js.get(key).toString();
return result;
}
public static void main(String[] args) throws JSONException {
System.out.println(new KeyToValue().evaluate("sex=1&weight=128&height=180&sla=30000", "sex"));
}
}
使用的过程相同