希腊神话中的数据兽(Hugegraph--基于希腊神话人物关系数据的常用查询)

随着社交、电商、金融、零售、物联网等行业的快速发展,现实社会织起了了一张庞大而复杂的关系网,亟需一种支持海量复杂数据关系运算的数据库即图数据库。本系列文章是学习知识图谱以及图数据库相关的知识梳理与总结

本文会包含如下内容:

  • 希腊神话人物关系数据
  • Gremlin 常用语句汇总

本篇文章适合人群:架构师、技术专家、对知识图谱与图数据库感兴趣的高级工程师

1. 希腊神话人物关系数据

通过Property Graph Model图数据模型来描述希腊神话中各人物角色的关系(也被成为人物关系图谱),具体关系详见下图

希腊神话中的数据兽(Hugegraph--基于希腊神话人物关系数据的常用查询)(1)

推荐使用HugeGraph-Studio 通过可视化的方式来执行上述代码。另外也可以通过HugeGraph-Client、HugeApi、GremlinConsole和GremlinDriver等多种方式执行下面的代码。(包括Schema、索引及数据的创建

但注意如下:

  • HugeGraph默认是自动生成Id,如果用户通过primaryKeys指定VertexLabel的primaryKeys字段列表后,VertexLabel的Id策略将会自动切换到primaryKeys策略。 启用primaryKeys策略后,HugeGraph通过vertexLabel primaryKeys拼接生成VertexId ,可实现自动去重,同时无需额外创建索引即可以使用primaryKeys中的属性进行快速查询。 例如 "character" 和 "location" 都有primaryKeys("name")属性,因此在不额外创建索引的情况下可以通过g.V().hasLabel('character') .has('name','hercules')查询vertex 。
  • 针对character的age属性需要创建range索引,否则不能根据age进行范围过滤查询

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)

2. Gremlin 常用语句汇总2.1 统计类

查询目的

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()

2.2 遍历查询类

查询目的

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')

2.3 图计算类

查询目的

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()

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页