neo4j python 算法_python操作neo4j简单实例
一:neo4j是什么
neo4j:Neo4j是一個(gè)高性能的,NOSQL圖形數(shù)據(jù)庫(kù),有關(guān)于更多neo4j的資料出門(mén)右拐
二:python操作neo4j的實(shí)現(xiàn)
python中neo4j的操作可以利用驅(qū)動(dòng)包或者py2neo包,
我采用py2neo第三方包實(shí)現(xiàn)以下簡(jiǎn)單功能,先建立一個(gè)neo4j的鏈接
對(duì)于neo4j的安裝其實(shí)比較簡(jiǎn)單,這里就跳過(guò)
def__init__(self):
self.conn = Graph("http://localhost:7474",username="11111",password="11111")
節(jié)點(diǎn)和關(guān)系的創(chuàng)建
a = Node(label, **doc1)
b = Node(label, **doc2)
r2 = Relationship(b, ship, a)
self.conn.merge(r2,label=label)
更新節(jié)點(diǎn)屬性
node.update(doc)
通過(guò)關(guān)系查找相關(guān)聯(lián)的關(guān)系節(jié)點(diǎn)
selector = NodeSelector(self.conn)
query = '_.age=22'
persons = selector.select(label).where(query).order_by('_.' + order)
添加節(jié)點(diǎn)
obj = self.get(label, field, value)
tmp_obj = label()
tmp_obj.setAll(doc=doc)
obj.knows.add(tmp_obj)
print(list(obj.knows))
self.conn.push(obj)
刪除節(jié)點(diǎn),這里有坑,官方文檔說(shuō)刪除節(jié)點(diǎn)必須先刪除關(guān)系。當(dāng)節(jié)點(diǎn)大于2時(shí),比如a->b->c 這時(shí)官方文檔中的栗子有報(bào)錯(cuò)。因此自己現(xiàn)在用這種方式刪除
obj = self.get(label, src_field, src_value)
tar = self.get(label, desc_field, desc_value)
obj.knows.remove(tar)
self.conn.push(obj)
self.conn.delete(tar)
以上是python簡(jiǎn)單的neo4j的一些操作,畢竟是圖形數(shù)據(jù)庫(kù),扒了點(diǎn)數(shù)據(jù)看看效果,這里直接上圖
QQ圖片20171122132212.png
菜鳥(niǎo)路過(guò),如果不合理的地方,請(qǐng)大家多多指教
總結(jié)
以上是生活随笔為你收集整理的neo4j python 算法_python操作neo4j简单实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CSS中的text-shadow。
- 下一篇: c++ 冒泡排序_干货|python笔记