pythonneo4j.v1dri返回的数据结构

2024-10-03 11:12:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用用于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_事务返回一个如下所示的数据结构:

^{pr2}$

有没有什么方法可以把它转换成一个熟悉的Python数据结构?我现在完全不知所措。在


Tags: nameselfnodereturnsessiondefdriveruri