Tinkerpop Gremlin表达

时间:2017-07-22 00:22:18

标签: graph azure-cosmosdb gremlin

Graph Structure

在上图中,我有用户,客户,网站和工作。用户连接到客户端(管理)和客户端(有) - >网站(有)--->工作。我正在努力让所有有活跃工作的客户。

g.V().hasLabel('user').
      has('id', '338d219c-3457-4ad8-8172-37a7d897c0df').
      outE('manages').
      inV().
      hasLabel('client').
      outE('have').
      inV().
      hasLabel('site').
      outE('have').
      inV().
      hasLabel('job').
      has('status', 'In Progress')

以上查询获取所有工作,但我有兴趣了解客户,网站和工作。我想知道如何实现这一目标?

1 个答案:

答案 0 :(得分:5)

你可能没有多少方法可以做到这一点,但首先让我们简化现有的Gremlin,因为你说它可以做你想做的事情:

g.V().has('user','id','338d219c-3457-4ad8-8172-37a7d897c0df').
  out('manages').
  hasLabel('client').
  out('have').
  hasLabel('site').
  out('have').
  has('job','status', 'In Progress')

请注意,outE().inV()这样的组合基本上只是out()。事实上,如果你把它们写成outE().inV(),当应用策略时,Gremlin会遇到将遍历重写为out()的麻烦。请参阅以下两次遍历的explain()

gremlin> g.V().outE().inV().outE().inV().explain()
==>Traversal Explanation
=============================================================================================================================================================
Original Traversal                 [GraphStep(vertex,[]), VertexStep(OUT,edge), EdgeVertexStep(IN), VertexStep(OUT,edge), EdgeVertexStep(IN)]

ConnectiveStrategy           [D]   [GraphStep(vertex,[]), VertexStep(OUT,edge), EdgeVertexStep(IN), VertexStep(OUT,edge), EdgeVertexStep(IN)]
MatchPredicateStrategy       [O]   [GraphStep(vertex,[]), VertexStep(OUT,edge), EdgeVertexStep(IN), VertexStep(OUT,edge), EdgeVertexStep(IN)]
FilterRankingStrategy        [O]   [GraphStep(vertex,[]), VertexStep(OUT,edge), EdgeVertexStep(IN), VertexStep(OUT,edge), EdgeVertexStep(IN)]
InlineFilterStrategy         [O]   [GraphStep(vertex,[]), VertexStep(OUT,edge), EdgeVertexStep(IN), VertexStep(OUT,edge), EdgeVertexStep(IN)]
IncidentToAdjacentStrategy   [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
AdjacentToIncidentStrategy   [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
RangeByIsCountStrategy       [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
RepeatUnrollStrategy         [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
PathRetractionStrategy       [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
LazyBarrierStrategy          [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), NoOpBarrierStep(2500), VertexStep(OUT,vertex), NoOpBarrierStep(2500)]
TinkerGraphCountStrategy     [P]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), NoOpBarrierStep(2500), VertexStep(OUT,vertex), NoOpBarrierStep(2500)]
TinkerGraphStepStrategy      [P]   [TinkerGraphStep(vertex,[]), VertexStep(OUT,vertex), NoOpBarrierStep(2500), VertexStep(OUT,vertex), NoOpBarrierStep(2500)]
ProfileStrategy              [F]   [TinkerGraphStep(vertex,[]), VertexStep(OUT,vertex), NoOpBarrierStep(2500), VertexStep(OUT,vertex), NoOpBarrierStep(2500)]
StandardVerificationStrategy [V]   [TinkerGraphStep(vertex,[]), VertexStep(OUT,vertex), NoOpBarrierStep(2500), VertexStep(OUT,vertex), NoOpBarrierStep(2500)]

Final Traversal                    [TinkerGraphStep(vertex,[]), VertexStep(OUT,vertex), NoOpBarrierStep(2500), VertexStep(OUT,vertex), NoOpBarrierStep(2500)]
gremlin> g.V().out().out().explain()
==>Traversal Explanation
=============================================================================================================================================================
Original Traversal                 [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]

ConnectiveStrategy           [D]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
MatchPredicateStrategy       [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
FilterRankingStrategy        [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
InlineFilterStrategy         [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
IncidentToAdjacentStrategy   [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
AdjacentToIncidentStrategy   [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
RangeByIsCountStrategy       [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
RepeatUnrollStrategy         [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
PathRetractionStrategy       [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), VertexStep(OUT,vertex)]
LazyBarrierStrategy          [O]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), NoOpBarrierStep(2500), VertexStep(OUT,vertex), NoOpBarrierStep(2500)]
TinkerGraphCountStrategy     [P]   [GraphStep(vertex,[]), VertexStep(OUT,vertex), NoOpBarrierStep(2500), VertexStep(OUT,vertex), NoOpBarrierStep(2500)]
TinkerGraphStepStrategy      [P]   [TinkerGraphStep(vertex,[]), VertexStep(OUT,vertex), NoOpBarrierStep(2500), VertexStep(OUT,vertex), NoOpBarrierStep(2500)]
ProfileStrategy              [F]   [TinkerGraphStep(vertex,[]), VertexStep(OUT,vertex), NoOpBarrierStep(2500), VertexStep(OUT,vertex), NoOpBarrierStep(2500)]
StandardVerificationStrategy [V]   [TinkerGraphStep(vertex,[]), VertexStep(OUT,vertex), NoOpBarrierStep(2500), VertexStep(OUT,vertex), NoOpBarrierStep(2500)]

Final Traversal                    [TinkerGraphStep(vertex,[]), VertexStep(OUT,vertex), NoOpBarrierStep(2500), VertexStep(OUT,vertex), NoOpBarrierStep(2500)]

看看最后一行是如何相同的? IncidentToAdjacentStrategy应用了这个更改,因此最终两个遍历都编译为同一个东西。

在回答原始问题时,如果您需要拥有客户,网站和工作,请使用select()

g.V().has('user','id','338d219c-3457-4ad8-8172-37a7d897c0df').
  out('manages').
  hasLabel('client').as('c').
  out('have').
  hasLabel('site').as('s').
  out('have').
  has('job','status', 'In Progress').as('j').
  select('c','s','j')