当前位置:网站首页>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
边栏推荐
- Practice independent and controllable 3.0 and truly create the open source business of the Chinese people
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- 对象和类的关系
- 《21天精通TypeScript-3》-安装搭建TypeScript开发环境.md
- 研发效能度量指标构成及效能度量方法论
- Quelques réflexions cognitives
- The visual experience has been comprehensively upgraded, and Howell group and Intel Evo 3.0 have jointly accelerated the reform of the PC industry
- Cartoon: what is distributed transaction?
- 搜索 正排索引 和 倒排索引 区别
- abstract关键字和哪些关键字会发生冲突呢
猜你喜欢

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

Pits encountered in the use of boolean type in development

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

ES6 deep - ES6 class class

抽象类中子类与父类
英特尔第13代Raptor Lake处理器信息曝光:更多核心 更大缓存

Seaborn draws 11 histograms

数据访问 - EntityFramework集成

项目中批量update
Intel 13th generation Raptor Lake processor information exposure: more cores, larger cache
随机推荐
Seaborn绘制11个柱状图
公司自用的国产API管理神器
APICloud云调试解决方案
Which keywords will conflict with the abstract keyword
Use of set tag in SQL
Cartoon: what is the eight queens problem?
怎样在电脑上设置路由器的WiFi密码
數據訪問 - EntityFramework集成
企业级备份软件Veritas NetBackup(NBU) 8.1.1服务端的安装部署
数据访问 - EntityFramework集成
List uses stream flow to add according to the number of certain attributes of the element
《21天精通TypeScript-3》-安装搭建TypeScript开发环境.md
CISP-PTE之PHP伪协议总结
漫画:什么是MapReduce?
对象和类的关系
The list set is summed up according to a certain attribute of the object, the maximum value, etc
Practice independent and controllable 3.0 and truly create the open source business of the Chinese people
Cartoon: what is MapReduce?
Find the root of the following equation by chord cutting method, f (x) =x^3-5x^2+16x-80=0
list去重并统计个数