如何从gremlin服务器中检索所有的图名

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

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

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

java orientdb gremlin
1个回答
1
投票

没有直接的API来获取这个列表,但是如果你使用基于脚本的请求,并且你的提供者的实现没有因为安全原因而不允许这样做(或者干脆不支持它,因为它已经实现了协议的方式),那么有一个变通的方法来获取它。简而言之,我只期望这种方法能与TinkerPop的Gremlin Server实现一起工作,并且如果安全沙箱被禁用或配置为允许访问相关类。

Gremlin Server的主机是一个 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}
© www.soinside.com 2019 - 2024. All rights reserved.