Neo4j是一個高性能的NoSQL圖形數據庫,它可以將結構化數據存儲在網絡上而不是表中
pip install py2neo
然后,你可以使用以下代碼示例連接到Neo4j數據庫并執行一些基本操作:
from py2neo import Graph, Node, Relationship
# 連接到Neo4j數據庫
graph = Graph("http://localhost:7474", username="neo4j", password="your_password")
# 創建節點
person = Node("Person", name="Alice")
graph.create(person)
# 創建關系
graph.create(Relationship(person, "KNOWS", Node(name="Bob")))
# 查詢節點和關系
for node in graph.nodes.match("Person"):
print(node["name"])
for rel in graph.relationships.match("KNOWS"):
print(rel.start_node["name"], rel.end_node["name"])
pip install neo4j
然后,你可以使用以下代碼示例連接到Neo4j數據庫并執行一些基本操作:
from neo4j import GraphDatabase
# 連接到Neo4j數據庫
uri = "bolt://localhost:7687"
user = "neo4j"
password = "your_password"
driver = GraphDatabase.driver(uri, auth=(user, password))
def create_person(tx, name):
query = "CREATE (p:Person {name: $name}) RETURN p"
result = tx.run(query, name=name)
return result.single()[0]
def main():
with driver.session() as session:
person = session.read_transaction(create_person, "Alice")
print(person["name"])
if __name__ == "__main__":
main()
這兩個庫都可以滿足你的需求,具體選擇哪個庫取決于你的項目需求和個人喜好。Py2neo的API更簡單,易于上手,而Neo4j驅動程序提供了更多高級功能和更好的性能。