如何将 Titan 图形数据导出到 JSON 文件?

How to export Titan graph data to a JSON file?

我正在尝试使用以下代码将 Titan 图形数据导出到 JSON 文件

TitanGraph titanGraph = TitanFactory.open("D:/Dev/Titan/titan-1.0.0-hadoop1/conf/titan-cassandra.properties");
FileOutputStream outJSON = new FileOutputStream("D:/titan-graph.json");
GraphSONWriter.build().create().writeGraph(outJSON, titanGraph);

虽然 运行 此代码出现异常:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.IllegalStateException) (through reference chain: com.thinkaurelius.titan.graphdb.relations.RelationIdentifier["inVertexId"])
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:177)
    at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:187)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:647)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONUtil.writeWithType(GraphSONUtil.java:49)
    at org.apache.tinkerpop.gremlin.structure.util.star.StarGraphGraphSONSerializer.ser(StarGraphGraphSONSerializer.java:100)
    at org.apache.tinkerpop.gremlin.structure.util.star.StarGraphGraphSONSerializer.serialize(StarGraphGraphSONSerializer.java:64)
    at org.apache.tinkerpop.gremlin.structure.util.star.StarGraphGraphSONSerializer.serialize(StarGraphGraphSONSerializer.java:54)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:128)
    at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:2881)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:2304)
    at org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter.writeVertex(GraphSONWriter.java:76)
    at org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter.writeVertices(GraphSONWriter.java:104)
    at org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter.writeGraph(GraphSONWriter.java:65)
    at com.mimos.titan.TitanGraphTest.main(TitanGraphTest.java:29)
Caused by: java.lang.IllegalStateException
    at com.google.common.base.Preconditions.checkState(Preconditions.java:158)
    at com.thinkaurelius.titan.graphdb.relations.RelationIdentifier.getInVertexId(RelationIdentifier.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:466)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:639)
    ... 12 more

您应该让 Titan 图处理序列化,因为它需要 register custom handlers 来序列化某些类型,例如 RelationIdentifier

titanGraph.io(IoCore.graphson()).writeGraph("D:/titan-graph.json")

Titan 1.0.0 使用的 Apache TinkerPop 3.0.1 documentation 中对此进行了描述。