neo4j图形数据库的db api 2.0驱动程序。
neo4jdb的Python项目详细描述
为Neo4J实现PythonDBAPI2.0,与Python2.6、2.7兼容, 3.2和3.3以及neo4j>;=2.0.0
http://legacy.python.org/dev/peps/pep-0249/
安装
pip install neo4jdb
最小可行片段
import neo4j connection = neo4j.connect("http://localhost:7474") cursor = connection.cursor() for name, age in cursor.execute("MATCH (n:User) RETURN n.name, n.age"): print name, age
用法
库通常遵循db api,请参阅文档 对于db api的详细用法。
# Write statements cursor.execute("CREATE (n:User {name:'Stevie Brook'}") connection.commit() # Or connection.rollback() # With positional parameters cursor.execute("CREATE (n:User {name:{0}})", "Bob") # or l = ['Bob'] cursor.execute("CREATE (n:User {name:{0}})", *l) # With named parameters cursor.execute("CREATE (n:User {name:{name}})", name="Bob") # or d = {'name': 'Bob'} cursor.execute("CREATE (n:User {name:{name}})", **d) # or d = {'node': {'name': 'Bob'}} cursor.execute("CREATE (n:User {node})", **d)
如果要求cypher返回节点或关系,则这些节点和关系类型表示为 是带有id、标签、类型、结束id和开始id的附加元数据的对象。
# Retrieve and access a node for node, in cursor.execute("MATCH (n) RETURN n"): print node.id, node.labels print node['a_property'] # Retrieve and access a relationship for rel, in cursor.execute("MATCH ()-[r]->() RETURN r"): print rel.id, rel.type, rel.start_id, rel.end_id print rel['a_property']
使用上下文管理器。上下文中的任何异常都将导致引发异常并回滚事务。
from neo4j.contextmanager import Neo4jDBConnectionManager manager = contextmanager.Neo4jDBConnectionManager('http://localhost:7474') with manager.read as r: # r is just a cursor for name, age in r.execute("MATCH (n:User) RETURN n.name, n.age"): print name, age # When leaving read context the transaction will be rolled back with manager.write as w: w.execute("CREATE (n:User {name:{name}})", name="Bob") # When leaving write context the transaction will be committed # When using transaction a new connection will be created with manager.transaction as t: t.execute("CREATE (n:User {name:{name}})", name="Bob") # When leaving transaction context the transaction will be # committed and the connection will be closed # Rolling back or commit in contexts with manager.transaction as t: t.execute("CREATE (n:User {name:{name}})", name="Bob") if something == True: t.connection.commit() # This will commit the transaction else: t.connection.rollback() # This will rollback the transaction
建筑和测试
Neo4JDB使用摊铺机作为其构建系统。安装摊铺机:
pip install paver
然后您可以使用:
paver build
安装时使用:
paver install
运行测试需要在本地主机上运行neo4j服务器。摊铺机可处理 这是给你的:
paver start_server paver nosetests paver stop_server
不符合规范
参数
库委托给neo4j进行参数替换,这意味着它可以 不使用规范中定义的任何标准参数替换类型。
相反,它使用带命名和/或位置参数的花括号:
{0} or {identifier}
类型系统
因为neo4j的wire格式是json,所以库不支持 日期或二进制值类型。这在将来可能会随着wire格式的改变而改变 因为neo4j进化了。
类似地,由于neo4j是模式可选数据库,它可能返回 结果表中每个单元格中的任意类型。因此,对 结果表总是将每种列类型标记为neo4j.mixed。