美文网首页
neo4j 从零开始

neo4j 从零开始

作者: zywang_amss | 来源:发表于2018-05-31 13:16 被阅读0次

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 内操作。一台电脑同时只能打开一个 neo4j 数据库。

远程访问 neo4j

利用端口转发,需要同时 tunnel 7474 端口(浏览器页面)和 7687 端口(数据库访问)。

备份与读取整个数据库

无论备份 与 还原 都必须关闭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

系统控制

:server change-password // 修改密码
:server disconnect // 断开连接
:sysinfo // 这里 ID Allocation 意味着这些点存在过,不都现在还在

Cypher 语言入门操作

https://neo4j.com/developer/guide-neo4j-browser/

MATCH (n) RETURN n limit 1000 // 返回至多 1000 个点
:config initialNodeDisplay: 1000 // 图中一次显示 1000 个点(越大越慢)
MATCH (n) RETURN count(*) // 返回点的数目
MATCH (n)-[r]->() RETURN COUNT(r) // 返回边的数目
// 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 类型节点的子节点,关系类型为 next,关系的 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( # 连接数据库
    "http://localhost:7474", 
    username="neo4j", 
    password="neo4j"
)
selector = NodeSelector(link_graph) # 建立数据库查询器
# link_graph.delete_all() # !清空数据库!

ctg1 = Node("contig", name="contig1_f") 
link_graph.create(ctg1) # 创建节点

ctg1 = selector.select("contig", name="contig1_f").first() # 选择已有节点
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) # 创建有向边

注意

我们需要考虑 neo4j 是用来分析数据还是展示数据。
考虑到 Python 中 networkx 进行图的表示已经很不错,我更倾向于将 neo4j 作为一个可视化的工具,而不是用它和 Cypher 代替 Python 进行数据分析。(也许经过深入学习 Cypher 后,可以用 neo4j 处理数据,但现在直接用 Python 进行 DFS 这种图算法更加简单。)

TODO

使用 neovis.js 显示权重

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

相关文章

网友评论

      本文标题:neo4j 从零开始

      本文链接:https://www.haomeiwen.com/subject/dqjqsftx.html