Python編程:py2neo操作neo4j圖數(shù)據(jù)庫(kù)
py2neo文檔: https://py2neo.org/v4/index.html
安裝:
pip install py2neo
1
本文用的版本是:
py2neo 4.1.3
代碼示例
# -*- coding: utf-8 -*- from py2neo import Graph, Node, Relationship, NodeMatcher from py2neo.matching import RelationshipMatcher # 連接數(shù)據(jù)庫(kù) graph = Graph("http://localhost:7474", username="neo4j", password='123456') # 創(chuàng)建節(jié)點(diǎn) p1 = Node("Person", name="張三") p2 = Node("Person", name="李四") graph.create(p1) graph.create(p2) # 創(chuàng)建關(guān)系 r = Relationship(p1, "認(rèn)識(shí)", p2) graph.create(r) # 節(jié)點(diǎn)查詢 mather = NodeMatcher(graph) result = mather.match("Person", name="張三").first() print(result) # (_0:Person {name: '\u5f20\u4e09'}) # 查詢關(guān)系 relation_matcher = RelationshipMatcher(graph) ret = relation_matcher.match((result,), r_type="認(rèn)識(shí)").first() print(ret) # (張三)-[:認(rèn)識(shí) {}]->(李四) # 直接運(yùn)行cypher語(yǔ)句 ret = graph.run('match(p:Person{name:"李四"}) return p').data() print(ret) # [{'p': (_3:Person {name: '\u674e\u56db'})}]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Python 數(shù)據(jù)庫(kù)
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。