HugeGraph是一款易用、高效、通用的开源图数据库系统(Graph Database,GitHub项目地址), 实现了Apache TinkerPop3框架及完全兼容Gremlin查询语言, 具备完善的工具链组件,助力用户轻松构建基于图数据库之上的应用和产品。

HugeGraph-Studio:HugeGraph-Studio是HugeGraph的Web可视化工具,可用于执行Gremlin语句及展示图。

本文详细介绍在HugeGraph-Studio可视化界面上执行Gremlin语句构建红楼梦贾府人物关系知识图谱的实例。

1、添加属性:人物的姓名、性别、备注,根源的名称

graph.schema().propertyKey("姓名").asText().ifNotExist().create()
graph.schema().propertyKey("性别").asText().ifNotExist().create()
graph.schema().propertyKey("备注").asText().ifNotExist().create()
graph.schema().propertyKey("名称").asText().ifNotExist().create()

2、添加顶点类型VertexLabel:人物类型、根源类型

person = graph.schema().vertexLabel("人物").properties("姓名", "性别", "备注").primaryKeys("姓名").ifNotExist().create()
root = graph.schema().vertexLabel("根源").properties("名称", "备注").primaryKeys("名称").ifNotExist().create()

3、添加边类型(EdgeLabel):人物关系:夫妻、子女、小妾、丫鬟等,根源关系:继承

spouse = graph.schema().edgeLabel("夫妻").sourceLabel("人物").targetLabel("人物").ifNotExist().create()
children = graph.schema().edgeLabel("子女").sourceLabel("人物").targetLabel("人物").ifNotExist().create()
concubine = graph.schema().edgeLabel("小妾").sourceLabel("人物").targetLabel("人物").ifNotExist().create()
servant = graph.schema().edgeLabel("丫鬟").sourceLabel("人物").targetLabel("人物").ifNotExist().create()
extend = graph.schema().edgeLabel("继承").sourceLabel("根源").targetLabel("人物").ifNotExist().create()

4、添加顶点Vertex:根源、人物

