首页 > 解决方案 > Gremlin Python:在列表中返回顶点 ID 和边属性

问题描述

我很难返回我正在寻找的确切信息。本质上,我想返回某种类型的所有边(在任一方向)。我想在列表中获取顶点 ID 和边缘参数。理想情况下,我的输出格式如下所示:

{
    "from": "vertex_id_1",
    "to": "vertex_id_2",
    "similarity": 0.45
}

我一直在使用g.both().vertexMap().toList()来获取相似之处,但我无法以这种方式获取顶点 ID。

标签: pythongraphgremlinamazon-neptune

解决方案


以玩具图为例,您最好这样做project()

gremlin> g.V().bothE('knows').
......1>   project('from','to','weight').
......2>     by(outV().id()).
......3>     by(inV().id()).
......4>     by('weight')
==>[from:1,to:2,weight:0.5]
==>[from:1,to:4,weight:1.0]
==>[from:1,to:2,weight:0.5]
==>[from:1,to:4,weight:1.0]

推荐阅读