当前位置:网站首页>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
边栏推荐
- Flet教程之 11 Row组件在水平数组中显示其子项的控件 基础入门(教程含源码)
- [brush title] goose factory shirt problem
- Enterprise backup software Veritas NetBackup (NBU) 8.1.1 installation and deployment of server
- Apple 已弃用 NavigationView,使用 NavigationStack 和 NavigationSplitView 实现 SwiftUI 导航
- Practice independent and controllable 3.0 and truly create the open source business of the Chinese people
- Raspberry pie 4B installation pytorch1.11
- 如何安装mysql
- Benji Bananas 会员通行证持有人第二季奖励活动更新一览
- Parameter type setting error during batch update in project SQL
- yarn 常用命令
猜你喜欢
公司自用的国产API管理神器
Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
How to set the WiFi password of the router on the computer
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
迁移/home分区
Pits encountered in the use of boolean type in development
Binary tree related OJ problems
今日睡眠质量记录79分
Flet教程之 11 Row组件在水平数组中显示其子项的控件 基础入门(教程含源码)
OneForAll安装使用
随机推荐
The new version of effect editor is online! 3D rendering, labeling, and animation, this time an editor is enough
阿掌的怀念
Google Earth engine (GEE) -- a brief introduction to kernel kernel functions and gray level co-occurrence matrix
Sentinel flow guard
Record a 'very strange' troubleshooting process of cloud security group rules
解决CMakeList find_package找不到Qt5,找不到ECM
How to uninstall MySQL cleanly
一键安装脚本实现快速部署GrayLog Server 4.2.10单机版
Summary of methods for finding intersection of ordered linked list sets
今日睡眠质量记录79分
PHP strict mode
ES6 drill down - Async functions and symbol types
JSON转MAP前后数据校验 -- 自定义UDF
Reduce the cost by 40%! Container practice of redis multi tenant cluster
How to set the WiFi password of the router on the computer
Data Lake (XIV): spark and iceberg integrated query operation
记一次'非常诡异'的云安全组规则问题排查过程
Is it safe for Guotai Junan to open an account online
【刷題篇】鹅廠文化衫問題
Can you help me see what the problem is? [ERROR] Could not execute SQL stateme