2

我正在使用 gremlin Python 来查询 Neptune DB。给定一个顶点,我需要返回所有向外的边缘以及它们的“从”和“到”ID、标签和任何其他属性。

下面的查询

query_result = g.V().has('name', 'marco').outE().inV().path().toList()

以我可以解析成字典列表的形式给我'from'和'to',边缘的值映射给我其他值,但我需要它在一个列表中返回。我的理想格式是 [{from: x, to: y, label: foo, property1: bar},...]

非常感谢任何帮助。

4

2 回答 2

4

You can get almost the result you need by simply projecting the results. I will add an example at the end of how to flatten it even more. You should be able to tweak this query any way you need adding more valueMap steps etc. This does not yield a single list but groups each edge with its properties, label and ID.

Note that I used valueMap(true) which is deprecated and the new form is valueMap().with(WithOptions.tokens). Either will still work currently. The advantage of this approach is that no tracking of the path is needed which in general is more efficient in terms of memory usage etc. by the query engine.

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]

gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]

gremlin> g.V().has('name','marko').
               outE().
               project('from','edge','to').
                 by(outV()).
                 by(valueMap(true)).
                 by(inV())

==>[from:v[1],edge:[id:9,label:created,weight:0.4],to:v[3]]
==>[from:v[1],edge:[id:7,label:knows,weight:0.5],to:v[2]]
==>[from:v[1],edge:[id:8,label:knows,weight:1.0],to:v[4]]    

If you want to flatten this result into a single list you can just add a little more to the query:

gremlin> g.V().has('name','marko').
               outE().
               project('from','edge','to').
                 by(outV()).
                 by(valueMap(true)).
                 by(inV()).
               union(select('edge').unfold(),
                     project('from').by(select('from')).unfold(),
                     project('to').by(select('to')).unfold()).fold()   


[id=9,label=created,weight=0.4,from=v[1],to=v[3],id=7,label=knows,weight=0.5,from=v[1],to=v[2],id=8,label=knows,weight
=1.0,from=v[1],to=v[4]]  

Lastly if you wanted a series of such lists back rather than one big one you can wrap the union step in local scope.

gremlin> g.V().has('name','marko').
               outE().
               project('from','edge','to').
                 by(outV()).
                 by(valueMap(true)).
                 by(inV()).local(
               union(select('edge').unfold(),
                     project('from').by(select('from')).unfold(),
                     project('to').by(select('to')).unfold()).fold())      

==>[id=9,label=created,weight=0.4,from=v[1],to=v[3]]
==>[id=7,label=knows,weight=0.5,from=v[1],to=v[2]]
==>[id=8,label=knows,weight=1.0,from=v[1],to=v[4]]        
于 2020-05-19T13:18:58.450 回答
1

您可以通过elementMap以下步骤完成:

g.V().has('name', 'marko').outE().inV().
  path().
    by(elementMap())

编辑:

如果不支持,您可以使用步骤elementMap分别指定要从顶点和边获取的内容。by在那里你可以创建任何你想要的数据格式project

g.V().has('name', 'marko').outE().inV().
  path().
    by(valueMap(true)).by(union(
      project('from', 'to').
        by(outV().id()).
        by(inV().id()),
      valueMap(true)
    ).unfold().
    group().by(keys).
      by(select(values).unfold()))

示例:https ://gremlify.com/ab

于 2020-05-19T06:14:56.737 回答