当前位置:网站首页>Data verification before and after JSON to map -- custom UDF
Data verification before and after JSON to map -- custom UDF
2022-07-05 16:39:00 【Mu code text】
JSON turn MAP Before and after data verification
Content catalog
JSON turn MAP The data after verification can pass the original sql Realization , But the code logic is slightly complicated , To simplify the operation , You can also customize UDF Realization .
Because the purpose you want to achieve is relatively simple , Namely map and json Before and after the conversion , Then enter two parameters , stay evalute Method map and json that will do , What we have to do is one by one key Compare value value
One 、initialize Method
Inspection parameters :initialize Method
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
// The incoming parameters must be two
if (arguments.length != 2) {
throw new UDFArgumentLengthException("The function MAP_KEYS only accepts one argument.");
} else if (!(arguments[0] instanceof MapObjectInspector)) {
// The first parameter type needs to be MAP
throw new UDFArgumentTypeException(0, "\""
+ ObjectInspector.Category.MAP.toString().toLowerCase()
+ "\" is expected at function JsonCheckMap, " + "but \""
+ arguments[0].getTypeName() + "\" is found");
} else if (!PrimitiveObjectInspector.PrimitiveCategory.STRING.equals(((PrimitiveObjectInspector)arguments[1]).getPrimitiveCategory())){
// The second parameter type needs to be String
throw new UDFArgumentTypeException(0, "\""
+ PrimitiveObjectInspector.PrimitiveCategory.STRING.toString().toLowerCase()
+ "\" is expected at function JsonCheckMap, " + "but \""
+ arguments[1].getTypeName() + "\" is found");
}
mapOI = (MapObjectInspector) arguments[0];
ObjectInspector mapKeyOI = mapOI.getMapKeyObjectInspector();
// The return result is String type , Is actually a Boolean
return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
}
Two 、evaluate Method
Processing logic :evaluate Method
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
// Set empty each time
retArray.clear();
// Set the result every time true
isEquals = true;
Object mapObj = arguments[0].get();
String json = arguments[1].get().toString();
// The second parameter is changed to json object
JSONObject jsonObject = new JSONObject(json);
// Get the first parameter map
Map<?,?> mapVal = mapOI.getMap(mapObj);
// Traverse key
if (mapVal != null) {
retArray.addAll(mapVal.keySet());
for (int i = 0; i < retArray.size(); i++) {
try {
// If map Medium key stay json Does not exist in the , return false
String mapV = mapVal.get(retArray.get(i)).toString();
String jsonV = jsonObject.getString(retArray.get(i).toString());
if (!mapV.equals(jsonV)){
isEquals = false;
return false;
}
} catch (Exception e){
log.print(e.getMessage());
return false;
}
}
}
// Return results
return isEquals;
}
3、 ... and 、 test
Package and upload cluster , Create a test function
test 1:
CREATE TEMPORARY FUNCTION cdp_to_json AS 'com.bilibili.udf.JsonCheckMap'
using jar "viewfs://jssz-bigdata-cluster/department/bigdata/file/hdfsfile/UDF.jar/UDF.jar";
select cdp_to_json(map('hello','world'), '{"hello":"wol"}')
false
test 2:
CREATE TEMPORARY FUNCTION cdp_to_json AS 'com.bilibili.udf.JsonCheckMap'
using jar "viewfs://jssz-bigdata-cluster/department/bigdata/file/hdfsfile/UDF.jar/UDF.jar";
select cdp_to_json(map('hello','world'), '{"hello":"world"}')
true
边栏推荐
- Can you help me see what the problem is? [ERROR] Could not execute SQL stateme
- The list set is summed up according to a certain attribute of the object, the maximum value, etc
- What is the difference between EDI license and ICP business license
- [echart] resize lodash to realize chart adaptation when window is zoomed
- 【深度学习】深度学习如何影响运筹学?
- [graduation season] as a sophomore majoring in planning, I have something to say
- Cartoon: what is service fusing?
- 一些认知的思考
- Domestic API management artifact used by the company
- Research and development efficiency measurement index composition and efficiency measurement methodology
猜你喜欢

Summary of methods for finding intersection of ordered linked list sets

Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely

Data access - entityframework integration

Convert obj set to entity set

Single merchant v4.4 has the same original intention and strength!

清晰还原31年前现场,火山引擎超清修复Beyond经典演唱会

【学术相关】多位博士毕业去了三四流高校,目前惨不忍睹……

ES6 drill down - Async functions and symbol types

ES6 deep - ES6 class class

Flet教程之 11 Row组件在水平数组中显示其子项的控件 基础入门(教程含源码)
随机推荐
Raspberry pie 4B installation pytorch1.11
Cartoon: what is MapReduce?
sqlserver 做cdc 要对数据库性能有什么要求么
PSPNet | 语义分割及场景分析
自己要有自己的坚持
今日睡眠质量记录79分
Google Earth engine (GEE) -- a brief introduction to kernel kernel functions and gray level co-occurrence matrix
漫画:什么是MapReduce?
一些認知的思考
How to uninstall MySQL cleanly
Single merchant v4.4 has the same original intention and strength!
Sentinel-流量防卫兵
[es6] add if judgment or ternary operator judgment in the template string
Seaborn draws 11 histograms
Migrate /home partition
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
Single merchant v4.4 has the same original intention and strength!
One click installation script enables rapid deployment of graylog server 4.2.10 stand-alone version
Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely
Data Lake (XIV): spark and iceberg integrated query operation