是否有一种方法可以基于查询外部的数据而不是基于图形有条件地执行gremlin查询的一部分。
我使用Go语言、Gremlingo驱动程序和AWS Neptune。我已经能够从我的代码中做到这一点,但它并不漂亮。下面是我是如何做到的。
func (n NeptuneGremlinGraph) Put(assetID string, version string, records []les.DeltaEditRecord) error {
g := gremlin.Traversal_().WithRemote(n.connection)
anonT := gremlin.T__
for _, r := range records {
promise := n.addParent(g.V().HasLabel("Entity").
Has("asset_id", assetID).
Has("version", version).
Has("entity_id", r.EntityID).
Fold().
Coalesce(anonT.Unfold(),
anonT.AddV("Entity").
Property("asset_id", assetID).
Property("version", version).
Property("entity_id", r.EntityID)).
Store("e").
V().HasLabel("Component").
Has("asset_id", assetID).
Has("version", version).
Has("entity_id", r.EntityID).
Has("component_id", r.ComponentID).
Fold().
Coalesce(anonT.Unfold().
Property("value", r.Value),
anonT.AddV("Component").
Property("asset_id", assetID).
Property("version", version).
Property("entity_id", r.EntityID).
Property("component_id", r.ComponentID).
Property("value", r.Value)).
AddE("ATTACHED_TO").To(anonT.Cap("e").Unfold()), anonT, "e", assetID, version, r).Iterate()
err := <-promise
if err != nil {
return err
}
}
return nil
}
func (n NeptuneGremlinGraph) addParent(graphTraversal *gremlin.GraphTraversal, anonT gremlin.AnonymousTraversal, e, assetID, version string, record les.DeltaEditRecord) *gremlin.GraphTraversal {
if !n.hasParent(record) {
return graphTraversal
}
parent := n.getParent(record)
return graphTraversal.V().
HasLabel("Entity").
Has("asset_id", assetID).
Has("version", version).
Has("entity_id", parent).
Fold().
Coalesce(anonT.Unfold(),
anonT.AddV("Entity").
Property("asset_id", assetID).
Property("version", version).
Property("entity_id", parent)).
AddE("CHILD_OF").From(anonT.Cap(e).Unfold())
}
如果我能给gremlin.GraphTraversal添加一个新方法,那就太好了,但是我不认为你能给另一个包中的struct添加方法,我想我也可以在一个事务中用两个完全独立的查询来完成。
只是想知道是否有什么我错过了,并有一种方法可以做到这一点的查询。
1条答案
按热度按时间xwbd5t1u1#
这里有两种不同的方法。首先,你可以在代码中动态地构建一个查询。就像上面的例子一样,如果你的第二个方法接受一个图形遍历对象作为参数,那么在作为图形遍历对象返回之前,可以附加额外的步骤。下面是简单的伪代码:
只有在最后添加一个终端步骤,图形遍历才会发送到服务器。
更复杂的方法是创建自己的DSL,“扩展”查询语言以简化重用的查询组件。https://tinkerpop.apache.org/docs/current/reference/#gremlin-go-dsl