janusgraph使用ConfiguredGraphFactory配置动态创建图

前言

如果之前是使用固定的一张或多张图,修改为使用动态创建不会影响之前的数据。

修改配置

编辑conf/germlin-server/gremlin-server/gremlin-server.yamll,添加graphManager 修改graphs的值.一般情况都是将Janusgraph作为服务端,应用作为客户端使用代码连接,所以需要修改channelizer并且需要修改ScriptFileGremlinPlugin,去掉里面的scripts/empty-sample.groovy脚本。

channelizer: org.janusgraph.channelizers.JanusGraphWebSocketChannelizer
graphManager: org.janusgraph.graphdb.management.JanusGraphManager
graphs: {
  ConfigurationManagementGraph: conf/JanusGraph-configurationmanagement.properties
}
scriptEngines: {
  gremlin-groovy: {
    plugins: {
               org.apache.tinkerpop.gremlin.jsr223.ScriptFileGremlinPlugin: {files: []}}}}

ConfigurationManagementGraph的值参考之前的graphs值。JanusGraph-configurationmanagement.properties配置大致如下

gremlin.graph=org.janusgraph.core.ConfiguredGraphFactory
storage.backend=cql
graph.graphname=ConfigurationManagementGraph
storage.hostname=127.0.0.1
index.search.backend=elasticsearch
index.search.hostname=127.0.0.1

重启janusgraph

su janusgraph
bin/janusgraph.sh stop
bin/janusgraph.sh start

创建新图

使用带session方式连接服务端,conf/remote.yaml文件位置根据自己情况设置,这里使用默认值

:remote connect tinkerpop.server conf/remote.yaml
:remote console

设置后端存储和后端索引,graph.graphname是图的名称类似MySQL中的库名,这里的名称需要唯一且在后面的打开图时需要使用

map = new HashMap<String, Object>();
map.put("storage.backend", "cql");
map.put("storage.hostname", "127.0.0.1");
map.put("graph.graphname", "graph1");
map.put("index.search.backend", "elasticsearch");
map.put("index.search.hostname", "127.0.0.1");
ConfiguredGraphFactory.createConfiguration(new MapConfiguration(map));

创建和打开图。只有首次使用需要使用create函数,如果非首次会提示图名称需要保证唯一,如果直接使用bash连接也可以不执行create,直接使用open

ConfiguredGraphFactory.create("graph1");
ConfiguredGraphFactory.open("graph1");

测试新图

添加一个节点测试新建图

gremlin> g1 = ConfiguredGraphFactory.open("graph1");
gremlin> g1.addVertex();
gremlin> g1.traversal().count();
gremlin> g1.close();

更新配置

map = new HashMap();
map.put("storage.hostname", "10.0.0.1");
ConfiguredGraphFactory.updateConfiguration("graph1", map);
g1 = ConfiguredGraphFactory.open("graph1");

使用模板创建新图

对于一个集群上的几张图存储和索引配置基本相同janusgraph支持使用模板来创建新图减少重复输入形同配置,这里不需要添加graph.graphname配置项,否则创建模板时会报错。

map = new HashMap<String, Object>();
map.put("storage.backend", "cql");
map.put("storage.hostname", "127.0.0.1");
map.put("index.search.backend", "elasticsearch");
map.put("index.search.hostname", "127.0.0.1");
ConfiguredGraphFactory.createTemplateConfiguration(new MapConfiguration(map));

创建和打开图的方式和之前的一样

ConfiguredGraphFactory.create("graph2");
ConfiguredGraphFactory.open("graph2");

使用模板创建的图在更新模板后janusgraph不保证配置能够更新到已经使用这个模板创建的图。需要手动删除图配置重新创建。

// replace key with map
ConfiguredGraphFactory.updateTemplateConfiguration(new MapConfiguration(map));
// Remove Configuration
ConfiguredGraphFactory.removeConfiguration("graph2");
// Recreate
ConfiguredGraphFactory.create("graph2");

使用shell测试远程连接

连接前需要保证图已经存在。同样的conf/remote.yaml根据自己情况设置;graph1_traversal = graph.graphname + ‘_traversal’,图和traversal是janusgraph自动绑定的

bin/gremlin.sh
gremlin> cluster = Cluster.open('conf/remote.yaml')
gremlin> graph = EmptyGraph.instance()
gremlin> g = graph.traversal().withRemote(DriverRemoteConnection.using(cluster, "graph1_traversal"))

如果前面的channelizerScriptFileGremlinPlugin设置有误在这则会报错,信息大致如下:

The traversal source [g] for alias [g] is not configured on the server.

使用java代码测试远程连接

这里使用的4.0 api,5.0 withRemote方法已经打上@deprecated注解。java代码如下 :

Cluster cluster = Cluster.open("classpath:remote.yaml") ;
Graph graph = EmptyGraph.instance();
GraphTraversalSource g = graph.traversal().withRemote(DriverRemoteConnection.using(cluster, "graph1_traversal"));
long count = g.V().count().next();

remote.yaml配置参考:

hosts: [localhost]
port: 8182
serializer: {
  className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0,
  config: { ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry] }}

添加以前使用的图

查看修改前的gremlin-server.yamll的graphs配置项中的图配置。跟踪到具体配置文件默认情况如下:

graphs: {
  graph: conf/gremlin-server/janusgraph-cql-es-server.properties
}

查看conf/gremlin-server/janusgraph-cql-es-server.properties直接使用这个配置文件中的配置按照之前创建新图的步骤打开即可。需要注意的是janusgraph默认的图名称是janusgraphgraph.graphname

storage.cql.keyspace=janusgraph

你可能感兴趣的:(janusgraph使用ConfiguredGraphFactory配置动态创建图)