Java连接远程Gremlin Server

1. 启动gremlin server,默认采用websocket通讯方式:

    gremlin-server.bat

可以采用不同的配置文件启动,配置文件在conf目录下,gremlin-server.bat <配置文件路径>

2. 配置客户端,参考 http://tinkerpop.apache.org/docs/current/reference/#connecting-via-java

加入依赖:

<dependency>

<groupId>org.apache.tinkerpop</groupId>

<artifactId>gremlin-driver</artifactId>

<version>3.3.4</version>

</dependency>

配置driver.yaml ,详细配置项参考 http://tinkerpop.apache.org/docs/current/reference/#connecting-via-java

**(connectionPool.channelizer取值默认是Channelizer.WebSocketChannelizer,还有一个是NIO,貌似和server的channelizer不是同一个概念,server配置成http的话,会导致连接不上)

**如果配置serializeResultToString=true的话,返回的对象类型都是字符串

hosts: [localhost]

port: 8182

serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0, config: { serializeResultToString: false }}


编写代码如下:

String config="driver.yaml";

try {

Cluster cluster = Cluster.open(config);

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

g.addV("test").property("name","john").iterate();

List<Vertex> list = g.V().has("name").toList();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

cluster.close();

}

这里默认采用的是TinkerGraph作为后端的存储实现,可以切换到别的实现,TinkerGraph是内存数据库,如果需要落盘,配置conf/tinkergraph-empty.properties如下:

gremlin.graph=org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph

gremlin.tinkergraph.graphLocation=d:/graph-data

gremlin.tinkergraph.graphFormat=gryo

gremlin.tinkergraph.vertexIdManager=LONG

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容