当前位置:网站首页>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
边栏推荐
- 怎样在电脑上设置路由器的WiFi密码
- Exception com alibaba. fastjson. JSONException: not match : - =
- 利用GrayLog告警功能实现钉钉群机器人定时工作提醒
- 解决CMakeList find_package找不到Qt5,找不到ECM
- 【刷題篇】鹅廠文化衫問題
- 帮忙看看是什么问题可以吗?[ERROR] Could not execute SQL stateme
- 10 minutes to help you get ZABBIX monitoring platform alarm pushed to nail group
- 《21天精通TypeScript-3》-安装搭建TypeScript开发环境.md
- Cartoon: what is MapReduce?
- Do sqlserver have any requirements for database performance when doing CDC
猜你喜欢

Binary tree related OJ problems

Research and development efficiency measurement index composition and efficiency measurement methodology

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

Accès aux données - intégration du cadre d'entité

"21 days proficient in typescript-3" - install and build a typescript development environment md

Data Lake (XIV): spark and iceberg integrated query operation

中间表是如何被消灭的?

服务器的数据库连不上了2003,10060“Unknown error“【服务已起、防火墙已关、端口已开、netlent 端口不通】

BS-XX-042 基于SSM实现人事管理系统

怎样在电脑上设置路由器的WiFi密码
随机推荐
程序员如何提升自己的格局?
Accès aux données - intégration du cadre d'entité
數據訪問 - EntityFramework集成
树莓派4b安装Pytorch1.11
The difference between searching forward index and inverted index
Data access - entityframework integration
Record a 'very strange' troubleshooting process of cloud security group rules
如何安装mysql
Benji Banas membership pass holders' second quarter reward activities update list
【刷题篇】鹅厂文化衫问题
《21天精通TypeScript-3》-安装搭建TypeScript开发环境.md
《MongoDB入门教程》第04篇 MongoDB客户端
Intel 13th generation Raptor Lake processor information exposure: more cores, larger cache
Today's sleep quality record 79 points
ES6 drill down - Async functions and symbol types
Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)
一些认知的思考
Using graylog alarm function to realize the regular work reminder of nail group robots
10分钟帮你搞定Zabbix监控平台告警推送到钉钉群
sqlserver 做cdc 要对数据库性能有什么要求么