Table Of Contents

TitanGraphQuery gremlin


query.gremlin(self, gremlin)

Executes a Gremlin query.

Parameters:

gremlin : unicode

The Gremlin script to execute.

Examples of Gremlin queries:

g.V[0..9] - Returns the first 10 vertices in graph g.V.userId - Returns the userId property from vertices g.V(‘name’,’hercules’).out(‘father’).out(‘father’).name - Returns the name of Hercules’ grandfather

Returns:

: dict

List of query results serialized to JSON and runtime of Gremlin query in seconds. The list of results is in GraphSON format(for vertices or edges) or JSON (for other results like counts). GraphSON is a JSON-based format for property graphs which uses reserved keys that begin with underscores to encode vertex and edge metadata.

Examples of valid GraphSON:

{ \"name\": \"lop\", \"lang\": \"java\",\"_id\": \"3\", \"_type\": \"vertex\" }
{ \"weight\": 1, \"_id\": \"8\", \"_type\": \"edge\", \"_outV\": \"1\", \"_inV\": \"4\", \"_label\": \"knows\" }

See https://github.com/tinkerpop/blueprints/wiki/GraphSON-Reader-and-Writer-Library

Executes a Gremlin query on an existing graph.

Notes

The query does not support pagination so the results of query should be limited using the Gremlin range filter [i..j], for example, g.V[0..9] to return the first 10 vertices.

Examples

Get the first two outgoing edges of the vertex whose source equals 5767244:

>>> mygraph = ta.get_graph("mytitangraph")
>>> results = mygraph.query.gremlin("g.V('source', 5767244).outE[0..1]")
>>> print results["results"]

The expected output is a list of edges in GraphSON format:

[{u'_label': u'edge', u'_type': u'edge', u'_inV': 1381202500, u'weight': 1, u'_outV': 1346400004, u'_id': u'fDEQC9-1t7m96-1U'},{u'_label': u'edge', u'_type': u'edge', u'_inV': 1365600772, u'weight': 1, u'_outV': 1346400004, u'_id': u'frtzv9-1t7m96-1U'}]

Get the count of incoming edges for a vertex:

>>> results = mygraph.query.gremlin("g.V('target', 5767243).inE.count()")
>>> print results["results"]

The expected output is:

[4]

Get the count of name and age properties from vertices:

>>> results = mygraph.query.gremlin("g.V.transform{[it.name, it.age]}[0..10])")
>>> print results["results"]

The expected output is:

[u'["alice", 29]', u'[ "bob", 45 ]', u'["cathy", 34 ]']