我正在使用neo4j(neo4j.v1)的python驱动程序,我正在通过文档,以及neo4j网站中找到的各种入门代码。
到目前为止我制作的代码:
from neo4j.v1 import GraphDatabase, basic_auth
import sys
class boltSession(object):
def __init__(self, uri, user, password):
self._driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
self._driver.close()
def get_people(self, attr):
with self._driver.session() as session:
return session.read_transaction(self.match_people_node, attr)
def add_person(self, attr):
with self._driver.session() as session:
return session.write_transaction(self.create_person_node, attr)
def get_person(self, name):
with self._driver.session() as session:
session.read_transaction(self.match_person_node, name)
return
@staticmethod
def match_people_node(tx, attr):
name = attr['name']
result = tx.run("MATCH (a:Person{name: $name}) RETURN a", name = name)
return [record["a"] for record in result]
@staticmethod
def match_person_node(tx,name):
result = tx.run("MATCH (a:Person {name: $name}) RETURN count(a)", name=name)
return result.single()[0]
@staticmethod
def create_person_node(tx, attr):
name = attr['name']
country = attr['country']
result = tx.run("CREATE (a:Person {name: $name, country: $country})",
name = name,
country = country)
return result
uri = 'bolt://localhost:7687'
user = 'neo4j'
password = 'neo4j'
neoSession = boltSession(uri, user, password)
attr1 = {} ; attr1['name'] = 'Joe' ; attr1['country'] = 'USA' ;
attr2 = {} ; attr2['name'] = 'Mat' ; attr2['country'] = 'USA' ;
attr3 = {} ; attr3['name'] = 'Joe' ; attr3['country'] = 'AUS' ;
neoSession.add_person(attr1)
neoSession.add_person(attr2)
neoSession.add_person(attr3)
seek1 = {} ; seek1['name'] = 'Joe'
seek2 = {} ; seek2['country'] = 'USA'
print neoSession.get_people(seek1)
neoSession.close()
此代码适用于python2.7,为数据库分配3个节点,read_transaction返回如下所示的数据结构:
<Node id=1555 labels=set([u'Person']) properties={u'country': u'USA', u'name': u'Joe'}>
有没有办法将其转换为熟悉的Python数据结构?我现在完全失去了。