1. 程式人生 > 其它 >使用py2neo構建neo4j圖模型小demo

使用py2neo構建neo4j圖模型小demo

技術標籤:自然語言處理neo4j

  1. 首先啟動neo4j
./neo4j console
  1. 連線資料庫
from py2neo import Graph, Node, Relationship

graph = Graph("http://localhost:7474", auth=("neo4j", "xxxxxxx"))
# graph.delete_all()  # 刪除已有的所有內容
  1. 新增節點
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)
  1. 新增關係
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)