Getting Started
Prerequisites
Goblin requires python version 3.5 to 3.8 and runs on gremlin-python drivers 3.2.4 and later.
Goblin is in the Pypi repository and as such can be installed through pip.
pip install goblin
The Basics
OGM
Define custom vertex/edge classes using the provided base goblin.element
,
goblin.properties.Property
, and goblin.properties
.
from goblin import element, propertiesclass Person(element.Vertex):name = properties.Property(properties.String)age = properties.Property(properties.Integer)class Knows(element.Edge):notes = properties.Property(properties.String, default='N/A')
Create a goblin.app.Goblin
and register the element classes.
import asynciofrom goblin import Goblinloop = asyncio.get_event_loop()app = loop.run_until_complete(Goblin.open(loop))app.register(Person, Knows)
Other than user defined properties, elements provide no interface. Use a
goblin.session.Session
object to interact with the database.
async def go(app):session = await app.session()leif = Person()leif.name = 'Leif'leif.age = 28jon = Person()jon.name = 'Jonathan'works_with = Knows(leif, jon)session.add(leif, jon, works_with)await session.flush()result = await session.g.E(works_with.id).next()assert result is works_withpeople = session.traversal(Person) # element class based traversal sourceasync for person in people:print(person)loop.run_until_complete(go(app))
Note that a goblin.session
does not necessarily
correspond to a Gremlin Server session. Instead, all elements created using
a session are 'live' in the sense that if the results of a traversal executed
against the session result in different property values for an element, that
element will be updated to reflect these changes.
Gremlin Language Variant
Generate and submit Gremlin traversals in native Python.
from goblin import DriverRemoteConnection # alias for aiogremlin.DriverRemoteConnectionfrom goblin import Graph # alias for aiogremlin.Graphasync def go(loop):remote_connection = await DriverRemoteConnection.open('http://localhost:8182/gremlin', 'g')g = Graph().traversal().withRemote(remote_connection)vertices = await g.V().toList()await remote_connection.close()return verticesresults = loop.run_until_complete(go(loop))# results = [v[...], ...]
Driver
Submit scripts and bindings.
import asynciofrom goblin import Cluster # alias for aiogremlin.Clusterloop = asyncio.get_event_loop()async def go(loop):cluster = await Cluster.open(loop)client = await cluster.connect()resp = await client.submit("g.addV('developer').property(k1, v1)",bindings={'k1': 'name', 'v1': 'Leif'})async for msg in resp:print(msg)await cluster.close()loop.run_until_complete(go(loop))