随着社交、电商、金融、零售、物联网等行业的快速发展,现实社会织起了了一张庞大而复杂的关系网,亟需一种支持海量复杂数据关系运算的数据库即图数据库。本系列文章是学习知识图谱以及图数据库相关的知识梳理与总结
本文会包含如下内容:
本篇文章适合人群:架构师、技术专家、对知识图谱与图数据库感兴趣的高级工程师
1. 希腊神话人物关系数据通过Property Graph Model图数据模型来描述希腊神话中各人物角色的关系(也被成为人物关系图谱),具体关系详见下图
推荐使用HugeGraph-Studio 通过可视化的方式来执行上述代码。另外也可以通过HugeGraph-Client、HugeApi、GremlinConsole和GremlinDriver等多种方式执行下面的代码。(包括Schema、索引及数据的创建)
但注意如下:
schema = hugegraph.schema()
schema.propertyKey("name").asText().ifNotExist().create()
schema.propertyKey("age").asInt().ifNotExist().create()
schema.propertyKey("time").asInt().ifNotExist().create()
schema.propertyKey("reason").asText().ifNotExist().create()
schema.propertyKey("type").asText().ifNotExist().create()
schema.vertexLabel("character").properties("name", "age", "type").primaryKeys("name").nullableKeys("age").ifNotExist().create()
schema.vertexLabel("location").properties("name").primaryKeys("name").ifNotExist().create()
schema.edgeLabel("father").link("character", "character").ifNotExist().create()
schema.edgeLabel("mother").link("character", "character").ifNotExist().create()
schema.edgeLabel("battled").link("character", "character").properties("time").ifNotExist().create()
schema.edgeLabel("lives").link("character", "location").properties("reason").nullableKeys("reason").ifNotExist().create()
schema.edgeLabel("pet").link("character", "character").ifNotExist().create()
schema.edgeLabel("brother").link("character", "character").ifNotExist().create()
schema.indexLabel("characterByAge").onV("character").by("age").range().ifNotExist().create();
// add vertices
Vertex saturn = graph.addVertex(T.label, "character", "name", "saturn", "age", 10000, "type", "titan")
Vertex sky = graph.addVertex(T.label, "location", "name", "sky")
Vertex sea = graph.addVertex(T.label, "location", "name", "sea")
Vertex jupiter = graph.addVertex(T.label, "character", "name", "jupiter", "age", 5000, "type", "god")
Vertex neptune = graph.addVertex(T.label, "character", "name", "neptune", "age", 4500, "type", "god")
Vertex hercules = graph.addVertex(T.label, "character", "name", "hercules", "age", 30, "type", "demigod")
Vertex alcmene = graph.addVertex(T.label, "character", "name", "alcmene", "age", 45, "type", "human")
Vertex pluto = graph.addVertex(T.label, "character", "name", "pluto", "age", 4000, "type", "god")
Vertex nemean = graph.addVertex(T.label, "character", "name", "nemean", "type", "monster")
Vertex hydra = graph.addVertex(T.label, "character", "name", "hydra", "type", "monster")
Vertex cerberus = graph.addVertex(T.label, "character", "name", "cerberus", "type", "monster")
Vertex tartarus = graph.addVertex(T.label, "location", "name", "tartarus")
// add edges
jupiter.addEdge("father", saturn)
jupiter.addEdge("lives", sky, "reason", "loves fresh breezes")
jupiter.addEdge("brother", neptune)
jupiter.addEdge("brother", pluto)
neptune.addEdge("lives", sea, "reason", "loves waves")
neptune.addEdge("brother", jupiter)
neptune.addEdge("brother", pluto)
hercules.addEdge("father", jupiter)
hercules.addEdge("mother", alcmene)
hercules.addEdge("battled", nemean, "time", 1)
hercules.addEdge("battled", hydra, "time", 2)
hercules.addEdge("battled", cerberus, "time", 12)
pluto.addEdge("brother", jupiter)
pluto.addEdge("brother", neptune)
pluto.addEdge("lives", tartarus, "reason", "no fear of death")
pluto.addEdge("pet", cerberus)
cerberus.addEdge("lives", tartarus)
查询目的 |
gremlin语句 |
统计某个顶点类型个数 |
g.V().hasLabel('character').count() |
统计某个边类型的个数 |
g.E().hasLabel('brother').count() |
按Label统计顶点数量 |
g.V().label().groupCount() g.V().group().by(label).by(count()) |
按Label统计边数量 |
g.E().label().groupCount() g.E().group().by(label).by(count()) |
统计某一顶点的边数 |
//统计pluto的兄弟数 g.V().has('character','name', 'pluto').out('brother').count() |
查询目的 |
gremlin语句 |
查找 hercules 的祖父 |
g.V().hasLabel('character').has('name','hercules').out('father').out('father') g.V().hasLabel('character').has('name','hercules').repeat(__.out('father')).times(2) |
查找 hercules 父亲的名称 |
g.V().hasLabel('character').has('name','hercules').out('father').values("name") |
查找与 pluto 生活在一起的人 |
g.V().hasLabel('character').has('name','pluto').out('lives').in('lives').values('name') |
查找与 pluto 生活在一起的人【但排除pluto本人】 |
g.V().hasLabel('character').has('name', 'pluto').as('x').out('lives').in('lives').where(neq('x')).values('name') |
查找年龄大于 100 岁的人物 |
g.V().hasLabel('character').has('age',gt(100)) |
查看pluto兄弟及居住的地方 |
g.V().hasLabel('character').has('name', 'pluto').out('brother').as('god').out('lives').as('place').select('god','place') g.V().hasLabel('character').has('name', 'pluto').out('brother').as('god').out('lives').as('place').select('god','place').by('name') |
查询目的 |
gremlin语句 |
最短路径查询 |
g.V('3:pluto').repeat(__.both().simplePath()).until(__.loops().is(P.eq(4))).hasId('3:hydra').path() g.V('3:pluto').repeat(__.both().simplePath()).until(__.hasId('3:hydra')).hasId('3:hydra').path() g.V('3:pluto').repeat(__.both().simplePath()).until(__.hasId('3:hydra').or().loops().is(P.gt(3))).hasId('3:hydra').path() g.V('3:pluto').repeat(__.both().simplePath()).until(__.hasId('3:hydra').and().loops().is(P.gt(3))).hasId('3:hydra').path() |
扩线查询(一、二层扩线) |
g.V('3:pluto').both().path() g.V('3:pluto').both().both().path() |
扩线查询【去重】 |
g.V('3:pluto').both().dedup().path() |
版权声明:xxxxxxxxx;
工作时间:8:00-18:00
客服电话
电子邮件
扫码二维码
获取最新动态