0

As known from tinkerpop doc, we can use code to connect remote graph db

    cluster = Cluster.build(addr).serializer(Serializers.DEFAULT_RESULT_SERIALIZER)
            .credentials(username, password)
            .enableSsl(true)
            .port(port).create();
    client = cluster.connect();
    client.submit("g.E().drop()").all().join();
    client.submit("g.V().drop()").all().join();
    client.submit("g.addV('person').property('id', '1').property('name', 'pli').property('age', 31)").all().join();

but there is another interface from org.apache.tinkerpop.gremlin.structure.Graph with code

    Graph graph = EmptyGraph.instance();
    GraphTraversalSource g = graph.traversal().withRemote(DriverRemoteConnection.using(cluster));

    g.E().drop();
    g.V().drop();
    g.addV("web").property("id", "2").properties("name", "github");

Can I integrate these two part for writing query to graph database? instead of use gremlin string literal for query? Thanks in advance!

Update 1 with one new try with following code

        client = cluster.connect().alias(new HashMap<String, String>());
        Graph graph = EmptyGraph.instance();
        GraphTraversalSource g = graph.traversal().withRemote(DriverRemoteConnection.using(cluster, "g"));
        GraphTraversal traversal = g.E().drop().V().drop().addV("web").property("id", "2").properties("name", "github");

        client.submit(traversal).all().join();

And it will hang at last client.submit in one native function ->

private native int poll0(long var1, int var3, int[] var4, int[] var5, int[] var6, long var7);

I guess it may need some specific configuration for alias hashmap ?

Update 2

Thanks for you clearly answers. I have a try on traversal this weekend with following code but still got hang when traversal.next/traversal.hasNext()

    private void prepareEnvironment() {
        this.cluster = Cluster.build(this.config.getEndpoint())
                .serializer(Serializers.DEFAULT_RESULT_SERIALIZER)
                .enableSsl(true)
                .port(Integer.valueOf(this.config.getPort()))
                .credentials(this.config.getUsername(), this.config.getPassword())
                .create();
        // this.client = this.cluster.connect(Constants.GREMLIN_ALIAS, true);
        // this.client = this.cluster.connect();
        this.graph = EmptyGraph.instance();
        this.graphSource = this.graph.traversal().withRemote(DriverRemoteConnection.using(cluster));
    }

    @Before
    public void setup() {
        this.prepareEnvironment();

//      client.submit("g.V().drop()").all().join();

        GraphTraversal graphTraversal = this.graphSource.V().drop();
        Object o = graphTraversal.next();

        // while (graphTraversal.hasNext()) {
        // }
    }

I capture some log of console, and It seems it loops on http response here.

