知识库

在 Neo4j 4.0 中使用 Python 1.7.x 驱动程序

在撰写本文时,针对 Neo4j 4.0 的 Python Bolt 驱动尚未发布。v4 版 Python 驱动预计将于2020年第一季度末推出。

那么,如何使用 1.7.x 系列的 Python 驱动连接 Neo4j 4.0?


最关键的一点是,在使用 1.7.x 系列驱动连接 4.0.x 数据库时,需要关闭加密通信。

下面是具体做法

from neo4j import GraphDatabase

uri = "bolt://:7687"

driver = GraphDatabase.driver(uri, auth=("neo4j", "letmein"), encrypted=False)

def print_movies_acted_in(tx, name):
    for record in tx.run("MATCH (a:Person)-[:ACTED_IN]->(m) "
                         "WHERE a.name = $name "
                         "RETURN m.title", name=name):
        print(record["m.title"])

with driver.session() as session:
    session.read_transaction(print_movies_acted_in, "Keanu Reeves")

请注意,通过 Driver 构造函数设置的 encrypted 配置。若系统支持 TLS,默认值为 True。

driver = GraphDatabase.driver(uri, auth=("neo4j", "letmein"), encrypted=False)

欲了解 Driver 对象的更多细节,请参阅以下链接 -

如预期的那样,上述代码的输出将是

The Matrix Revolutions
The Matrix Reloaded
Something's Gotta Give
The Devil's Advocate
The Replacements
Johnny Mnemonic
The Matrix
© . This site is unofficial and not affiliated with Neo4j, Inc.