知識圖譜-知識儲存-py2neo使用
使用py2neo實現python對Neo4j的操作,首先是搭建環境,選用合適的版本對實驗的成功至關重要。我選用的版本是Neo4j—3.0.7、py2neo—3.1.2、python2.7。需要注意的是如果使用預設的pip install py2neo則在進行create node時會出現報錯問題,因此需選擇合適的版本。
程式程式碼及註釋如下
# -*- coding: utf-8 -*- from py2neo import Graph, Node, Relationship, NodeSelector graph = Graph("http://localhost:7474", username="neo4j", password='password') graph.delete_all() ''' 1 —— 建立node,函式第一個引數是節點型別,第二個引數是value值 ''' a = Node('PersonTest', name='張三') b = Node('PersonTest', name='李四') r = Relationship(a, 'KNOWNS', b) s = a | b | r graph.create(s) ''' 2 —— Node查詢 ''' # 用CQL進行查詢,返回的結果是list data1 = graph.data('MATCH(p:PersonTest) return p') print "data1 = ", data1, type(data1) print # 用find_one()方法進行node查詢,返回的是查詢node的第一個node data2 = graph.find_one(label='PersonTest', property_key='name', property_value="李四") print "data2 = ", data2, type(data2) print # 用find()方法進行node查詢 data3 = graph.find(label='PersonTest') for data in data3: print "data3 = ", data print ''' 3 —— Relationship查詢 ''' relationship = graph.match_one(rel_type='KNOWNS') print relationship, type(relationship) print ''' 4 —— 更新Node的某個屬性值,若node沒有該屬性,則新增該屬性 ''' node1 = graph.find_one(label='PersonTest', property_key='name', property_value="張三") node1['age'] = 21 graph.push(node1) data4 = graph.find(label='PersonTest') for data in data4: print "data4 = ", data print node1['age'] = 99 graph.push(node1) data5 = graph.find(label='PersonTest') for data in data5: print "data5 = ", data print ''' 5 —— 刪除某node,在刪除node之前需要先刪除relationship ''' node = graph.find_one(label='PersonTest', property_key='name', property_value="李四") relationship = graph.match_one(rel_type='KNOWNS') graph.delete(relationship) graph.delete(node) data6 = graph.find(label='PersonTest') for data in data6: print "data6 = ", data ''' 6 —— 多條件查詢 ''' a = Node('PersonTest', name='張三', age=21, location='廣州') b = Node('PersonTest', name='李四', age=22, location='上海') c = Node('PersonTest', name='王五', age=21, location='北京') r1 = Relationship(a, 'KNOWS', b) r2 = Relationship(b, 'KNOWS', c) s = a | b | c | r1 | r2 graph.create(s) data7 = graph.find(label='PersonTest') for data in data7: print "data7 = ", data print # 單條件查詢,返回的是多個結果 selector = NodeSelector(graph) persons = selector.select('PersonTest', age=21) print("data8 = ", list(persons)) print # 多條件查詢 selector = NodeSelector(graph) persons = selector.select('PersonTest', age=21, location='廣州') print("data9 = ", list(persons)) print # orderby進行更復雜的查詢 selector = NodeSelector(graph) persons = selector.select('PersonTest').order_by('_.age') for data in persons: print "data10 = ", data print
改程式碼執行結果如下
data1 = [{u'p': (`張三`:PersonTest {name:"張三"})}, {u'p': (`李四`:PersonTest {name:"李四"})}] <type 'list'>
data2 = (`李四`:PersonTest {name:"李四"}) <class 'py2neo.types.Node'>
data3 = (`張三`:PersonTest {name:"張三"})
data3 = (`李四`:PersonTest {name:"李四"})(`張三`)-[:KNOWNS]->(`李四`) <class 'py2neo.types.Relationship'>
data4 = (`張三`:PersonTest {age:21,name:"張三"})
data4 = (`李四`:PersonTest {name:"李四"})data5 = (`張三`:PersonTest {age:99,name:"張三"})
data5 = (`李四`:PersonTest {name:"李四"})data7 = (`張三`:PersonTest {age:21,location:"廣州",name:"張三"})
data7 = (`李四`:PersonTest {age:22,location:"上海",name:"李四"})
data7 = (`王五`:PersonTest {age:21,location:"北京",name:"王五"})('data8 = ', [(`張三`:PersonTest {age:21,location:"廣州",name:"張三"}), (`王五`:PersonTest {age:21,location:"北京",name:"王五"})])
('data9 = ', [(`張三`:PersonTest {age:21,location:"廣州",name:"張三"})])
data10 = (`張三`:PersonTest {age:21,location:"廣州",name:"張三"})
data10 = (`王五`:PersonTest {age:21,location:"北京",name:"王五"})
data10 = (`李四`:PersonTest {age:22,location:"上海",name:"李四"})