<submit from client>    -> 23:05:38.559 [main] DEBUG org.apache.tinkerpop.gremlin.driver.Client - Submitted RequestMessage{, requestId=6dd55134-2266-4b23-a311-9d25b5b51dc0, op='eval', processor='', args={batchSize=64, gremlin=g.V().drop()}} to - Connection{host=Host{address=pli-gremlin-test.gremlin.cosmosdb.azure.com/104.45.144.73:443, hostUri=wss://pli-gremlin-test.gremlin.cosmosdb.azure.com:443/gremlin}}
<submit from traversal> -> 23:07:54.823 [main] DEBUG org.apache.tinkerpop.gremlin.driver.Client - Submitted RequestMessage{, requestId=1a7f550e-eb7f-497d-8b4b-1fbf40beb99e, op='bytecode', processor='traversal', args={gremlin=[[], [V(), drop()]], aliases={g=g}}} to - Connection{host=Host{address=pli-gremlin-test.gremlin.cosmosdb.azure.com/104.45.144.73:443, hostUri=wss://pli-gremlin-test.gremlin.cosmosdb.azure.com:443/gremlin}}

<submit from traversal log>   
23:16:16.708 [main] DEBUG org.apache.tinkerpop.gremlin.driver.ConnectionPool - Borrowing connection from pool on Host{address=pli-gremlin-test.gremlin.cosmosdb.azure.com/104.45.144.73:443, hostUri=wss://pli-gremlin-test.gremlin.cosmosdb.azure.com:443/gremlin} - timeout in 3000 MILLISECONDS
23:16:16.708 [main] DEBUG org.apache.tinkerpop.gremlin.driver.ConnectionPool - Return least used Connection{host=Host{address=pli-gremlin-test.gremlin.cosmosdb.azure.com/104.45.144.73:443, hostUri=wss://pli-gremlin-test.gremlin.cosmosdb.azure.com:443/gremlin}, isDead=false, borrowed=1, pending=0} on Host{address=pli-gremlin-test.gremlin.cosmosdb.azure.com/104.45.144.73:443, hostUri=wss://pli-gremlin-test.gremlin.cosmosdb.azure.com:443/gremlin}
23:16:16.724 [main] DEBUG org.apache.tinkerpop.gremlin.driver.Client - Submitted RequestMessage{, requestId=c50cef01-270e-469a-8423-838fe67565d4, op='bytecode', processor='traversal', args={gremlin=[[], [V(), drop()]], aliases={g=g}}} to - Connection{host=Host{address=pli-gremlin-test.gremlin.cosmosdb.azure.com/104.45.144.73:443, hostUri=wss://pli-gremlin-test.gremlin.cosmosdb.azure.com:443/gremlin}}
23:16:16.833 [gremlin-driver-loop-1] DEBUG io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder - Encoding WebSocket Frame opCode=2 length=360
23:16:20.432 [gremlin-driver-loop-1] DEBUG io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder - Decoding WebSocket Frame opCode=8
23:16:20.447 [gremlin-driver-loop-1] DEBUG io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder - Decoding WebSocket Frame length=23
23:16:45.197 [gremlin-driver-loop-2] DEBUG io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder - Decoding WebSocket Frame opCode=10
23:16:45.197 [gremlin-driver-loop-2] DEBUG io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder - Decoding WebSocket Frame length=0
23:16:45.197 [gremlin-driver-loop-2] DEBUG org.apache.tinkerpop.gremlin.driver.handler.WebSocketClientHandler - Received response from keep-alive request
23:17:15.191 [gremlin-driver-loop-2] DEBUG io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder - Decoding WebSocket Frame opCode=10
23:17:15.191 [gremlin-driver-loop-2] DEBUG io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder - Decoding WebSocket Frame length=0
23:17:15.191 [gremlin-driver-loop-2] DEBUG org.apache.tinkerpop.gremlin.driver.handler.WebSocketClientHandler - Received response from keep-alive request
23:17:45.215 [gremlin-driver-loop-2] DEBUG io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder - Decoding WebSocket Frame opCode=10
23:17:45.215 [gremlin-driver-loop-2] DEBUG io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder - Decoding WebSocket Frame length=0
23:17:45.215 [gremlin-driver-loop-2] DEBUG org.apache.tinkerpop.gremlin.driver.handler.WebSocketClientHandler - Received response from keep-alive request
1
  • Or is there any way to convert Graph/GraphTraversalSource to gremlin query String literal? Commented Mar 21, 2018 at 3:16

1 Answer 1

3

Can I integrate these two part for writing query to graph database? instead of use gremlin string literal for query?

That's precisely what withRemote() does. It compiles the traversals you write into Gremlin bytecode, sends that to the server and then returns the results.

Or is there any way to convert Graph/GraphTraversalSource to gremlin query String literal?

you can do that too, but it isn't really recommended. Prefer bytecode over strings, but if you absolutely had to do it AND if your traversal does not include lambdas you can do:

gremlin> translator = org.apache.tinkerpop.gremlin.groovy.jsr223.GroovyTranslator.of("g")
==>translator[g:gremlin-groovy]
gremlin> translator.translate(g.V().out('knows').has('name','josh').asAdmin().getBytecode())
==>g.V().out("knows").has("name","josh")

GroovyTranslator is in the gremlin-groovy module.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. Update question with one new try.
you don't need to do this: client.submit(traversal).all().join(); just iterate the traversal with toList() and the traversal will execute remotely on the server in a transparent fashion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.