[关闭]
@contribute 2016-09-05T13:37:14.000000Z 字数 1620 阅读 1431

vertx-gremlin

tinkerpop


vertx-gremlin

Gremlin server becomes Gremlin verticle

Gremlin Server 提供了一种在单图或多图的图实例中执行远程gremlin脚本的方式。更多了解Gremlin Server的相关内容请参考 tinkerpop-gremlin server

vertx-gremlin采用Vertx,充分利用vertx的优势,将Gremlin server变得更易使用、部署及维护。

添加依赖

  1. <dependency>
  2. <groupId>com.monogram.metagraph</groupId>
  3. <artifactId>vertx-gremlin-client</artifactId>
  4. <version>1.0-SNAPSHOT</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.monogram.metagraph</groupId>
  8. <artifactId>vertx-gremlin-server</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. </dependency>

创建客户端

如果你只是想连接本地一个Gremlin Server,那么用下面这段代码就可以了。注意,建立连接的默认IP为localhost,默认port为10180

  1. VertxGremlinClient client = VertxGremlinClient.create(vertx, config)

建立远程客户端连接只需在上面示例代码中添加两个参数就可以了。如下所示:

  1. VertxGremlinClient client = VertxGremlinClient.create(vertx, config, ip, host)

vertx-gremlin-client API使用

脚本格式定义

脚本采用json作为传输的数据格式,其中至少要包含了两个字段graphId,gremlinScript,举例如下:

  1. {
  2. "graphId": "grapid-22554353",
  3. "gremlin": "g.V().count()",
  4. }

也支持参数绑定的方式,如下所示:

  1. {
  2. "graphId": "grapid-22554353",
  3. "gremlinScript": "g.V().property('country',aaa).property('addr',bbb).valueMap()",
  4. "parambindings"[
  5. "aaa":"aaaValue",
  6. "bbb":"bbbValue"
  7. ]
  8. }

如上所示的数据中,vertx-gremlin-server会将这些参数处理后,gremlinScript在被执行时的值会编程g.V().property('country',"aaaValue").property('addr',"aaaValue").valueMap()。注意这里的参数名aaa,bbb,要取名特别一点,最好不要取名为gremlinScript中出现的字符串。

执行一个脚本

  1. JsonObject script = new JsonObject();
  2. script.put("graphId", "grapid-22554353");
  3. script.put("gremlinScript", "g.V().property('country',aaa).property('addr',bbb).valueMap()");
  4. vertxGremlinClient.execute( script, res -> {
  5. if (res.succeeded()) {
  6. JsonObject resultJson = res.result();
  7. System.out.println("执行结果为:" + resultJson);
  8. } else {
  9. res.cause().printStackTrace();
  10. }
  11. });
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注