【问题标题】:Gremlin DSE Graph find path on edge/vertex with multiple propertiesGremlin DSE Graph 在具有多个属性的边/顶点上查找路径
【发布时间】:2021-01-24 19:24:51
【问题描述】:

我有一个案例,也许这里的任何人都可以帮助我找出 DSE Graph 中的 gremlin 查询,以找到具有多个属性的边/顶点上的路径。

case

这是我在 DSE Graph 中的代码

schema.propertyKey("id").Uuid().ifNotExists().create()`
schema.propertyKey("name").Text().single().create()  
schema.propertyKey("vlan").Text().multiple().create()  
schema.vertexLabel("site").partitionKey('id').properties("id", "name", "vlan").ifNotExists().create()
schema.edgeLabel("linkTo").multiple().properties("id", "name", "vlan").create()
schema.edgeLabel("linkTo").connection("site","site").add()

schema.vertexLabel("site").index("search").search().by("name").by("vlan").asText().add()
schema.vertexLabel("site").index("siteBySiteId").materialized().by("name").add()
schema.vertexLabel("site").index("toLink").outE("linkTo").by("vlan").add()
schema.vertexLabel("site").index("fromLink").inE("linkTo").by("vlan").add()

AA = graph.addVertex(T.label, 'site', 'id', UUID.randomUUID(), 'name', 'AA', 'vlan', '3353', 'vlan', '3563', 'vlan', '2467')
BB = graph.addVertex(T.label, 'site', 'id', UUID.randomUUID(), 'name', 'BB', 'vlan', '3353', 'vlan', '3563')
CC = graph.addVertex(T.label, 'site', 'id', UUID.randomUUID(), 'name', 'CC', 'vlan', '2467')
DD = graph.addVertex(T.label, 'site', 'id', UUID.randomUUID(), 'name', 'CC', 'vlan', '3353', 'vlan', '3563', 'vlan', '2467')

AA.addEdge('linkTo', BB, 'id', UUID.randomUUID(), 'name', 'AA-BB', 'vlan', '3353', 'vlan', '3563')
AA.addEdge('linkTo', CC, 'id', UUID.randomUUID(), 'name', 'AA-CC', 'vlan', '2467')
BB.addEdge('linkTo', DD, 'id', UUID.randomUUID(), 'name', 'BB-DD', 'vlan', '3353', 'vlan', '3563')
CC.addEdge('linkTo', DD, 'id', UUID.randomUUID(), 'name', 'CC-DD', 'vlan', '2467')`enter code here

--------------------
g.V().
  has("site", "name", "AA").  
  emit().
  repeat(timeLimit(200).both("linkTo").        
   filter(bothE("linkTo").                   
     has("vlan",Search.tokenPrefix("3353"))). 
   simplePath()).                             
  has("name", "DD").                     
  path().unfold()

但没有成功:)

【问题讨论】:

    标签: gremlin tinkerpop dse


    【解决方案1】:

    所以这里发生了几件事:

    首先在您的创建脚本中,这一行将name 属性设置为CC 而不是DD

    DD = graph.addVertex(T.label, 'site', 'id', UUID.randomUUID(), 'name', 'CC', 'vlan', '3353', 'vlan', '3563', 'vlan', '2467')
    

    其次,在 Gremlin 中,您不能向边添加多属性 (described here),它们仅在顶点上有效。因此,您需要更改加载脚本以添加平行边,每个 vlan 属性一个,如下所示:

    AA.addEdge('linkTo', BB, 'id', UUID.randomUUID(), 'name', 'AA-BB', 'vlan', '3353')
    AA.addEdge('linkTo', BB, 'id', UUID.randomUUID(), 'name', 'AA-BB', 'vlan', '3563')
    AA.addEdge('linkTo', CC, 'id', UUID.randomUUID(), 'name', 'AA-CC', 'vlan', '2467')
    BB.addEdge('linkTo', DD, 'id', UUID.randomUUID(), 'name', 'BB-DD', 'vlan', '3353')
    BB.addEdge('linkTo', DD, 'id', UUID.randomUUID(), 'name', 'BB-DD', 'vlan', '3563')
    CC.addEdge('linkTo', DD, 'id', UUID.randomUUID(), 'name', 'CC-DD', 'vlan', '2467')
    

    由于您没有详细说明您试图获得的答案,我推断您正在寻找从AA 到DD 的所有路径,其中vlan 是3353。这可以通过几种不同的方式来完成。如果你像上面那样创建平行边,你可以使用这个 Gremlin 遍历来完成这个:

    g.V().
      has("site", "name", "AA").
      repeat(
        timeLimit(200).
        bothE("linkTo").
        has("vlan","3353").
        otherV().
        simplePath()
      ).until(has("name", "DD")).
      path().by('name')
    

    如果您决定只想将 vlan 属性存储在顶点上,您可以通过 Gremlin 获得,如下所示:

    g.V().
      has("site", "name", "AA").
      repeat(
        timeLimit(200).
        both("linkTo").
        has("vlan","3353").
        simplePath()
      ).until(has("name", "DD")).
      path().by('name').dedup()
    

    这样做的缺点是path 将不包含边缘。如果您还想要path 中的边缘信息,您可以将both() 步骤更改为bothE()/otherV() 模式,如下所示:

    g.V().
      has("site", "name", "AA").
      repeat(
        timeLimit(200).
        bothE("linkTo").
        otherV().
        has("vlan","3353").
        simplePath()
      ).until(has("name", "DD")).
      path().by('name').dedup()
    

    【讨论】:

      猜你喜欢
      • 2018-03-27
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      相关资源
      最近更新 更多