当前位置:网站首页>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
边栏推荐
- Desci: is decentralized science the new trend of Web3.0?
- 【网易云信】超分辨率技术在实时音视频领域的研究与实践
- HiEngine:可媲美本地的云原生内存数据库引擎
- 企业级备份软件Veritas NetBackup(NBU) 8.1.1服务端的安装部署
- Starkware: to build ZK "universe"
- sql中set标签的使用
- What is the difference between EDI license and ICP business license
- Five common negotiation strategies of consulting companies and how to safeguard their own interests
- How can programmers improve their situation?
- Single merchant v4.4 has the same original intention and strength!
猜你喜欢
List de duplication and count the number
The new version of effect editor is online! 3D rendering, labeling, and animation, this time an editor is enough
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
Mistakes made when writing unit tests
Accès aux données - intégration du cadre d'entité
服务器的数据库连不上了2003,10060“Unknown error“【服务已起、防火墙已关、端口已开、netlent 端口不通】
Cs231n notes (top) - applicable to 0 Foundation
项目sql中批量update的时候参数类型设置错误
超分辨率技术在实时音视频领域的研究与实践
2020-2022 two-year anniversary of creation
随机推荐
写单元测试的时候犯的错
abstract关键字和哪些关键字会发生冲突呢
今日睡眠质量记录79分
How to use FRP intranet penetration +teamviewer to quickly connect to the intranet host at home when mobile office
极坐标扇图使用场景与功能详解
异常com.alibaba.fastjson.JSONException: not match : - =
The database of the server is not connected to 200310060 "unknown error" [the service is up, the firewall is off, the port is on, and the netlent port is not connected]
Batch update in the project
后台系统发送验证码功能
Dataarts studio data architecture - Introduction to data standards
The new version of effect editor is online! 3D rendering, labeling, and animation, this time an editor is enough
一键安装脚本实现快速部署GrayLog Server 4.2.10单机版
10 minutes to help you get ZABBIX monitoring platform alarm pushed to nail group
sql中set标签的使用
obj集合转为实体集合
超分辨率技术在实时音视频领域的研究与实践
Single merchant v4.4 has the same original intention and strength!
HiEngine:可媲美本地的云原生内存数据库引擎
vulnhub-FirstBlood
StarkWare:欲构建ZK“宇宙”