2014-03-14 42 views
2

我有以下代碼將對象序列化爲Avro。如何在Avro中使用循環引用處理對象

List childrenList = new ArrayList(); 
RootNode root = new RootNode(); 
root.setChildrenList(childrenList); 

ChildNode child1 = new ChildNode(); 
child1.setParent(root); 
childrenList.add(child1); 

ChildNode child2 = new ChildNode(); 
child2.setParent(root); 
childrenList.add(child2); 

Schema schema = ReflectData.AllowNull.get().getSchema(root.getClass()); 
DatumWriter datumWriter = new ReflectDatumWriter (root.getClass()); 
DataFileWriter fileWriter = new DataFileWriter (datumWriter); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
fileWriter.create(schema, baos); 
fileWriter.append(root); 

但是由於root和children之間的循環引用,這會進入無限循環。

最終,Apache Avro失敗並出現stackoverflow錯誤。

我到處搜索了很多地方,但似乎沒有任何解決方案。

有誰知道如何讓Avro處理循環引用?

更新:我知道另外兩個序列化器框架--Gson和Jackson,只有Jackson很好地處理循環引用。所以我很懷疑Avro是否會爲此提供解決方案,因爲它看起來不是一個常見的案例?

回答

相關問題