0、概述
本文以热播电视剧《人民的名义》中的人物关系为数据基础,抛开案例本身的内容,本项目的意义在于指出使用Neo4j数据库的一般流程是什么?包括数据的导入、操作、查询、展示,从而体会出与传统数据库相比Neo4j在处理图数据的巨大威力和方便之处。
1、准备节点数据
Merge (n:People {name:"蔡成功",image:"caichenggong.png"})
Merge (n1:People {name:"欧阳青",image:"ouyangqing.png"})
Merge (n2:People {name:"李达康",image:"lidakang.png"})
Merge (n3:People {name:"沙瑞金",image:"sharuijin.png"})
Merge (n4:People {name:"高玉良",image:"gaoyuliang.png"})
Merge (n5:People {name:"侯亮平",image:"houliangping.png"})
Merge (n6:People {name:"赵东来",image:"zhaodonglai.png"})
Merge (n7:People {name:"陆亦可",image:"luyike.png"})
Merge (n8:People {name:"周正",image:"zhouzheng.png"})Merge (n110:People {name:"陈海",image:"chenhai.png"})
Merge (n111:People {name:"祁同伟",image:"qitongwei.png"})
Merge (n112:People {name:"陈岩石",image:"chenyanshi.png"})
Merge (n113:People {name:"高小琴",image:"gaoxiaoqin.png"})
Merge (n114:People {name:"欧阳青",image:"ouyangqing.png"})
Merge (n115:People {name:"丁义诊",image:"dingyizhen.png"})
Merge (n116:People {name:"王大路",image:"wangdalu.png"})
Merge (n117:People {name:"高玉良",image:"gaoyuliang.png"})
Merge (n118:People {name:"吴慧芬",image:"wuhuifen.png"})
Merge (n119:People {name:"钟小艾",image:"zhongxiaoai.png"})
Merge (n1110:People {name:"林华华",image:"linhuahua.png"})
Merge (n11a:People {name:"王馥真",image:"wangfuzhen.png"})
Merge (n11v:People {name:"季昌明",image:"jichangming.png"})
Merge (n11c:People {name:"陈阳",image:"chenyang.png"})
Merge (n11s:People {name:"梁璐",image:"lianglu.png"})
Merge (n11w:People {name:"高小琴",image:"gaoxiaoqin.png"})
Merge (n11d:People {name:"郑西坡",image:"zhenxipo.png"})
Merge (n11g:People {name:"高小凤",image:"gaoxiaofeng.png"})
2、准备关系数据
# 逐行粘贴执行
Match (n:People {name:"蔡成功"}),(b:People {name:"欧阳青"}) Merge(n)-[:贿赂]->(b)
Match (n:People {name:"蔡成功"}),(b:People {name:"侯亮平"}) Merge(n)-[:发小]->(b)
Match (n:People {name:"欧阳青"}),(b:People {name:"丁义诊"}) Merge(n)-[:合作]->(b)
Match (n:People {name:"欧阳青"}),(b:People {name:"王大路"}) Merge(n)-[:朋友]->(b)
Match (n:People {name:"欧阳青"}),(b:People {name:"李达康"}) Merge(n)-[:夫妻]->(b) Match (n:People {name:"李达康"}),(b:People {name:"王大路"}) Merge(n)-[:前同事]->(b)
Match (n:People {name:"李达康"}),(b:People {name:"丁义诊"}) Merge(n)-[:下属]->(b)
Match (n:People {name:"李达康"}),(b:People {name:"高玉良"}) Merge(n)-[:同事]->(b)
Match (n:People {name:"沙瑞金"}),(b:People {name:"高玉良"}) Merge(n)-[:下属]->(b)
Match (n:People {name:"沙瑞金"}),(b:People {name:"李达康"}) Merge(n)-[:下属]->(b)
Match (n:People {name:"沙瑞金"}),(b:People {name:"祁同伟"}) Merge(n)-[:下属]->(b)
Match (n:People {name:"高玉良"}),(b:People {name:"祁同伟"}) Merge(n)-[:学生]->(b)
Match (n:People {name:"高玉良"}),(b:People {name:"侯亮平"}) Merge(n)-[:学生]->(b)
Match (n:People {name:"高玉良"}),(b:People {name:"陈海"}) Merge(n)-[:学生]->(b)
Match (n:People {name:"高玉良"}),(b:People {name:"吴慧芬"}) Merge(n)-[:夫妻]->(b)
Match (n:People {name:"侯亮平"}),(b:People {name:"钟小艾"}) Merge(n)-[:夫妻]->(b)
Match (n:People {name:"侯亮平"}),(b:People {name:"陈海"}) Merge(n)-[:同学]->(b)
Match (n:People {name:"侯亮平"}),(b:People {name:"陆亦可"}) Merge(n)-[:下属]->(b)
Match (n:People {name:"侯亮平"}),(b:People {name:"赵东来"}) Merge(n)-[:办案]->(b)
Match (n:People {name:"赵东来"}),(b:People {name:"陆亦可"}) Merge(n)-[:喜欢]->(b)
Match (n:People {name:"陆亦可"}),(b:People {name:"陈海"}) Merge(n)-[:喜欢]->(b)
Match (n:People {name:"陆亦可"}),(b:People {name:"周正"}) Merge(n)-[:下属]->(b)
Match (n:People {name:"陆亦可"}),(b:People {name:"林华华"}) Merge(n)-[:下属]->(b)
Match (n:People {name:"周正"}),(b:People {name:"林华华"}) Merge(n)-[:情侣]->(b)
Match (n:People {name:"陈海"}),(b:People {name:"陈岩石"}) Merge(n)-[:父亲]->(b)
Match (n:People {name:"陈海"}),(b:People {name:"王馥真"}) Merge(n)-[:母亲]->(b)
Match (n:People {name:"陈海"}),(b:People {name:"季昌明"}) Merge(n)-[:上级]->(b)
Match (n:People {name:"陈海"}),(b:People {name:"陈阳"}) Merge(n)-[:姐姐]->(b)
Match (n:People {name:"祁同伟"}),(b:People {name:"陈阳"}) Merge(n)-[:初恋]->(b)
Match (n:People {name:"祁同伟"}),(b:People {name:"梁璐"}) Merge(n)-[:夫妻]->(b)
Match (n:People {name:"祁同伟"}),(b:People {name:"高小琴"}) Merge(n)-[:情妇]->(b)
Match (n:People {name:"陈岩石"}),(b:People {name:"郑西坡"}) Merge(n)-[:前下属]->(b)
Match (n:People {name:"高小琴"}),(b:People {name:"高小凤"}) Merge(n)-[:姐妹]->(b)
3、人物关系分析
# 六层关系理论:世界上任何2个人的关系都不超过6层
Match p=allshortestpaths((N:People {name:'蔡成功'})-[*..6]-(m:People {name:'季昌明'})) return p
4、使用D3.js对Neo4j进行可视化
Neo4j browser 是面向数据分析人员的,Neo4j browser现成的效果是没法在Web系统里进行可视化展示或者嵌入,必须使用D3插件重新实现eo4j browser的某些功能。
具体该怎么做,Neo4j官网有相关的说明(https://neo4j.com/developer/tools-graph-visualization/#_visualization_resources),其效果图如下,(官网也是使用D3.js实现的,效果远不如Neo4j官网):
5、总结
本项目Web结构代码稍多,并且是某个培训机构的代码,这里就不方便贴出了,上面的讲课内容也大部分出自该课程。最后这个项目懂的java前端开发的容易做出来,这里的重点还是应用Neo4j自带的可视化功能,并且Neo4j本身的可视化功能已经很优秀了。