【发布时间】:2022-08-24 15:57:37
【问题描述】:
我正在尝试构建一个需要针对 Avro 模式进行数据验证的系统。实际上,我正在使用以下内容进行验证:
DatumReader reader = new GenericDatumReader(schema);
Decoder decoder = DecoderFactory.get().jsonDecoder(schema, data);
ValidatingDecoder validatingDecoder = DecoderFactory.get().validatingDecoder(schema, decoder);
reader.read(null, validatingDecoder);
作为架构:
\"{\"
+ \" \\\"namespace\\\": \\\"com.myApp.events\\\",\"
+ \" \\\"type\\\": \\\"record\\\",\"
+ \" \\\"name\\\": \\\"CityPeriodEvent\\\",\"
+ \" \\\"fields\\\": [\"
+ \" { \\\"name\\\": \\\"cityCode\\\", \\\"type\\\": \\\"string\\\" },\"
+ \" { \\\"name\\\": \\\"periodId\\\", \\\"type\\\": \\\"long\\\" },\"
+ \" { \\\"name\\\": \\\"startTime\\\", \\\"type\\\": {\\\"type\\\": \\\"long\\\", \\\"logicalType\\\": \\\"timestamp-millis\\\"} },\"
+ \" { \\\"name\\\": \\\"finishTime\\\", \\\"type\\\": {\\\"type\\\": \\\"long\\\", \\\"logicalType\\\": \\\"timestamp-millis\\\"} },\"
+ \" { \\\"name\\\": \\\"currency\\\", \\\"type\\\": [\\\"null\\\", \\\"string\\\"], \\\"default\\\": null },\"
+ \" { \\\"name\\\": \\\"habitants\\\", \\\"type\\\": \\\"long\\\" }\"
+ \" ]\"
+ \"}\";
正确的数据配置:
\"{\\\"cityCode\\\": \\\"BCN\\\", \\\"periodId\\\": 3, \\\"startTime\\\": 500, \\\"finishTime\\\": 3000, \\\"currency\\\": {\\\"string\\\":\\\"eur\\\"}, \\\"habitants\\\": 10000\";
数据配置不正确:
\"{\\\"cityCode\\\": \\\"BCN\\\", \\\"periodId\\\": 3, \\\"startTime\\\": 500, \\\"finishTime\\\": 3000, \\\"currency\\\": {\\\"string\\\":\\\"eur\\\"}, \\\"habitants\\\": 10000, \\\"someAdditional\\\": 3}\";
(请注意,不正确的最后有一个附加字段)。
问题是这两种配置都验证为正确,而我只希望第一个是正确的,所以我正在寻找一个非常严格的验证。
根据我在文档中读到的内容, DatumReader.read 函数通过模式执行 DFS,将模式的叶子与数据配置文件的字段进行比较。这会导致忽略额外的字段,因为比较是单向的。
有没有办法让这个更严格?我一直在做研究并尝试不同的东西,但似乎都没有奏效。我不认为我在寻找非常具体的东西,我认为很多系统都需要这个,所以我认为我只是错过了一些东西。