暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

OrientDB之关系图谱详解

左羊公社 2020-02-21
525

每日金句

年少时吃了亏,满心委屈都恨不得跟身边人说个遍,只想被捧着护着,人人怜惜着安抚着;如今吃了亏,却尽是遮着掩着,因为心里明白,每个亏里都藏着笑话,露着软肋,生怕被旁人瞧了去,抓了把柄,磨了刀子。(by 林朵)

左羊以王者荣耀(GloryOfKings)中简单的人物关系为例,描述如何使用orientDB图数据库创建关系图谱。

 

首先大家因该都了解OrientDB基本操作,不了解的话可以去看左羊的前两篇文章OrientDB之安装AND基本操作》《orientdb系列之常用语句,让我们踏入王者关系的海洋吧!

 

首先我们创建一个名为(GloryOfKings)的数据库

    create database remote:localhost/GloryOfKings root root

    连接数据库

      connetc remote:localhost/GloryOfKings root root

      类/关系梳理

      OrientDB数据库不仅是一个文档数据库,而且是一个图形数据库。使用诸如Vertex和Edge的新概念以图形的形式存储数据。它在顶点上应用多态性。概念Edge在某些属性的帮助下像顶点之间的关系。边和顶点是图形数据库的主要组件。它在边上应用多态性。顶点的基类是V。边缘的基类是E。

        V 顶点(实体)
        person(英雄):周瑜,大乔,小乔,刘备,张飞,曹操
        Occupation(职业):法师,战士,肉辅,法辅
        camp(阵营):魏国,蜀国,吴国


        E 边缘(关系):
        spouse(夫妻),Sisters(姐妹),Brother(兄弟),
        ,hostile(敌对),SecretLove(暗恋),ascription(归属),job(工作)

        创建实体类(大致相当于MySQL的建表)

          create class Person extends V
          create class Occupation extends V
          create class camp extends V
          create class spouse extends E
          create class Sisters extends E
          create class Brother extends E
          create class hostile extends E
          create class SecretLove extends E
          create class ascription extends E
          create class job extends E

          我们登录管理页面看下是否创建成功

          创建Vertex

          英雄实体

            create vertex Person set name="周瑜",sex="男",camp="吴国",Occupation="法师",title="周瑜年第逾壮,方可有为,乃以意气之未除,遽致短命,不无可惜。"
            create vertex Person set name="大乔",sex="女",camp="吴国",Occupation="法辅",title="折戟沉沙铁未销,自将磨洗认前朝。"
            create vertex Person set name="小乔",sex="女",camp="吴国",Occupation="法师",title="东风不与周郎便,铜雀春深锁二乔。"
            create vertex Person set name="刘备",sex="男",camp="蜀国",Occupation="战士",title="雄姿杰出,有王霸之略,吾敬刘玄德。"
            create vertex Person set name="张飞",sex="男",camp="蜀国",Occupation="肉辅",title="蜀之诸将,惟飞最雄。"
            create vertex Person set name="曹操",sex="男",camp="魏国",Occupation="战士",title="对酒当歌,人生几何!譬如朝露,去日苦多。"

            在这里报了如下错误!原因标点中英文符号问题,大家要注意代码中的中英文符号

            执行结果

            阵营实体

              create vertex camp set name="吴国",address="中国东南部"
              create vertex camp set name="蜀国",address="中国西南部"
              create vertex camp set name="魏国",address="中国北部"

              执行结果

              职业实体

                create vertex Occupation set name="法师",title="法术伤害"
                create vertex Occupation set name="战士",title="物理伤害"
                create vertex Occupation set name="肉辅",title="抗揍型辅助"
                create vertex Occupation set name="法辅",title="法术型辅助"

                执行结果

                 

                创建Edge

                归属关系实体

                  ascription(归属)
                  create edge ascription from(select from Person where name="周瑜") to(select from camp where name="吴国")
                  create edge ascription from(select from Person where name="大乔") to(select from camp where name="吴国")
                  create edge ascription from(select from Person where name="小乔") to(select from camp where name="吴国")
                  create edge ascription from(select from Person where name="刘备") to(select from camp where name="蜀国")
                  create edge ascription from(select from Person where name="张飞") to(select from camp where name="蜀国")
                  create edge ascription from(select from Person where name="曹操") to(select from camp where name="魏国")

                  人物关系实体

                    spouse(夫妻)
                    create edge spouse from(select from Person where name="周瑜") to (select from Person where name="小乔")
                    create edge spouse from(select from Person where name="小乔") to (select from Person where name="周瑜")

                      Sisters(姐妹)
                      create edge Sisters from(select from Person where name="小乔") to (select from Person where name="大乔")
                      create edge Sisters from(select from Person where name="大乔") to (select from Person where name="小乔")

                        Brother(兄弟)
                        create edge Brother from(select from Person where name="刘备") to (select from Person where name="张飞")
                        create edge Brother from(select from Person where name="张飞") to (select from Person where name="刘备")

                          SecretLove(暗恋)
                          create edge SecretLove from(select from Person where name="曹操") to (select from Person where name="大乔")
                          create edge SecretLove from(select from Person where name="曹操") to (select from Person where name="小乔")

                          铜雀春深锁二乔是一个远大的理想!

                          阵营关系实体

                            hostile(敌对)
                            create edge hostile from(select from camp where name="吴国") to (select from camp where name="蜀国")
                            create edge hostile from(select from camp where name="吴国") to (select from camp where name="魏国")
                            create edge hostile from(select from camp where name="魏国") to (select from camp where name="吴国")
                            create edge hostile from(select from camp where name="魏国") to (select from camp where name="蜀国")
                            create edge hostile from(select from camp where name="蜀国") to (select from camp where name="吴国")
                            create edge hostile from(select from camp where name="蜀国") to (select from camp where name="魏国")

                            三足鼎立,未出茅庐,天下三分

                            职业关系

                            前面的关系看起来都是三国的事,但接下来加上职业就成了大家熟悉的王者荣耀了

                              job(工作)
                              create edge job from(select from Person where name="周瑜") to(select from Occupation where name="法师")
                              create edge job from(select from Person where name="大乔") to(select from Occupation where name="法辅")
                              create edge job from(select from Person where name="小乔") to(select from Occupation where name="法师")
                              create edge job from(select from Person where name="刘备") to(select from Occupation where name="战士")
                              create edge job from(select from Person where name="张飞") to(select from Occupation where name="肉辅")
                              create edge job from(select from Person where name="曹操") to(select from Occupation where name="战士")

                              接下来左羊会以王者荣耀关系为例继续完善这个关系图谱,会涉及到大量OrientDB的知识,左羊在这里祝大家排位一路小平坡!适当游戏,努力学习,工作顺利,日进斗金!



                              [

                              END

                              ]


                              关注左羊公社

                              发现更多精彩    



                              文章转载自左羊公社,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                              评论