Created
August 23, 2011 14:01
-
-
Save espeed/1165193 to your computer and use it in GitHub Desktop.
Neo4j Jython Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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