以笑傲江湖(SwordsMan)中简单的人物关系为例,描述如何利用orientDB图数据库表达构建知识图谱
这里默认大家都是了解OrientDB基本语法,了解Nosql和图形数据库的小伙伴,不了解的可以自行查阅资料。废话不多说,我们开始吧。
创建一个名为SwordsMan的数据库:
create database plocal:/home/wangtd/local/orientdb/databases/SwordsMan #登录操作 connect plocal:/home/wangtd/local/orientdb/databases/SwordsMan admin admin
实体/关系梳理
我们假定一个简单的实体、关系模型,其中包括点(Vertex)和边(Edge),这两种类型是OrientDB中图数据库的重要类型。
- 实体:
人物(令狐冲,岳不群,宁中则,任盈盈)
教派(华山派,日月神教)
功夫(华山剑法,独孤九剑,玉女十三剑)- 关系:情侣,师徒,门徒,技能
创建类对象
create class Person extends V create class Group extends V create class KungFu extends V create class Lover extends E create class Student extends E create class Teacher extends E create class Disciple extends E create class Skill extends E
创建Vertex/Edge
创建基本实体
create vertex Person set name="令狐冲",sex="男",age="25",group="华山派",kungfu="独孤九剑" create vertex Person set name="任盈盈",sex="女",age="18",group="日月神教",kungfu="长经双刀" create edge Lover from(select from Person where name="令狐冲") to (select from Person where name="任盈盈") create edge Lover from(select from Person where name="任盈盈") to (select from Person where name="令狐冲") create vertex Person set name="岳不群",age="40",kungfu="辟邪剑谱" create vertex Person set name="宁中则",age="35",kungfu="玉女十三剑" create edge Lover from(select from Person where name="岳不群") to (select from Person where name="宁中则") create edge Lover from(select from Person where name="宁中则") to (select from Person where name="岳不群")
创建边关系
create vertex Group set name="华山派",addr="华山" create vertex Group set name="日月神教",addr="黑木崖" create edge Disciple from(select from Person where group="华山派") to (select from Group where name="华山派") create edge Disciple from(select from Person where group="日月神教") to (select from Group where name="日月神教") create edge Teacher from(select from Person where name="令狐冲") to(select from Person where name="岳不群") create edge Teacher from(select from Person where name="令狐冲") to(select from Person where name="宁中则") create edge Student from(select from Person where name="岳不群") to(select from Person where name="令狐冲") create edge Student from(select from Person where name="宁中则") to(select from Person where name="令狐冲")
更新/维护关系
create vertex KungFu set name="华山剑法" create vertex KungFu set name="玉女十三剑" create vertex KungFu set name="长经双剑" create edge Skill from(select from Person where name="令狐冲") to(select from KungFu where name="华山剑法") create edge Skill from(select from Person where name="岳不群") to(select from KungFu where name="华山剑法") create edge Skill from(select from Person where name="宁中则") to(select from KungFu where name="玉女十三剑") create edge Skill from(select from Person where name="任盈盈") to(select from KungFu where name="长经双剑")
体会
- 因为orientDB本身支持sql语法,所以数据的增删改查比较方便
- 图数据库重要的两种类型Vertex和Edge
- 构建知识图谱前,要先定义Vertex和Edge的对象,要确定出Vertex的类型有哪些,Edge的类型有哪些
- 图谱关系是以In/Out代表的双向关系,一条边定义了有向的出度和入度
方向
图谱的构建是最基础的一步,在图谱构建之后:
- 完善图谱
- 使用java api query
- 达到知识图谱应用的效果