简体   繁体   中英

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.

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.

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).

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). 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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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