Neo4j是一個高性能的NoSQL圖形數(shù)據(jù)庫,它可以將結(jié)構(gòu)化數(shù)據(jù)存儲在網(wǎng)絡(luò)上而不是表中
pip install py2neo
然后,你可以使用以下代碼示例連接到Neo4j數(shù)據(jù)庫并執(zhí)行一些基本操作:
from py2neo import Graph, Node, Relationship
# 連接到Neo4j數(shù)據(jù)庫
graph = Graph("http://localhost:7474", username="neo4j", password="your_password")
# 創(chuàng)建節(jié)點
person = Node("Person", name="Alice")
graph.create(person)
# 創(chuàng)建關(guān)系
graph.create(Relationship(person, "KNOWS", Node(name="Bob")))
# 查詢節(jié)點和關(guān)系
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數(shù)據(jù)庫并執(zhí)行一些基本操作:
from neo4j import GraphDatabase
# 連接到Neo4j數(shù)據(jù)庫
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驅(qū)動程序提供了更多高級功能和更好的性能。