【问题标题】:How to retrieve all graph names from the gremlin server如何从 gremlin 服务器检索所有图形名称
【发布时间】:2023-10-06 01:33:01
【问题描述】:

我的 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?

【问题讨论】:

    标签: java orientdb gremlin


    【解决方案1】:

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

    Gremlin 服务器托管一个处理脚本的 ScriptEngine 实例。它有一个“上下文”,可用作具有相同名称的变量。您可以通过以下方式访问该变量:

    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(或者更可能你会过滤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}
    

    【讨论】:

    • 非常感谢!这完美无缺!这是否记录在某处?
    • 谢谢斯蒂芬!非常感谢您的帮助。