当前位置:网站首页>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
边栏推荐
- CISP-PTE之SQL注入(二次注入的应用)
- Dare not buy thinking
- Benji Banas membership pass holders' second quarter reward activities update list
- 【 brosser le titre 】 chemise culturelle de l'usine d'oies
- Research and development efficiency measurement index composition and efficiency measurement methodology
- 详解SQL中Groupings Sets 语句的功能和底层实现逻辑
- Domestic API management artifact used by the company
- 面对新的挑战,成为更好的自己--进击的技术er
- 如何将mysql卸载干净
- How does win11 change icons for applications? Win11 method of changing icons for applications
猜你喜欢

ES6 deep - ES6 class class

有序链表集合求交集 方法 总结

普洛斯数据中心发布DC Brain系统,科技赋能智慧化运营管理

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

PSPNet | 语义分割及场景分析

How was the middle table destroyed?

公司自用的国产API管理神器

单商户 V4.4,初心未变,实力依旧!

Research and development efficiency measurement index composition and efficiency measurement methodology

迁移/home分区
随机推荐
Quelques réflexions cognitives
Parameter type setting error during batch update in project SQL
sqlserver 做cdc 要对数据库性能有什么要求么
用键盘输入一条命令
如何安装mysql
Benji Bananas 会员通行证持有人第二季奖励活动更新一览
JSON转MAP前后数据校验 -- 自定义UDF
Benji Banas membership pass holders' second quarter reward activities update list
Cartoon: what is service fusing?
Summary of methods for finding intersection of ordered linked list sets
一键安装脚本实现快速部署GrayLog Server 4.2.10单机版
Domestic API management artifact used by the company
文件操作--I/O
PSPNet | 语义分割及场景分析
怎样在电脑上设置路由器的WiFi密码
【组队 PK 赛】本周任务已开启 | 答题挑战,夯实商品详情知识
漫画:什么是分布式事务?
【学术相关】多位博士毕业去了三四流高校,目前惨不忍睹……
Win11 prompt: what if the software cannot be downloaded safely? Win11 cannot download software safely
Win11如何给应用换图标?Win11给应用换图标的方法