py2neo到BOLT:如何将tx.append语句转换为BOLT

时间:2016-06-15 16:34:26

标签: python neo4j py2neo

在Py2neo中,可以附加事务,然后将它们作为一个块提交给服务器

from py2neo import Graph
graph = Graph()
tx = graph.cypher.begin()
stmt1 = "CREATE (:Person {name: 'Guinevere'})"
stmt2 = "CREATE (:Person {name: 'Tom'})"
stmt3 = "CREATE (:Person {name: 'Anna'})"
tx.append(stmt)
tx.append(stmt2)
tx.append(stmt3)
tx.commit()

我似乎无法在BOLT Neo4j驱动程序手册中找到相同的语法来使其工作。附加不被识别。

driver = GraphDatabase.driver("bolt://localhost",
                                     auth=basic_auth('neo4j', 'password'),
                                     encrypted=True,
                                     trust=TRUST_ON_FIRST_USE)
session = driver.session()    
tx = session.begin_transaction()
tx.append(stmt1) --this does not work
tx.append(stmt2) --this does not work
tx.append(stmt3) --this does not work
tx.commit() --this does not work

这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

我已经尝试过,因为你在下面看到并且它有效:

from neo4j.v1 import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost")

session = driver.session()

stmt1 = "CREATE (:Person {name: 'Guinevere'})"

stmt2 = "CREATE (:Person {name: 'Tom'})"

stmt3 = "CREATE (:Person {name: 'Anna'})"

tx = session.begin_transaction()

tx.run(stmt1) # returned <neo4j.v1.session.StatementResult object at 0x7f3838f77a58>

tx.run(stmt2) # returned <neo4j.v1.session.StatementResult object at 0x7f3838f77a58>

tx.run(stmt3) # returned <neo4j.v1.session.StatementResult object at 0x7f3838f77e10>

tx.commit() # worked ;)

REF: https://neo4j.com/docs/api/python-driver/current/#example

答案 1 :(得分:0)

JRenato的回答确实是对的。我对运行和追加这个词感到困惑。我相信他们的意思略有不同。

我也简化了我的例子。我的问题出现在尝试为另一组附加语句使用相同的连接。看来你需要关闭会话以避免问题 - 否则python可能会抱怨会话仍然是开放的新的begin_transaction()