hive查找键值是否存在的udf,直接上代码。
package com.xixishuiba.udf;
import java.util.Iterator;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.json.JSONException;
import org.json.JSONObject;
public class FindJsonkey
extends UDF
{
public String evaluate(String column,String keyword) {
if (null == column || column == "{}") {
return "0";
}
if(null == keyword) {return "0";}
String ret = "0";
String tmp = column.replace("\"{", "{").replace("}\"", "}").replace("\\\"", "\"");
try {
JSONObject object = new JSONObject(tmp);
Iterator<String> itr = object.keys();
while (itr.hasNext()) {
String key = itr.next();
if(key.equals(keyword)){
ret = "1";
}
}
} catch (JSONException e) {
e.printStackTrace();
return "0";
}
return ret + "";
}
public static void main(String[] argv){
String col = "{\"p\":17,\"t\":2,\"pvid\":\"1531963476379551374lpEbhy12vx3Z3\",\"id\":1940361}";
String col2 = "{\"t\":2,\"id\":1940361,\"p\":17,\"pvid\":\"1531963476379551374lpEbhy12vx3Z3\"}";
FindJsonkey fj = new FindJsonkey();
//String split = parseJson.evaluate(str);
String a2 = fj.evaluate(col2,"pvid");
System.out.println(a2);
}
}
使用函数:
add jar /data/home/dw/lisardon/new/UDF_STY/findjsonkey.jar;
create temporary function findjsonkey as ‘com.autohome.udf.FindJsonkey’;
查找表中的json字段是否有 jsonkey这个key。
select * from tmp_table findjsonkey(jsoncolumn,jsonkey) = 1 ;