neo4j 從零開始
neo4j 簡介
http://www.durusau.net/localcopy/Graph-Modeling-Do.s-and-Don.ts.pdf
https://www.youtube.com/watch?v=78r0MgH0u0w
https://www.youtube.com/watch?v=AaJS-DGBQX4
https://www.youtube.com/watch?v=GekQqFZm7mA
開始
red hat 安裝 neo4j
https://neo4j.com/docs/operations-manual/current/installation/linux/rpm/
命令行啟動
neo4j console
建議用 root赂苗,screen 內(nèi)操作暇昂。一臺電腦同時只能打開一個 neo4j 數(shù)據(jù)庫怯伊。
遠程訪問 neo4j
利用端口轉(zhuǎn)發(fā)载萌,需要同時 tunnel 7474 端口(瀏覽器頁面)和 7687 端口(數(shù)據(jù)庫訪問)懂傀。
備份與讀取整個數(shù)據(jù)庫
無論備份 與 還原 都必須關(guān)閉Neo4j
neo4j-admin dump --database=graph.db --to=/backups/graph.db/2016-10-02.dump
neo4j-admin load --from=/backups/graph.db/2016-10-02.dump --database=graph.db --force
Browser 命令
進入 http://localhost:7474/browser/ 即可操作且警。
默認用戶名與密碼均為 neo4j荞胡,默認 server 為 bolt://localhost:7687
系統(tǒng)控制
:server change-password // 修改密碼
:server disconnect // 斷開連接
:sysinfo // 這里 ID Allocation 意味著這些點存在過茫经,不都現(xiàn)在還在
Cypher 語言入門操作
https://neo4j.com/developer/guide-neo4j-browser/
MATCH (n) RETURN n limit 1000 // 返回至多 1000 個點
:config initialNodeDisplay: 1000 // 圖中一次顯示 1000 個點(越大越慢)
MATCH (n) RETURN count(*) // 返回點的數(shù)目
MATCH (n)-[r]->() RETURN COUNT(r) // 返回邊的數(shù)目
// MATCH (n) DETACH DELETE n // 个扰!刪除圖中所有點瓷炮!
Cypher 進階
https://neo4j.com/docs/developer-manual/current/cypher/
https://gist.github.com/DaniSancas/1d5265fc159a95ff457b940fc5046887
https://neo4j.com/blog/query-cypher-data-relationships/
https://neo4j.com/blog/tuning-cypher-queries/
// 查看 name 為 12514_r 的 contig 類型節(jié)點的子節(jié)點,關(guān)系類型為 next递宅,關(guān)系的 property 中 link_num > 4. 限制返回至多 5 個 match
MATCH (n1:contig {name:"12514_r"})-[r:next]->(n2) where r.link_num>4 RETURN n1,n2 LIMIT 5
// 在以上基礎上再加一層
MATCH (n1:contig {name:"8111_r"})-[r1:next]->(n2)-[r2:next]->(n3) where r1.link_num > 5 and r2.link_num > 3 RETURN n1, n2, n3 limit 300
py2neo 使用樣例
http://py2neo.org/2.0/essentials.html
http://py2neo.org/v3/types.html
pip install py2neo
from py2neo import Graph, Node, Relationship, NodeSelector
link_graph = Graph( # 連接數(shù)據(jù)庫
"http://localhost:7474",
username="neo4j",
password="neo4j"
)
selector = NodeSelector(link_graph) # 建立數(shù)據(jù)庫查詢器
# link_graph.delete_all() # 娘香!清空數(shù)據(jù)庫!
ctg1 = Node("contig", name="contig1_f")
link_graph.create(ctg1) # 創(chuàng)建節(jié)點
ctg1 = selector.select("contig", name="contig1_f").first() # 選擇已有節(jié)點
ctg2 = Node("contig", name="contig2_f")
link_graph.create(ctg2)
# 邊的類型就是 link_num 的字符串表示办龄,這樣可以直接在 neo4j 圖中顯示烘绽,但好像難以進行 Cypher 操作
ctg1_ctg2 = Relationship(ctg1, str(parent_child_link), ctg2)
# 或者邊的類型為 next,邊中含有更多信息俐填,可以用于 Cypher 分析
ctg1_ctg2 = Relationship(ctg1, "next", ctg2, link_num=10, gapsizes=100)
link_graph.create(ctg1_ctg2) # 創(chuàng)建有向邊
注意
我們需要考慮 neo4j 是用來分析數(shù)據(jù)還是展示數(shù)據(jù)安接。
考慮到 Python 中 networkx 進行圖的表示已經(jīng)很不錯,我更傾向于將 neo4j 作為一個可視化的工具英融,而不是用它和 Cypher 代替 Python 進行數(shù)據(jù)分析盏檐。(也許經(jīng)過深入學習 Cypher 后,可以用 neo4j 處理數(shù)據(jù)驶悟,但現(xiàn)在直接用 Python 進行 DFS 這種圖算法更加簡單胡野。)
TODO
使用 neovis.js 顯示權(quán)重
https://github.com/neo4j-contrib/neovis.js
https://www.lyonwj.com/2016/06/26/graph-of-thrones-neo4j-social-network-analysis/
https://medium.com/neo4j/hands-on-graph-data-visualization-bd1f055a492d
https://medium.com/neo4j/graph-visualization-with-neo4j-using-neovis-js-a2ecaaa7c379
https://www.youtube.com/watch?v=0-1A7f8993M