当前位置:网站首页>JSON转MAP前后数据校验 -- 自定义UDF
JSON转MAP前后数据校验 -- 自定义UDF
2022-07-05 15:56:00 【牧码文】
JSON转MAP前后数据校验
JSON转MAP后的数据校验可以通过原始的sql实现,但是代码逻辑稍微复杂,为了简化操作,也可以通过自定义UDF实现。
因为想要实现的目的比较简单,就是map和json的转换前后比较,那么输入两个参数,在evalute方法中处理map和json即可,要做的也是逐个key比较value值即可
一、initialize方法
检查参数:initialize方法
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
// 传入参数必须是两个
if (arguments.length != 2) {
throw new UDFArgumentLengthException("The function MAP_KEYS only accepts one argument.");
} else if (!(arguments[0] instanceof MapObjectInspector)) {
// 第一个参数类型需要是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())){
// 第二个参数类型需要是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();
// 返回结果为String类型,其实是个Boolean
return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
}
二、evaluate方法
处理逻辑:evaluate方法
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
// 每次将集合置空
retArray.clear();
// 每次讲结果置true
isEquals = true;
Object mapObj = arguments[0].get();
String json = arguments[1].get().toString();
// 第二个参数转为json对象
JSONObject jsonObject = new JSONObject(json);
// 获得第一个参数的map
Map<?,?> mapVal = mapOI.getMap(mapObj);
// 遍历key
if (mapVal != null) {
retArray.addAll(mapVal.keySet());
for (int i = 0; i < retArray.size(); i++) {
try {
// 如果map中的key在json中不存在,返回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 isEquals;
}
三、测试
打包上传集群,创建测试函数
测试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
测试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
边栏推荐
- Pits encountered in the use of boolean type in development
- What is the difference between EDI license and ICP business license
- The new version of effect editor is online! 3D rendering, labeling, and animation, this time an editor is enough
- 事务回滚异常
- Subclasses and superclasses of abstract classes
- Some cognitive thinking
- Use of RLOCK lock
- Cartoon: what is blue-green deployment?
- [深度学习][原创]让yolov6-0.1.0支持yolov5的txt读取数据集模式
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
猜你喜欢

StarkWare:欲构建ZK“宇宙”

项目sql中批量update的时候参数类型设置错误

The new version of effect editor is online! 3D rendering, labeling, and animation, this time an editor is enough
![[deep learning] how does deep learning affect operations research?](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[deep learning] how does deep learning affect operations research?

Domestic API management artifact used by the company

CISP-PTE之SQL注入(二次注入的应用)

ES6深入—ES6 Generator 函数

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

Migrate /home partition

公司自用的国产API管理神器
随机推荐
ES6 drill down - ES6 generator function
What is the difference between EDI license and ICP business license
Query the latest record in SQL
How can programmers improve their situation?
Single merchant v4.4 has the same original intention and strength!
[js] skill simplification if empty judgment
用键盘输入一条命令
清晰还原31年前现场,火山引擎超清修复Beyond经典演唱会
单商户 V4.4,初心未变,实力依旧!
【网易云信】超分辨率技术在实时音视频领域的研究与实践
开发中Boolean类型使用遇到的坑
Data Lake (XIV): spark and iceberg integrated query operation
sql中查询最近一条记录
Exception com alibaba. fastjson. JSONException: not match : - =
Spring Festival Limited "forget trouble in the year of the ox" gift bag waiting for you to pick it up~
Migrate /home partition
Cartoon: what is blue-green deployment?
数据湖(十四):Spark与Iceberg整合查询操作
Which keywords will conflict with the abstract keyword
This article takes you through the addition, deletion, modification and query of JS processing tree structure data