使用py2neo構建neo4j圖模型小demo
阿新 • • 發佈:2021-01-13
- 首先啟動neo4j
./neo4j console
- 連線資料庫
from py2neo import Graph, Node, Relationship
graph = Graph("http://localhost:7474", auth=("neo4j", "xxxxxxx"))
# graph.delete_all() # 刪除已有的所有內容
- 新增節點
node1 = Node('Person', name='Bob')
graph.create(node1)
node2 = Node( 'Person', name='Alice')
graph.create(node2)
node3 = Node('animal', name='cat')
graph.create(node3)
node4 = Node('animal', name='dog')
graph.create(node4)
- 新增關係
r1 = Relationship(node2, 'know', node1)
r2 = Relationship(node1, 'know', node3)
r3 = Relationship(node2, 'has', node3)
r4 = Relationship(node4, 'has', node2)
graph.create(r1)
graph.create(r2)
graph.create(r3)
graph.create(r4)