Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@espeed
Created August 23, 2011 14:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save espeed/1165193 to your computer and use it in GitHub Desktop.
Save espeed/1165193 to your computer and use it in GitHub Desktop.
Neo4j Jython Example
# Neo4j Jython Example
# by James Thornton, http://jamesthornton.com
from org.neo4j.kernel import EmbeddedGraphDatabase
from org.neo4j.kernel import Config
from org.neo4j.graphdb import DynamicRelationshipType
class Database(object):
def __init__(self,path,config=None):
self.path = path
self.config = config
self.graph = None
def start(self):
self.graph = EmbeddedGraphDatabase(self.path)
def beginTx(self):
return self.graph.beginTx()
def shutdown(self):
self.graph.shutdown()
class Node(object):
@classmethod
def create(self):
return db.graph.createNode()
@classmethod
def get(self,eid):
return db.graph.getNodeById(eid)
def run():
db.start()
tx = db.beginTx()
try:
james = Node.create()
james.setProperty("name","James")
julie = Node.create()
julie.setProperty("name","Julie")
KNOWS = DynamicRelationshipType.withName( "knows" )
rel = james.createRelationshipTo(julie,KNOWS)
tx.success()
except:
print "FAIL"
tx.failure()
tx.finish()
james2 = Node.get(james.id)
assert james.id == james2.id
assert rel.getStartNode().equals(james)
assert rel.getEndNode().equals(julie)
print "NAME: ", james2.getProperty('name')
db.shutdown()
# Run the example
db = Database("/tmp/neo4jdb")
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment