当前位置:网站首页>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
边栏推荐
- Data access - entityframework integration
- Enter a command with the keyboard
- Binary tree related OJ problems
- 给自己打打气
- 有序链表集合求交集 方法 总结
- Win11提示无法安全下载软件怎么办?Win11无法安全下载软件
- Some cognitive thinking
- Spring Festival Limited "forget trouble in the year of the ox" gift bag waiting for you to pick it up~
- 如何安装mysql
- Benji Banas membership pass holders' second quarter reward activities update list
猜你喜欢
CISP-PTE之SQL注入(二次注入的应用)
【深度学习】深度学习如何影响运筹学?
PSPNet | 语义分割及场景分析
Binary tree related OJ problems
Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)
如何将mysql卸载干净
2020-2022两周年创作纪念日
Summary of methods for finding intersection of ordered linked list sets
How does win11 change icons for applications? Win11 method of changing icons for applications
Seaborn draws 11 histograms
随机推荐
Summary of methods for finding intersection of ordered linked list sets
文件操作--I/O
Quelques réflexions cognitives
How was the middle table destroyed?
Cartoon: what is distributed transaction?
Flet教程之 09 NavigationRail 基础入门(教程含源码)
[echart] resize lodash 实现窗口缩放时图表自适应
PHP strict mode
Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)
Facing new challenges and becoming a better self -- attacking technology er
CISP-PTE之SQL注入(二次注入的应用)
How to install MySQL
英特尔第13代Raptor Lake处理器信息曝光:更多核心 更大缓存
Starkware: to build ZK "universe"
Cartoon: what is the eight queens problem?
How to set the WiFi password of the router on the computer
今日睡眠质量记录79分
Mongodb getting started Tutorial Part 04 mongodb client
The list set is summed up according to a certain attribute of the object, the maximum value, etc
The new version of effect editor is online! 3D rendering, labeling, and animation, this time an editor is enough