- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 Avro 1.7.0 使用它的 Java's generic representation API我在处理我们当前的模式演变案例时遇到了问题。我们在这里处理的场景是通过将字段更改为 null
的联合来使原始类型字段成为可选字段。和那个原始类型。
我将使用一个简单的例子。基本上,我们的模式是:
int
的字段null
的并集和 int
根据schema resolution chapter根据 Avro 的规范,这种情况的解决方案应该是:
if reader's is a union, but writer's is not
The first schema in the reader's union that matches the writer's schema is recursively resolved against it. If none match, an error is signalled.
我的解释是我们应该将使用初始模式序列化的数据正确解析为 int
是读者模式中联合的一部分。
但是,当运行使用版本 2 回读版本 1 序列化记录的测试时,我得到
org.apache.avro.AvroTypeException: Attempt to process a int when a union was expected.
下面的测试正好说明了这一点:
@Test
public void testReadingUnionFromValueWrittenAsPrimitive() throws Exception {
Schema writerSchema = new Schema.Parser().parse("{\n" +
" \"type\":\"record\",\n" +
" \"name\":\"NeighborComparisons\",\n" +
" \"fields\": [\n" +
" {\"name\": \"test\",\n" +
" \"type\": \"int\" }]} ");
Schema readersSchema = new Schema.Parser().parse(" {\n" +
" \"type\":\"record\",\n" +
" \"name\":\"NeighborComparisons\",\n" +
" \"fields\": [ {\n" +
" \"name\": \"test\",\n" +
" \"type\": [\"null\", \"int\"],\n" +
" \"default\": null } ] }");
// Writing a record using the initial schema with the
// test field defined as an int
GenericData.Record record = new GenericData.Record(writerSchema);
record.put("test", Integer.valueOf(10));
ByteArrayOutputStream output = new ByteArrayOutputStream();
JsonEncoder jsonEncoder = EncoderFactory.get().
jsonEncoder(writerSchema, output);
GenericDatumWriter<GenericData.Record> writer = new
GenericDatumWriter<GenericData.Record>(writerSchema);
writer.write(record, jsonEncoder);
jsonEncoder.flush();
output.flush();
System.out.println(output.toString());
// We try reading it back using the second schema
// version where the test field is defined as a union of null and int
JsonDecoder jsonDecoder = DecoderFactory.get().
jsonDecoder(readersSchema, output.toString());
GenericDatumReader<GenericData.Record> reader =
new GenericDatumReader<GenericData.Record>(writerSchema,
readersSchema);
GenericData.Record read = reader.read(null, jsonDecoder);
// We should be able to assert that the value is 10 but it
// fails on reading the record before getting here
assertEquals(10, read.get("test"));
}
我想知道我的期望是否正确(这应该会成功解决,对吧?)或者我在哪些地方没有正确使用 avro 来处理这种情况。
最佳答案
将原始模式迁移到 null 和原始的联合的期望是正确的。
上面代码的问题在于解码器的创建方式。解码器需要作者的模式而不是读者的模式。
而不是这样做:
JsonDecoder jsonDecoder = DecoderFactory.get().
jsonDecoder(readersSchema, output.toString());
应该是这样的:
JsonDecoder jsonDecoder = DecoderFactory.get().
jsonDecoder(writerSchema, output.toString());
感谢 Doug Cutting 在 avro 的用户邮件列表中的回答: http://mail-archives.apache.org/mod_mbox/avro-user/201208.mbox/browser
关于java - 用于将字段从原语演变为联合的 Avro 模式解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12061765/
我对 JS 还很陌生,目前正在学习对象,对原语有点困惑。在 W3schools 上,javascript 原语定义为: “原始值是没有属性或方法的值。 原始数据类型是具有原始值的数据。 JavaScr
我可以用 java 做这个吗? private static boolean isRight(){ return new Random.nextBolean(); } boolean test
OpenCV 原语是否基于 CUDA Nvidia 性能原语 (NPP)? 我所说的原语是指在 NPP 库中实现的相同原语,例如:boxFilter、Mirror、Convolution... 我想知
这个问题在这里已经有了答案: How many integers can I create in 1GB memory? (3 个答案) 关闭 6 年前。 引用自 Algorithms 4th ed
我正在尝试编写我的第一个显示三角形的 DirectX 10 程序。一切都编译得很好,渲染函数被调用,因为背景变成了黑色。但是,我尝试使用三角形 strip 基元绘制的三角形根本不显示。 初始化函数:
什么是 UI 原语? if you find yourself wishing that you had a different set of UI primitives to work with,
我知道这个问题已经在 Stakoverflow 中被问过多次。但是没有一个解决方案对我有帮助。谁能告诉我做错了什么 我有一个 Controller Action 如下 Function WF
我正在尝试从一个程序中读取一个二进制文件,该程序将日志(某种)写入一个dat文件,我已经很好地找到了使用Java的格式。我这样加载它: DataInputStream in = new DataInp
我有代码对 MVC Controller 方法进行 AJAX 调用,如果我这样做,它会顺利工作: var obj = '{"titlename":"whatever"}'; $
我有一个对象: class Data{ int a; int b; AtomicIntegerArray c = new AtomicIntegerArray(10000); }
我的应用程序具有以下 Javascript 原语: var bearerToken = "a"; var expirationDate = "b"; var firstName = "c"; var
我经常发现自己需要让组件接受任何有效的 HTML 属性以使底层 HTML 元素使用它们。 class Input extends React.Component { // Here I use o
我尝试使用 javascriptserializer 反序列化我的 json,但收到错误“无效的 json 原语” 其他帖子提到要验证 json,我也这样做了。 JavaScriptSerialize
有没有办法制作一个原语并反复使用它?例如:如果我制作一个立方体,我可以创建 100 个立方体并制作一个 10x10 的网格吗?我试过使用 for 循环并在每个循环中更新 x 和 z 坐标,但它只移动在
扩展和装箱 Java 原语。 我知道不可能将包装类从一个扩展到另一个,因为它们不是来自同一继承树。为什么不能将基元扩展为另一种基元类型并自动装箱扩展的基元? 鉴于可以将字节参数传递给需要 int 的方
你能帮我解决这个问题吗?我有一个 asp.net 应用程序,在此我使用 Javascript 序列化程序序列化数据集,然后转换为列表。该代码如下所示。 JavaScriptSerializer jso
好的,所以当我尝试运行我的应用程序时遇到这个问题: Invalid JSON primitive: . public static void ReloadConfig() { if (!Fil
我正在尝试通过以下 jQuery Ajax 调用将字符串化的 JSON 对象发送到 MVC 方法: $.ajax({ type: "POST", url:
当我在 C# 中使用 JavaScriptSerializer 时,出现“无效的 JSON 原语”异常。我假设问题出在我的 json 输入字符串上,但我没有看到问题。 JavaScriptSerial
我知道您可以直接从 Scala 访问 java 原语 val javaDouble = new java.lang.Double(1.0) 这是否意味着我们正在通过包装器或直接访问原语?语法 new
我是一名优秀的程序员,十分优秀!