简介 最近给孩子买了三国演义,可是三国人物关系太复杂,就想着把三国人物关系做成一张图,这样方便看,整好neo4j图数据库非常适合社交关系的处理,下面就一起来看看,如何使用neo4j生成三国人物社交关系图。
neo4j安装 现在docker很流行,也很方便,在这里,我就用docker快速搭建一个neo4j图数据库服务。
拉取neo4j镜像
[root@mysql ~]#docker pull neo4j
[root@mysql ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
neo4j latest ce22583052bf 6 days ago 546MB
创建neo4j的docker容器
[root@mysql ~]#docker run -id -d --publish=7474:7474 --publish=7687:7687 neo4j
[root@mysql ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc33fcdae7f4 neo4j "/sbin/tini -g -- /d鈥 4 hours ago Up 4 hours 0.0.0.0:7474->7474/tcp, 7473/tcp, 0.0.0.0:7687->7687/tcp hopeful_roentgen
到这里,图数据库的服务已经搭建好了,剩下的就是将数据导入。
准备三国人物数据 在这里,我已经准备好了三国演义中蜀国主要人物的关系表,将这些数据库拷贝到linux系统的a.txt文件
刘备 关羽 结义兄弟
刘备 张飞 结义兄弟
刘备 卢植 主公
刘备 马超 主公
刘备 庞统 主公
刘备 黄忠 主公
刘备 赵云 主公
刘备 魏延 主公
刘备 蒋琬 主公
刘备 法正 主公
刘备 马良 主公
刘备 孟获 主公
刘备 啥摩柯 主公
刘备 糜竺 主公
刘备 糜芳 主公
甘夫人 刘备 妻
糜夫人 刘备 次妻
孙尚香 刘备 妻
关羽 关兴 父子
关平 关羽 义子
张飞 张苞 父子
马腾 马超 父子
马腾 韩遂 异性兄弟
庞德 庞统 叔父
马谡 马良 弟弟
祝融 孟获 妻
糜芳 糜夫人 兄妹
糜竺 糜芳 兄弟
刘备 诸葛亮 主公
黄月英 诸葛亮 妻
黄承彦 黄月英 父女
姜维 诸葛亮 弟子
诸葛亮 诸葛瑾 兄弟
诸葛亮 诸葛瞻 父子
刘备 刘禅 父子
甘夫人 刘禅 母子
刘禅 姜维 主公
诸葛瞻 刘禅 女婿
刘备 中山王:刘胜 后代
刘胜 刘启 父子
刘启 刘辩 宗亲
刘辩 刘协 让位
使用awk获取不重复的人物名称
cat a.txt |awk '{print $1"\n"$2}'|sort|uniq > b.sql
生成neo4j图数据节点插入语句
[root@mysql ~]# cat b.sql |awk '{print "create (country:shu { name:'\''"$1"'\''});"}'
create (country:shu { name:'法正'});
create (country:shu { name:'甘夫人'});
create (country:shu { name:'关平'});
create (country:shu { name:'关兴'});
create (country:shu { name:'关羽'});
create (country:shu { name:'韩遂'});
create (country:shu { name:'黄承彦'});
create (country:shu { name:'黄月英'});
create (country:shu { name:'黄忠'});
create (country:shu { name:'姜维'});
create (country:shu { name:'蒋琬'});
create (country:shu { name:'刘备'});
create (country:shu { name:'刘辩'});
create (country:shu { name:'刘禅'});
create (country:shu { name:'刘启'});
create (country:shu { name:'刘胜'});
create (country:shu { name:'刘协'});
create (country:shu { name:'卢植'});
create (country:shu { name:'马超'});
create (country:shu { name:'马良'});
create (country:shu { name:'马谡'});
create (country:shu { name:'马腾'});
create (country:shu { name:'孟获'});
create (country:shu { name:'糜芳'});
create (country:shu { name:'糜夫人'});
create (country:shu { name:'糜竺'});
create (country:shu { name:'庞德'});
create (country:shu { name:'庞统'});
create (country:shu { name:'啥摩柯'});
create (country:shu { name:'孙尚香'});
create (country:shu { name:'魏延'});
create (country:shu { name:'张苞'});
create (country:shu { name:'张飞'});
create (country:shu { name:'赵云'});
create (country:shu { name:'诸葛瑾'});
create (country:shu { name:'诸葛亮'});
create (country:shu { name:'诸葛瞻'});
create (country:shu { name:'祝融'});
生成neo4j图数据人物关系语句
[root@mysql ~]# cat a.txt |awk '{print "match (p1:shu {name:'\''"$1"'\''}),(p2:shu{name:'\''"$2"'\''}) create (p1)- [r:rel {relation:'\''"$3"'\''}]->(p2);"}'
match (p1:shu {name:'刘备'}),(p2:shu{name:'关羽'}) create (p1)- [r:rel {relation:'结义兄弟'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'张飞'}) create (p1)- [r:rel {relation:'结义兄弟'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'卢植'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'马超'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'庞统'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'黄忠'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'赵云'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'魏延'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'蒋琬'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'法正'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'马良'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'孟获'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'啥摩柯'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'糜竺'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'糜芳'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'甘夫人'}),(p2:shu{name:'刘备'}) create (p1)- [r:rel {relation:'妻'}]->(p2);
match (p1:shu {name:'糜夫人'}),(p2:shu{name:'刘备'}) create (p1)- [r:rel {relation:'次妻'}]->(p2);
match (p1:shu {name:'孙尚香'}),(p2:shu{name:'刘备'}) create (p1)- [r:rel {relation:'妻'}]->(p2);
match (p1:shu {name:'关羽'}),(p2:shu{name:'关兴'}) create (p1)- [r:rel {relation:'父子'}]->(p2);
match (p1:shu {name:'关平'}),(p2:shu{name:'关羽'}) create (p1)- [r:rel {relation:'义子'}]->(p2);
match (p1:shu {name:'张飞'}),(p2:shu{name:'张苞'}) create (p1)- [r:rel {relation:'父子'}]->(p2);
match (p1:shu {name:'马腾'}),(p2:shu{name:'马超'}) create (p1)- [r:rel {relation:'父子'}]->(p2);
match (p1:shu {name:'马腾'}),(p2:shu{name:'韩遂'}) create (p1)- [r:rel {relation:'异性兄弟'}]->(p2);
match (p1:shu {name:'庞德'}),(p2:shu{name:'庞统'}) create (p1)- [r:rel {relation:'叔父'}]->(p2);
match (p1:shu {name:'马谡'}),(p2:shu{name:'马良'}) create (p1)- [r:rel {relation:'弟弟'}]->(p2);
match (p1:shu {name:'祝融'}),(p2:shu{name:'孟获'}) create (p1)- [r:rel {relation:'妻'}]->(p2);
match (p1:shu {name:'糜芳'}),(p2:shu{name:'糜夫人'}) create (p1)- [r:rel {relation:'兄妹'}]->(p2);
match (p1:shu {name:'糜竺'}),(p2:shu{name:'糜芳'}) create (p1)- [r:rel {relation:'兄弟'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'诸葛亮'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'黄月英'}),(p2:shu{name:'诸葛亮'}) create (p1)- [r:rel {relation:'妻'}]->(p2);
match (p1:shu {name:'黄承彦'}),(p2:shu{name:'黄月英'}) create (p1)- [r:rel {relation:'父女'}]->(p2);
match (p1:shu {name:'姜维'}),(p2:shu{name:'诸葛亮'}) create (p1)- [r:rel {relation:'弟子'}]->(p2);
match (p1:shu {name:'诸葛亮'}),(p2:shu{name:'诸葛瑾'}) create (p1)- [r:rel {relation:'兄弟'}]->(p2);
match (p1:shu {name:'诸葛亮'}),(p2:shu{name:'诸葛瞻'}) create (p1)- [r:rel {relation:'父子'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'刘禅'}) create (p1)- [r:rel {relation:'父子'}]->(p2);
match (p1:shu {name:'甘夫人'}),(p2:shu{name:'刘禅'}) create (p1)- [r:rel {relation:'母子'}]->(p2);
match (p1:shu {name:'刘禅'}),(p2:shu{name:'姜维'}) create (p1)- [r:rel {relation:'主公'}]->(p2);
match (p1:shu {name:'诸葛瞻'}),(p2:shu{name:'刘禅'}) create (p1)- [r:rel {relation:'女婿'}]->(p2);
match (p1:shu {name:'刘备'}),(p2:shu{name:'刘胜'}) create (p1)- [r:rel {relation:'后代'}]->(p2);
match (p1:shu {name:'刘胜'}),(p2:shu{name:'刘启'}) create (p1)- [r:rel {relation:'父子'}]->(p2);
match (p1:shu {name:'刘启'}),(p2:shu{name:'刘辩'}) create (p1)- [r:rel {relation:'宗亲'}]->(p2);
match (p1:shu {name:'刘辩'}),(p2:shu{name:'刘协'}) create (p1)- [r:rel {relation:'让位'}]->(p2);
** 执行插入语句** 将上面生成的节点和关系语句在neo4j中执行,就可以了。然后查询,就可以看到效果图了