jiajia = graph.addVertex(T.label, "根源", "名称", "贾家", "备注", "贾家血缘")
jiayan = graph.addVertex(T.label, "人物", "姓名", "贾演", "性别", "男", "备注", "宁国公")
jiadaihua = graph.addVertex(T.label, "人物", "姓名", "贾代化", "性别", "男", "备注", "")
jiafu = graph.addVertex(T.label, "人物", "姓名", "贾敷", "性别", "男", "备注", "早亡")
jiajing = graph.addVertex(T.label, "人物", "姓名", "贾敬", "性别", "男", "备注", "离家弃子,修道炼丹")
jiazhen = graph.addVertex(T.label, "人物", "姓名", "贾珍", "性别", "男", "备注", "贾府之族长")
jiarong = graph.addVertex(T.label, "人物", "姓名", "贾蓉", "性别", "男", "备注", "")
jiaqiang = graph.addVertex(T.label, "人物", "姓名", "贾蔷", "性别", "男", "备注", "贾珍养子")
jiaxichun = graph.addVertex(T.label, "人物", "姓名", "贾惜春", "性别", "女", "备注", "金陵十二钗之一,出家")
youshi = graph.addVertex(T.label, "人物", "姓名", "尤氏", "性别", "女", "备注", "贾珍继室")
qinkeqing = graph.addVertex(T.label, "人物", "姓名", "秦可卿", "性别", "女", "备注", "金陵十二钗之一")
ruizhu = graph.addVertex(T.label, "人物", "姓名", "瑞珠", "性别", "女", "备注", "")
baozhu = graph.addVertex(T.label, "人物", "姓名", "宝珠", "性别", "女", "备注", "")
ruhua = graph.addVertex(T.label, "人物", "姓名", "入画", "性别", "女", "备注", "")
caiping = graph.addVertex(T.label, "人物", "姓名", "彩屏", "性别", "女", "备注", "")
caier = graph.addVertex(T.label, "人物", "姓名", "彩儿", "性别", "女", "备注", "")
jiayuan = graph.addVertex(T.label, "人物", "姓名", "贾源", "性别", "男", "备注", "荣国公")
jiadaishan = graph.addVertex(T.label, "人物", "姓名", "贾代善", "性别", "男", "备注", "")
shitaijun = graph.addVertex(T.label, "人物", "姓名", "史太君", "性别", "女", "备注", "贾母")
jiashe = graph.addVertex(T.label, "人物", "姓名", "贾赦", "性别", "男", "备注", "")
xingfuren = graph.addVertex(T.label, "人物", "姓名", "邢夫人", "性别", "女", "备注", "续弦")
jialian = graph.addVertex(T.label, "人物", "姓名", "贾琏", "性别", "男", "备注", "")
jiacong = graph.addVertex(T.label, "人物", "姓名", "贾琮", "性别", "男", "备注", "")
jiayingchun = graph.addVertex(T.label, "人物", "姓名", "贾迎春", "性别", "女", "备注", "金陵十二钗之一")
wangxifeng = graph.addVertex(T.label, "人物", "姓名", "王熙凤", "性别", "女", "备注", "金陵十二钗之一")
pinger = graph.addVertex(T.label, "人物", "姓名", "平儿", "性别", "女", "备注", "王熙凤的陪嫁婢女")
youerjie = graph.addVertex(T.label, "人物", "姓名", "尤二姐", "性别", "女", "备注", "王夫人")
qiutong = graph.addVertex(T.label, "人物", "姓名", "秋桐", "性别", "女", "备注", "原贾赦丫头")
jiaqiaojie = graph.addVertex(T.label, "人物", "姓名", "贾巧姐", "性别", "女", "备注", "金陵十二钗之一")
siqi = graph.addVertex(T.label, "人物", "姓名", "司棋", "性别", "女", "备注", "")
xiuju = graph.addVertex(T.label, "人物", "姓名", "绣橘", "性别", "女", "备注", "王熙凤的陪嫁婢女")
lianhuaer = graph.addVertex(T.label, "人物", "姓名", "莲花儿", "性别", "女", "备注", "")
jiazheng = graph.addVertex(T.label, "人物", "姓名", "贾政", "性别", "男", "备注", "")
wangfuren = graph.addVertex(T.label, "人物", "姓名", "王夫人", "性别", "女", "备注", "")
zhaoyiniang = graph.addVertex(T.label, "人物", "姓名", "赵姨娘", "性别", "女", "备注", "")
zhouyiniang = graph.addVertex(T.label, "人物", "姓名", "周姨娘", "性别", "女", "备注", "")
jiazhu = graph.addVertex(T.label, "人物", "姓名", "贾珠", "性别", "男", "备注", "早亡")
jiabaoyu = graph.addVertex(T.label, "人物", "姓名", "贾宝玉", "性别", "男", "备注", "出家")
xuebaochai = graph.addVertex(T.label, "人物", "姓名", "薛宝钗", "性别", "女", "备注", "金陵十二钗之一")
jiahuan = graph.addVertex(T.label, "人物", "姓名", "贾环", "性别", "男", "备注", "赵姨娘之子")
liwan = graph.addVertex(T.label, "人物", "姓名", "李纨", "性别", "女", "备注", "金陵十二钗之一")
jiayuanchun = graph.addVertex(T.label, "人物", "姓名", "贾元春", "性别", "女", "备注", "加封贤德妃,金陵十二钗之一")
jiatanchun = graph.addVertex(T.label, "人物", "姓名", "贾探春", "性别", "女", "备注", "赵姨娘之女,,金陵十二钗之一")
jialan = graph.addVertex(T.label, "人物", "姓名", "贾兰", "性别", "男", "备注", "")
jiamin = graph.addVertex(T.label, "人物", "姓名", "贾敏", "性别", "女", "备注", "")
linruhai = graph.addVertex(T.label, "人物", "姓名", "林如海", "性别", "男", "备注", "")
lindaiyu = graph.addVertex(T.label, "人物", "姓名", "林黛玉", "性别", "女", "备注", "金陵十二钗之一")
xiren = graph.addVertex(T.label, "人物", "姓名", "袭人", "性别", "女", "备注", "")
qingwen = graph.addVertex(T.label, "人物", "姓名", "晴雯", "性别", "女", "备注", "")
sheyue = graph.addVertex(T.label, "人物", "姓名", "麝月", "性别", "女", "备注", "")
qiuwen = graph.addVertex(T.label, "人物", "姓名", "秋纹", "性别", "女", "备注", "")
shishu = graph.addVertex(T.label, "人物", "姓名", "侍书", "性别", "女", "备注", "")
cuimo = graph.addVertex(T.label, "人物", "姓名", "翠墨", "性别", "女", "备注", "")
xiaochan = graph.addVertex(T.label, "人物", "姓名", "小婵", "性别", "女", "备注", "")
zhijuan = graph.addVertex(T.label, "人物", "姓名", "紫鹃", "性别", "女", "备注", "")
xueyan = graph.addVertex(T.label, "人物", "姓名", "雪雁", "性别", "女", "备注", "")
chunxian = graph.addVertex(T.label, "人物", "姓名", "春纤", "性别", "女", "备注", "")
baoqin = graph.addVertex(T.label, "人物", "姓名", "抱琴", "性别", "女", "备注", "")
qingyun = graph.addVertex(T.label, "人物", "姓名", "青芸", "性别", "女", "备注", "")
qinyun = graph.addVertex(T.label, "人物", "姓名", "琴韵", "性别", "女", "备注", "")

