具有Avro的递归架构(SchemaBuilder) [英] Recursive schema with avro (SchemaBuilder)
问题描述
是否可以制作一个递归的avro模式,例如
Is it possible to make an avro schema which is recursive, like
Schema schema = SchemaBuilder
.record("RecursiveItem")
.namespace("com.example")
.fields()
.name("subItem")
.type("RecursiveItem")
.withDefault(null) // not sure about that too...
.endRecord();
像这样使用它时,我得到一个StackOverflowError:
I get a StackOverflowError when using it like that:
static class RecursiveItem {
RecursiveItem subItem;
}
RecursiveItem item1 = new RecursiveItem();
RecursiveItem item2 = new RecursiveItem();
item1.subItem = item2;
final DatumWriter<RecursiveItem> writer = new SpecificDatumWriter<>(schema);
// note: I actually want a binary output, but I started with some json code I found
ByteArrayOutputStream stream = new ByteArrayOutputStream();
final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, stream);
writer.write(rec1, encoder);
String json = stream.toString();
注意:如果我使用以下方法创建架构,也会得到StackOverflowError
Note: I also get StackOverflowError if I make the schema using:
Schema schema = ReflectData.get().getSchema(RecursiveItem.class);
推荐答案
警告:我找到了一种写解决方案,但无法阅读:-\
Warning: I found a solution to write, but cannot read it bask :-\
我不确定是否真的了解,但是我设法使其适用于:
I am not sure to really understand, but I manage to make it works with:
-
应该使用
-
ReflectDatumWriter
代替SpecificDatumWriter
由于在编码时自动查找架构,我仍然找不到架构的问题.它查找具有自动派生的名称空间和名称的架构.在我的类是静态子类的情况下,应使用以下内容:
I still had issue with schema not found due to automated lookup of schema upon encoding. It looks for a schema with a namespace+name derived automatically. And in my case where the class is a static subclass, the following should be used:
String cls = RecursiveItem.class.getSimpleName();
String pck = RecursiveItem.class.getPackage().getName();
if (RecursiveItem.class.getEnclosingClass() != null) // nested class
pck = RecursiveItem.class.getEnclosingClass().getName() + "$";
要管理null,应使用以下架构
To manage the null, the following schema should be used
Schema schema0 = SchemaBuilder
.record(cls)
.namespace(pck)
.fields()
.name("subItem")
.type().unionOf().nullType().and().type("RecursiveItem").endUnion()
.nullDefault()
.endRecord();
这篇关于具有Avro的递归架构(SchemaBuilder)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!