鉴于两个特定的顶点,如何在Titan中获取它们之间的子图?

时间:2017-02-16 03:06:07

标签: gremlin titan nosql

我们有数百万个顶点和数千万个边来形成有向图。有人知道如何在两个给定顶点之间获取Titan中的子图吗?

1 个答案:

答案 0 :(得分:2)

TinkerPop提供了两种子图方法:

  1. subgraph step
  2. subgraph strategy
  3. 使用subgraph步骤"弹出"子图成单独的图形实例。您可以在以下示例中看到图表" g"被提交到一个新的Graph实例" know"边缘:

    gremlin> subGraph = g.E().hasLabel('knows').subgraph('subGraph').cap('subGraph').next() //(1)
    ==>tinkergraph[vertices:3 edges:2]
    gremlin> sg = subGraph.traversal()
    ==>graphtraversalsource[tinkergraph[vertices:3 edges:2], standard]
    gremlin> sg.E() //(2)
    ==>e[7][1-knows->2]
    ==>e[8][1-knows->4]
    

    如果您只想遍历图表的子集,则可以使用Subgraph策略:

    gremlin> graph = TinkerFactory.createTheCrew()
    ==>tinkergraph[vertices:6 edges:14]
    gremlin> g = graph.traversal().withStrategies(SubgraphStrategy.build().
               vertices(or(hasNot('location'),properties('location').count().is(gt(3)))).
               edges(hasLabel('develops')).
               vertexProperties(or(hasLabel(neq('location')),hasNot('endTime'))).create())
    ==>graphtraversalsource[tinkergraph[vertices:6 edges:14], standard]
    gremlin> g.V().valueMap(true)
    ==>[id:1,label:person,name:[marko],location:[santa fe]]
    ==>[id:8,label:person,name:[matthias],location:[seattle]]
    ==>[id:10,label:software,name:[gremlin]]
    ==>[id:11,label:software,name:[tinkergraph]]
    gremlin> g.E().valueMap(true)
    ==>[id:13,label:develops,since:2009]
    ==>[id:14,label:develops,since:2010]
    ==>[id:21,label:develops,since:2012]
    

    根据你问题的标题,听起来你只想要两个顶点的图形,它们之间有边缘。我想这听起来像你想要使用subgraph步骤。当然,获得这个结果似乎比这更容易:

    gremlin> g = TinkerFactory.createModern().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
    gremlin> g.V(1).bothE().where(otherV().hasId(2))
    ==>e[7][1-knows->2]
    

    具有边缘列表在某种意义上构成子图,特别是在两个已知顶点的情况下。