如何从 gremlin 服务器检索所有图形名称

How to retrieve all graph names from the gremlin server

我的gremlin-server.yaml文件如下:

host: localhost
port: 8182
scriptEvaluationTimeout: 30000
channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer
graphManager : com.orientechnologies.tinkerpop.server.OrientGremlinGraphManager
graphs: {
  graph : ../config/db1.properties,
  graph2 : ../config/db2.properties
}
scriptEngines: {
  gremlin-groovy: {
    plugins: { org.apache.tinkerpop.gremlin.server.jsr223.GremlinServerGremlinPlugin: {},
               org.apache.tinkerpop.gremlin.orientdb.jsr223.OrientDBGremlinPlugin: {},
               org.apache.tinkerpop.gremlin.jsr223.ImportGremlinPlugin: {classImports: [java.lang.Math], methodImports: [java.lang.Math#*]},
               org.apache.tinkerpop.gremlin.jsr223.ScriptFileGremlinPlugin: {files: [../config/db.groovy]}}}}
serializers:
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { ioRegistries: [org.apache.tinkerpop.gremlin.orientdb.io.OrientIoRegistry] }}             # application/vnd.gremlin-v3.0+gryo
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { serializeResultToString: true }}                                                                       # application/vnd.gremlin-v3.0+gryo-stringd
  - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV3d0, config: { ioRegistries: [org.apache.tinkerpop.gremlin.orientdb.io.OrientIoRegistry] }}         # application/json
processors:
  - { className: org.apache.tinkerpop.gremlin.server.op.session.SessionOpProcessor, config: { sessionTimeout: 28800000 }}
  - { className: org.apache.tinkerpop.gremlin.server.op.traversal.TraversalOpProcessor, config: { cacheExpirationTime: 600000, cacheMaxSize: 1000 }}

我正在使用 java 连接到 gremlin 服务器。有没有办法从代码中检索图形名称:graph 和 graph2?

或者,如果我将 graph 和 graph2 遍历绑定到 db.groovy 文件中的 g 和 g2 并将它们添加为全局绑定,有没有办法检索名称:g 和 g2?

没有直接 API 获取此列表,但如果您使用基于脚本的请求并且您的提供程序实施出于安全原因未禁止此操作(或根本不允许),则有一种解决方法可以获取此列表鉴于它实现协议的方式,不支持它)。简而言之,我只希望这种方法适用于 TinkerPop 的 Gremlin 服务器实现,并且如果安全沙箱被禁用或配置为允许访问所涉及的 类。

Gremlin 服务器托管一个处理脚本的 ScriptEngine 实例。它有一个 "context" 可以作为同名变量使用。您可以通过以下方式访问该变量:

gremlin> :remote connect tinkerpop.server conf/remote.yaml
==>Configured localhost/127.0.0.1:8182
gremlin> :remote console
==>All scripts will now be sent to Gremlin Server - [localhost/127.0.0.1:8182] - type ':remote console' to return to local mode
gremlin> context
==>org.apache.tinkerpop.gremlin.jsr223.GremlinScriptContext@c7ef4c5

一旦你有了它,你就可以过滤掉 Graph(或者更可能你会 fitler GraphTraversalSource 个实例)并获取它在服务器上已知的名称:

gremlin> context.getBindings(javax.script.ScriptContext.GLOBAL_SCOPE).entrySet().findAll{it.value instanceof Graph}.key
==>graph
gremlin> context.getBindings(javax.script.ScriptContext.GLOBAL_SCOPE).entrySet().findAll{it.value instanceof GraphTraversalSource}.key
==>g

如您所见,它与兼容的驱动程序一起工作得很好:

gremlin> cluster = Cluster.open()
==>localhost/127.0.0.1:8182
gremlin> client = cluster.connect()
==>org.apache.tinkerpop.gremlin.driver.Client$ClusteredClient@5408d4b3
gremlin> client.submit("context.getBindings(javax.script.ScriptContext.GLOBAL_SCOPE).entrySet().findAll{it.value instanceof GraphTraversalSource}.key").all().get()
==>result{object=g class=java.lang.String}