从tinkerpop doc可以看出,我们可以使用代码连接远程图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();
但是org.apache.tinkerpop.gremlin.structure.Graph还有另一个接口,代码为
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");
我可以将这两部分集成到图形数据库中进行查询吗?而不是使用gremlin字符串文字进行查询? 提前谢谢!
使用以下代码进行一次新的尝试更新1
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();
它将在最后一个client.submit中挂起在一个本机函数中 - &gt;
private native int poll0(long var1, int var3, int[] var4, int[] var5, int[] var6, long var7);
我想它可能需要一些针对别名hashmap的特定配置?
更新2
谢谢你的答案。我在本周末尝试使用以下代码进行遍历,但在遍历时仍会挂起.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()) {
// }
}
我捕获了一些控制台日志,似乎它在这里的http响应上循环。
<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
答案 0 :(得分:2)
我可以将这两部分集成到图形数据库中进行查询吗?而不是使用gremlin字符串文字进行查询?
这正是withRemote()
所做的。它编译你写入Gremlin字节码的遍历,将其发送到服务器,然后返回结果。
或者有没有办法将Graph / GraphTraversalSource转换为gremlin查询字符串文字?
你也可以这样做,但不是真的推荐。首选字符串代码,但如果你必须这样做,如果你的遍历不包括lambda,你可以这样做:
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
位于gremlin-groovy
模块中。