Query Apache Tinkerpop GraphDB using Gremlin -- Explain the "Drop" "From" "Index" "key" "Label" "Project" "PropertyMap" and "Value" Steps

2020年05月02日

-
AddEdge Step
AddVertex Step
AddProperty Step


Drop Step
Remove element and properties from the graph. It is a filter step because the traversal yields no outgoing objects.

g.V("5ab8e996-5d52-c7fc-8b63-c53ff975d906").drop().iterate()

g.V().toList()
结果:
[v[1], v[2], v[3], v[4], v[5], v[6]]
Vertex 5ab8e996-5d52-c7fc-8b63-c53ff975d906 is gone.


From Step
g.V("1").both().as_("person").both().cyclicPath().path().from_("person").toList()
结果:
[path[v[2], v[1]], path[v[4], v[1]], path[v[3], v[1]]]


marko = g.V().has('name','marko').next()
peter = g.V().has('name','peter').next()
g.addE('knows').from_(marko).to(peter).iterate()
-
g.V("1").out().toList()
结果:
[v[2], v[4], v[6], v[3]]


Index Step
Index each element in the current collection.
g.V().hasLabel("software").toList()
结果:
[v[3], v[5]]


Indexing non-collection items results in multiple indexed single-item collections.
g.V().hasLabel("software").index().toList()
结果:
[[[v[3], 0]], [[v[5], 0]]]


Index all software names in their alphabetical order.
g.V().hasLabel("software").values("name").fold().order(
    Scope.local).index().unfold().order().by(
        __.tail(Scope.local, 1)).toList()
结果:
[['lop', 0], ['ripple', 1]]


Same as statement 1, but with an explicitely specified list indexer.
g.V().hasLabel("software").values("name").fold().order(
    Scope.local).index().with_(
        WithOptions.indexer, WithOptions.list
    ).unfold().order().by(__.tail(Scope.local, 1)).toList()
结果:
[['lop', 0], ['ripple', 1]]


Index all person names in their alphabetical order and store the result in an ordered map.
g.V().hasLabel("person").values("name").fold().order(
    Scope.local).index().with_(
        WithOptions.indexer, WithOptions.map).toList()
结果:
[{0: 'josh', 1: 'marko', 2: 'peter', 3: 'vadas'}]


Key Step
Takes a Property and extracts the key from it.
g.V("1").properties().key().toList()
结果:
['name', 'age']


Label Step
Takes an Element and extracts its label from it.
g.V().label().toList()
结果:
['person', 'person', 'person', 'person', 'software', 'software']


g.V("1").outE().label().toList()
结果:
['knows', 'knows', 'created']


g.V("1").properties().label().toList()
结果:
['name', 'age']


Project Step
g.V().out('created').project('a','b').by('name').by(
    __.in_('created').count()).order().by(
    __.select('b'), Order.decr).select('a').toList()
结果:
['lop', 'lop', 'lop', 'ripple']


g.V().has('name','marko').project('out','in').by(
    __.outE().count()).by(
    __.inE().count()).toList()
结果:
[{'out': 3, 'in': 0}]


PropertyMap Step
Yields a Map representation of the properties of an element.

g.V().propertyMap().toList()
结果:
[{'name': [vp[name->marko]], 'age': [vp[age->29]]}, {'name': [vp[name->vadas]], 'age': [vp[age->27]]}, {'name': [vp[name->lop]], 'lang': [vp[lang->java]]}, {'name': [vp[name->josh]], 'age': [vp[age->32]]}, {'name': [vp[name->ripple]], 'lang': [vp[lang->java]]}, {'name': [vp[name->peter]], 'age': [vp[age->35]]}]


g.V().propertyMap('age').toList()
结果:
[{'age': [vp[age->29]]}, {'age': [vp[age->27]]}, {}, {}, {'age': [vp[age->32]]}, {'age': [vp[age->35]]}]


g.V().propertyMap('age','blah').toList()
结果:
[{'age': [vp[age->29]]}, {'age': [vp[age->27]]}, {}, {}, {'age': [vp[age->32]]}, {'age': [vp[age->35]]}]


g.E().propertyMap().toList()
结果:
[{'weight': p[weight->0.5]}, {'weight': p[weight->1.0]}, {'weight': p[weight->0.4]}, {'weight': p[weight->1.0]}, {'weight': p[weight->0.4]}, {'weight': p[weight->0.2]}]


ShortestPath step


Find all shortest paths from marko.
g = g.withComputer()
g.V().shortestPath()
结果:
[['withStrategies', VertexProgramStrategy]][['V'], ['shortestPath']]


g.V().has('person','name','marko').shortestPath()
结果:
[['withStrategies', VertexProgramStrategy]][['V'], ['has', 'person', 'name', 'marko'], ['shortestPath']]


Find all shortest paths to peter.

Find all in-directed paths to josh.

Find all shortest paths from marko to josh.

Find all shortest paths from marko to josh using a custom distance property.

Find all shortest paths from marko to josh and include edges in the result.
g.V().shortestPath().with(ShortestPath.target, __.has('name','peter')) //3\
g.V().shortestPath().
        with(ShortestPath.edges, Direction.IN).
        with(ShortestPath.target, __.has('name','josh')) //4\
g.V().has('person','name','marko').
      shortestPath().
        with(ShortestPath.target, __.has('name','josh')) //5\
g.V().has('person','name','marko').
      shortestPath().
        with(ShortestPath.target, __.has('name','josh')).
        with(ShortestPath.distance, 'weight') //6\
g.V().has('person','name','marko').
      shortestPath().
        with(ShortestPath.target, __.has('name','josh')).
        with(ShortestPath.includeEdges, true) //7
        Find all shortest paths.
结果:


TimeLimit Step

g.V().repeat(
    __.both().groupCount('m')).times(16).cap('m').order(
    Scope.local).by(
    Column.values, Order.decr).next()
结果:
Query cannot be completed due to memory limitations.


Value Step

g.V("1").properties().value().toList()
结果:
['marko', 29]


References

Key Step

Label Step

Project Step

PropertyMap Step

ShortestPath step

Value Step

Drop Step

Category: GraphDB Tags: public

Upvote


Downvote