简体   繁体   English

如何使用 python 将度数添加到 Gremlin 列表中的每个顶点?

[英]How do you add the degree to every vertex in a list in Gremlin using python?

I'm trying to add the degree (number of vertexes connected to the given vertexes) to each one of the vertexes in a list.我正在尝试将度数(连接到给定顶点的顶点数)添加到列表中的每个顶点。

Generating the degree for each vertex works-为每个顶点生成度数 -

c.g.V(ids).as_('vertex'). \
  both(). \
  groupCount(). \
  by(select('vertex')).toList()

Saving a constant degree to all of them works将恒定的度数保存到所有这些都有效

c.g.V(ids).as_('vertex'). \
  both().groupCount().by(select('vertex')).unfold(). \
  sideEffect(
    __.select(Column.keys).property(Cardinality.single, "degree", 1)
  ).toList()

Though when I try to save the degree itself I get errors.尽管当我尝试保存学位本身时,我遇到了错误。 Note that the query groups the vertexes, and we have a dictionary from vertex to its degree.请注意,查询对顶点进行了分组,并且我们有一个从顶点到它的度数的字典。 In the sideEffect function, I select the key - the vertex, and try to save the value into it.sideEffect function 中,我 select 键 - 顶点,并尝试将值保存到其中。

Queries I have tried-我试过的查询-

c.g.V(ids).as_('vertex'). \
  both().groupCount().by(select('vertex')).unfold(). \
  sideEffect(
    __.store('x').select(Column.keys).property(Cardinality.single, "degree", cap('x')).select(Column.values))
  ).toList()
c.g.V(ids).as_('vertex'). \
  both().groupCount().by(select('vertex')).unfold(). \
  sideEffect(
    __.store('x').select(Column.keys).property(Cardinality.single, "degree", __.select(Column.values))
  ).toList()

Does anyone know what is wrong with my queries?有谁知道我的查询有什么问题? I basically want to extract Column.values from the group and insert it into the property.我基本上想从组中提取Column.values并将其插入到属性中。

Edit: The current implementation is as suggested in the first solution -编辑:当前的实施是第一个解决方案中的建议 -

c.g.V(ids).property(Cardinality.single,
                    "degree",
                    __.both()
                    .count()).iterate()

The reason I'm working on it is because it was really slow (the actual query has many has and hasLabel queries which make it slower).我正在处理它的原因是因为它真的很慢(实际查询有很多hashasLabel查询,这使得它变慢了)。

I've noticed that the first query I've attached is much much faster than the one currently used, and that's why I'm trying to use it.我注意到我附加的第一个查询比当前使用的查询快得多,这就是我尝试使用它的原因。

You don't really need to create a group to do this.您真的不需要创建一个组来执行此操作。 You can just create the counts as part of the standard Gremlin traversal flow (stream).您可以将计数创建为标准 Gremlin 遍历流(流)的一部分。 For example:例如:

gremlin> g.V(1,2,3,4).project('degree').by(both().count())
==>[degree:486]
==>[degree:84]
==>[degree:188]
==>[degree:150]

gremlin> g.V(1,2,3,4).property('degree',both().count())
==>v[1]
==>v[2]
==>v[3]
==>v[4]

gremlin> g.V(1,2,3,4).values('degree')
==>486
==>84
==>188
==>150  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM