如何在 Python Gremlin 中添加 g:Vertex GraphSON?
How to add g:Vertex GraphSON in Python Gremlin?
我把g:Vertex
的example vertex GraphSON放在一个文件里:
$ 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 服务器:
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:
Could not deserialize the JSON value as required. Nested exception:
java.lang.InstantiationException: Cannot deserialize the value with
the detected type contained in the JSON ('g:Vertex') to the type
specified in parameter to the object mapper (interface java.util.Map).
Those types are incompatible. at [Source: (ByteArrayInputStream);
line: 1, column: 36]
我试过弄乱 DriverRemoteConnection
的 graphson_reader
和 message_serializer
参数来指定 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 参考文档的 link。
我把g:Vertex
的example vertex GraphSON放在一个文件里:
$ 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 服务器:
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: Could not deserialize the JSON value as required. Nested exception: java.lang.InstantiationException: Cannot deserialize the value with the detected type contained in the JSON ('g:Vertex') to the type specified in parameter to the object mapper (interface java.util.Map). Those types are incompatible. at [Source: (ByteArrayInputStream); line: 1, column: 36]
我试过弄乱 DriverRemoteConnection
的 graphson_reader
和 message_serializer
参数来指定 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 参考文档的 link。