5、添加边Edge,关系

jiajia.addEdge("继承", jiayan)
jiajia.addEdge("继承", jiayuan)
jiayan.addEdge("子女", jiadaihua)
jiayuan.addEdge("子女", jiadaishan)
jiadaihua.addEdge("子女", jiafu)
jiadaihua.addEdge("子女", jiajing)
jiajing.addEdge("子女", jiazhen)
jiajing.addEdge("子女", jiaxichun)
jiazhen.addEdge("子女", jiaqiang)
jiazhen.addEdge("子女", jiarong)
jiazhen.addEdge("夫妻", youshi)
jiarong.addEdge("夫妻", qinkeqing)
jiarong.addEdge("丫鬟", ruizhu)
jiarong.addEdge("丫鬟", baozhu)
jiaxichun.addEdge("丫鬟", ruhua)
jiaxichun.addEdge("丫鬟", caiping)
jiaxichun.addEdge("丫鬟", caier)
jiadaishan.addEdge("子女", jiashe)
jiadaishan.addEdge("子女", jiazheng)
jiadaishan.addEdge("子女", jiamin)
jiadaishan.addEdge("夫妻", shitaijun)
jiashe.addEdge("子女", jialian)
jiashe.addEdge("子女", jiacong)
jiashe.addEdge("子女", jiayingchun)
jiashe.addEdge("夫妻", xingfuren)
jialian.addEdge("子女", jiaqiaojie)
jialian.addEdge("夫妻", wangxifeng)
jialian.addEdge("小妾", pinger)
jialian.addEdge("小妾", youerjie)
jialian.addEdge("小妾", qiutong)
jiayingchun.addEdge("丫鬟", siqi)
jiayingchun.addEdge("丫鬟", xiuju)
jiayingchun.addEdge("丫鬟", lianhuaer)

jiazheng.addEdge("子女", jiazhu)
jiazheng.addEdge("子女", jiayuanchun)
jiazheng.addEdge("子女", jiabaoyu)
jiazheng.addEdge("子女", jiatanchun)
jiazheng.addEdge("子女", jiahuan)
jiazheng.addEdge("夫妻", wangfuren)
jiazheng.addEdge("小妾", zhaoyiniang)
jiazheng.addEdge("小妾", zhouyiniang)
jiazhu.addEdge("子女", jialan)
jiazhu.addEdge("夫妻", liwan)
jiayuanchun.addEdge("丫鬟", baoqin)
jiayuanchun.addEdge("丫鬟", qingyun)
jiayuanchun.addEdge("丫鬟", qinyun)
jiabaoyu.addEdge("夫妻", xuebaochai)
jiabaoyu.addEdge("丫鬟", xiren)
jiabaoyu.addEdge("丫鬟", qingwen)
jiabaoyu.addEdge("丫鬟", sheyue)
jiabaoyu.addEdge("丫鬟", qiuwen)
jiatanchun.addEdge("丫鬟", shishu)
jiatanchun.addEdge("丫鬟", cuimo)
jiatanchun.addEdge("丫鬟", xiaochan)
jiamin.addEdge("夫妻", linruhai)
jiamin.addEdge("子女", lindaiyu)
lindaiyu.addEdge("丫鬟", zhijuan)
lindaiyu.addEdge("丫鬟", xueyan)
lindaiyu.addEdge("丫鬟", chunxian)

第4步和第5步的语句需要一起执行。

添加成功后的效果图:

 

可视化界面上还可按表格查看所有人物数据,或者查看人物关系知识图谱的json格式数据。

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