【问题标题】:How to add g:Vertex GraphSON in Python Gremlin?如何在 Python Gremlin 中添加 g:Vertex GraphSON?
【发布时间】:2020-04-10 04:29:41
【问题描述】:

我将g:Vertexexample 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 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]

我尝试使用DriverRemoteConnectiongraphson_readermessage_serializer 参数来指定GraphSONSerializersV3d0,但我无法克服该错误。

如何将上面的示例顶点 GraphSON 读入 Python 的 Gremlin 服务器中的图形?

【问题讨论】:

    标签: python json gremlin gremlin-server graphson


    【解决方案1】:

    您是如何创建 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 参考文档的链接。

    http://tinkerpop.apache.org/docs/3.4.6/dev/io/#graphson

    【讨论】:

    • 哦不,你是说我的输入不是 Graphson 吗? FWIW,我输入的链接和您引用的链接是相同的,除了我指向this block,我认为这是一个格式正确的 GraphSON2 顶点的示例。如果这不对,您能否澄清一下该块是什么以及是否有办法加载它?
    • 您展示的是运行查询时可能获得的 JSON。如果您阅读文档,它会显示类似于我在答案中发布的示例,这些示例显示了用于加载图表的文件格式。请注意差异,例如没有 g:Vertex,只有“id”等。我建议您按照我的做法从 TinkerGraph 中保存一个顶点并研究输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多