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

huangapple go评论84阅读模式
英文:

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?

英文:

My gremlin-server.yaml file is as follows:

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 }}

I am using java to connect to the gremlin server. Is there a way to retrieve the graph names: graph and graph2 from code?

Alternatively if I bind graph and graph2 traversals to g and g2 in the db.groovy file and add them as global bindings, is there a way to retrieve the names: g and g2?

答案1

得分: 2

没有直接的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(或者更可能是过滤出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}
英文:

There is no direct API to get this listing but there is a workaround to get it if you use a script-based request and your provider implementation has not disallowed this for security reason (or simply doesn't support it given the way it has implemented the protocol). In short, I would only expect this approach to work with TinkerPop's Gremlin Server implementation and if security sandboxing is disabled or configured to allow access to the classes involved.

Gremlin Server hosts a ScriptEngine instance that process scripts. It has a "context" which is available as a variable with the same name. You can access that variable with:

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

Once you have that, you can filter out Graph (or more likely you would fitler GraphTraversalSource instances) and get the name it is known by on the server:

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

As you can see it works just as well with a compliant driver:

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}

huangapple
  • 本文由 发表于 2020年5月29日 04:36:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/62074067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定