如何在 gremlin-go 中执行“constant([:])”

问题描述 投票:0回答:1

我之前在 Stack Overflow 帖子中收到了 Kelvin 的有用指导(链接在这里:Gremin mergeV() Upsert:了解是否添加了顶点(无副作用))有关使用 Gremlin 成功执行 Groovy 查询的信息mergeV 语法。该查询使我能够确定是否添加或获取了顶点,而没有任何副作用。现在,我在尝试将该 Groovy 代码转换为 Go 代码时遇到了问题。 在我的 Gremlin 控制台中正常运行的 Groovy 代码如下:

g.mergeV([(label): 'principal', spiffe_id: '1']). 
option(Merge.onCreate,[creat_at:'10/23/2023', update_at:'11/23/2023']).
option(Merge.onMatch,sideEffect(constant(true).store('found')).constant([:])).
option(Merge.onCreate,sideEffect(constant(false).store('found')).constant([:])).
project('spiffe_id','status'). by(id()). by(select('found').unfold())

但是,当尝试将此代码转换为 Golang 时,我遇到了错误:

code:244 message:class java.util.LinkedHashMap cannot be cast to 
class org.apache.tinkerpop.gremlin.structure.Element

这是我正在使用的 Golang 代码:

var __ = gremlingo.T__

steps := graph.g.MergeV(map[interface{}]interface{}{
        gremlingo.T.Label: "principal", "spiffe_id": "1"}).
        Option(gremlingo.Merge.OnCreate, map[interface{}]interface{}{
            "created_at": "10/23/2023", "updated_at": "10/23/2023"}).
        Option(gremlingo.Merge.OnCreate, __.SideEffect(__.Constant(false).Store("found")).Constant(map[interface{}]interface{}{})).
        Option(gremlingo.Merge.OnMatch, __.SideEffect(__.Constant(true).Store("found")).Constant(map[interface{}]interface{}{})).
        Project("principal", "status").By(__.Identity()).By(__.Select("found").Unfold())
queryResult, err := steps.ElementMap().ToList()
...

在尝试注释掉涉及

Constant(map[interface{}]interface{}{})
的部分时,我遇到了一个新错误:
java.lang.Boolean cannot be cast to class java.util.Map
。看来问题是由 Groovy 中的
constant([:])
操作引起的,我尝试在 Golang 中将其复制为
Constant(map[interface{}]interface{}{})

在序列化和反序列化过程中,gremlin Element 结构尝试利用 Java LinkedHashMap 时似乎存在不匹配。我不确定这是由于我的 Go 代码中的错误还是由于 gremlin-go 客户端和 Java 服务器之间的不兼容。

我非常感谢任何有关解决此问题的见解或指导。谢谢您的帮助!

gremlin tinkerpop3 gremlin-server
1个回答
0
投票

主要原因是Groovy和Golang的请求不等价。

错误

代码:244 消息:类 java.util.LinkedHashMap 无法转换为 类 org.apache.tinkerpop.gremlin.struct.Element

被步骤

ElementMap()
抛出,因为它无法处理
Element
以外的输入,但接收由前一个
Map
生成的
Project
。 在 Groovy 示例中没有
ElementMap()

因此,一个可能的解决方案是删除

ElementMap()
。或者,如果你需要的话,就搬进去吧
Project
Project("principal", "status").By(__.Identity().ElementMap()).By(__.Select("found").Unfold())

附注在 Gremlin 服务器 3.7.1 上测试

© www.soinside.com 2019 - 2024. All rights reserved.