赞
踩
pip install neo4j -i https://mirror.baidu.com/pypi/simple
默认用户名和密码是neo4j
from neo4j import GraphDatabase
url = "neo4j://localhost:7687"
username = "neo4j"
password = "neo4j"
GraphDatabase.driver(url, auth=(username, password))
没报错就是连接成功
from neo4j import GraphDatabase class HelloWorldExample: def __init__(self, uri, user, password): self.driver = GraphDatabase.driver(uri, auth=(user, password)) def close(self): self.driver.close() def clear_data(self): def work(tx): tx.run(""" MATCH (n) DETACH DELETE n """) with self.driver.session() as session: session.write_transaction(work) def create_node(self): def person_work(tx): tx.run("""create(p:Person{name: "小明"})""") tx.run("""create(p:Person{name: "小美"})""") with self.driver.session() as session: session.write_transaction(person_work) def create_relationship(self): def work(tx): tx.run("""MATCH (a:Person {name:'小明'}), (b:Person {name:'小美'}) MERGE (a)-[:爱]->(b)""") with self.driver.session() as session: session.write_transaction(work) def search(self, message): with self.driver.session() as session: greeting = session.write_transaction(self._work_search, message) print(greeting) @staticmethod def _work_search(tx, userName): result = tx.run("match(p:Person{name:$userName})-[:爱]-(whom) return whom.name", userName=userName) return result.single()[0] url = "neo4j://localhost:7687" username = "neo4j" password = "neo4j" app = HelloWorldExample(url, username, password) app.clear_data() app.create_node() app.create_relationship() app.search('小明') app.close()
如下图,实体和关系已建好,可以进行查询等操作
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。