Previous topic

Properties

Next topic

Groovy

Donate Bitcoins

Gremlin

Execute Gremlin scripts in the database.

The Gremlin class is used to execute Gremlin scripts on the database. Gremlin is a domain-specific language for graphs. It’s like SQL for graphs.

By default, a Gremlin object is built into the Graph object as g.gremlin. You write your Gremlin-Groovy scripts in a text file, which allows you to get full syntax highlighting in an editor,

Example (same example as used for GroovyScripts):

// gremlin.groovy

// calculate basic collaborative filtering for user_id
def rank_items(user_id) {
    m = [:]
    g.v(user_id).out('likes').in('likes').out('likes').groupCount(m)
    m.sort{a,b -> a.value <=> b.value}
    return m.values()
}

The scripts file can contain more than one function per file.

You add the scripts file to the Scripts object using the g.scripts.update(file_path) method. Then you can get the individual scripts by their function names:

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() # create Neo4j Graph object
>>> g.scripts.update('gremlin.groovy') # add file to scripts index
>>> script = g.scripts.get('rank_items') # get a function by its name
>>> params = dict(user_id=3) # put function params in dict
>>> items = g.gremlin.query(script, params) # execute the script in DB
class bulbs.gremlin.Gremlin(client)[source]

An interface for executing Gremlin scripts on the client.

Parameters:client (Client) – The Client object for the database.
command(script, params=None)[source]

Returns the raw Result object from an arbitrary Gremlin command.

Parameters:
  • script (str) – Gremlin script to execute on the client.
  • params (dict or None) – Optional paramaters to bind to the Gremlin script.
Return type:

Result

Note

Use this when you are executing a command that returns a single result that does not need to be initialized.

query(script, params=None)[source]

Returns initialized Element objects from an arbitrary Gremlin query.

Parameters:
  • script (str) – Gremlin script to execute on the client.
  • params (dict or None) – Optional paramaters to bind to the Gremlin script.
Return type:

Generator of objects: Vertex, Edge, Node, or Relationship

Note

Use this when you are returning elements that need to be initialized.

execute(script, params=None)[source]

Returns the raw Response object from an arbitrary Gremlin script.

Parameters:
  • script (str) – Gremlin script to execute on the client.
  • params (dict or None) – Optional paramaters to bind to the Gremlin script.
Return type:

Response

Note

Use this when you are returning element IDs and the actual elements are cached in Redis or Membase. Or, when you’re returning primitives or Table data.