python - 如何在 Python Gremlin 中添加 g:Vertex GraphSON?
问题描述
我将 a 的示例顶点 GraphSONg:Vertex
放在一个文件中:
$ cat vertex.json
{ "@type" : "g:Vertex", "@value" : { "id" : { "@type" : "g:Int32", "@value" : 1 }, "label" : "person", "properties" : { "name" : [ { "@type" : "g:VertexProperty", "@value" : { "id" : { "@type" : "g:Int64", "@value" : 0 }, "value" : "marko", "label" : "name" } } ], "location" : [ { "@type" : "g:VertexProperty", "@value" : { "id" : { "@type" : "g:Int64", "@value" : 6 }, "value" : "san diego", "label" : "location", "properties" : { "startTime" : { "@type" : "g:Int32", "@value" : 1997 }, "endTime" : { "@type" : "g:Int32", "@value" : 2001 } } } }, { "@type" : "g:VertexProperty", "@value" : { "id" : { "@type" : "g:Int64", "@value" : 7 }, "value" : "santa cruz", "label" : "location", "properties" : { "startTime" : { "@type" : "g:Int32", "@value" : 2001 }, "endTime" : { "@type" : "g:Int32", "@value" : 2004 } } } }, { "@type" : "g:VertexProperty", "@value" : { "id" : { "@type" : "g:Int64", "@value" : 8 }, "value" : "brussels", "label" : "location", "properties" : { "startTime" : { "@type" : "g:Int32", "@value" : 2004 }, "endTime" : { "@type" : "g:Int32", "@value" : 2005 } } } }, { "@type" : "g:VertexProperty", "@value" : { "id" : { "@type" : "g:Int64", "@value" : 9 }, "value" : "santa fe", "label" : "location", "properties" : { "startTime" : { "@type" : "g:Int32", "@value" : 2005 } } } } ] } } }
尝试将其读入 Python Gremlin Server:
from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
g = traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','g'))
g.io("/home/ubuntu/vertex.json").read().iterate()
产生错误:
GremlinServerError: 500: org.apache.tinkerpop.shaded.jackson.databind.JsonMappingException: 无法按要求反序列化 JSON 值。嵌套异常:java.lang.InstantiationException:无法将 JSON ('g:Vertex') 中包含的检测类型的值反序列化为对象映射器参数中指定的类型(接口 java.util.Map)。这些类型是不兼容的。在 [来源:(ByteArrayInputStream);行:1,列:36]
我试过弄乱要指定的graphson_reader
和message_serializer
参数,但我无法克服那个错误。 DriverRemoteConnection
GraphSONSerializersV3d0
如何将上面的示例顶点 GraphSON 读入 Python 的 Gremlin 服务器的图形?
解决方案
您是如何创建 GraphSON 的?使用 TinkerGraph 创建一个简单的 GraphSON 文件并将您的文件与该文件进行比较以确保您的语法正确可能是值得的。我使用以下步骤创建了如下所示的 JSON。您示例中的 GraphSON 看起来更像是查询结果,而不是描述图形的文件。无论如何,这是一个例子:
gremlin> g.addV('test').property('name','some-name').property('age','some-age')
==>v[61015]
gremlin> g.io('test.json').write()
{"id":{"@type":"g:Int64","@value":61015},"label":"test","properties":{"name":[{"id":{"@type":"g:Int64","@value":61016},"value":"some-name"}],"age":[{"id":{"@type":"g:Int64","@value":61017},"value":"some-age"}]}}
这是 Apache TinkerPop GraphSON 参考文档的链接。
推荐阅读
- c# - 您可以使用反射在异步方法中获取 LocalVariables 吗?
- javascript - 使用强大的错误 NodeJS Express MongoDB 解析图像问题
- python-3.x - 在python中将数组写入csv文件
- java - Java 将对象写入 CSV 文件
- debezium - debezium oracle 连接器的问题
- string - 从字符串中提取张量
- c# - 将具有数据注释属性的 C# 类或 .net 核心模型序列化为 JSON 格式
- android - 防止快速单击按钮并使用 rxjava 发出请求
- java - Spring OncePerRequestFilter 阻止将 POST 请求发送到路由
- python - 熊猫选择总和最接近给定数字的行