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

./gremlin-server.sh ../conf/gremlin-server/gremlin-server.yaml

创建新图

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

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

:remote console

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

map = new HashMap<String, Object>();
map.put("storage.backend", "cql");  #Cassandra
map.put("storage.hostname", "127.0.0.1");
map.put("graph.graphname", "graph1");
map.put("index.search.backend", "elasticsearch"); #es
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.vertices().size();

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"))

使用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默认的图名称是janusgraph即graph.graphname

storage.cql.keyspace=janusgraph
示例

gremlin> :remote connect tinkerpop.server conf/remote.yaml session
==>Configured localhost/127.0.0.1:8182

gremlin> :remote console
==>All scripts will now be sent to Gremlin Server - [localhost:8182]-[5206cdde-b231-41fa-9e6c-69feac0fe2b2] - type ':remote console' to return to local mode

gremlin> ConfiguredGraphFactory.open("graph");
Please create configuration for this graph using the
ConfigurationManagementGraph API.

gremlin> ConfiguredGraphFactory.create("graph");
Please create a template Configuration using the
ConfigurationManagementGraph API.

gremlin> map = new HashMap();
gremlin> map.put("storage.backend", "cql");
gremlin> map.put("storage.hostname", "127.0.0.1");
gremlin> map.put("GraphName", "graph1");
gremlin> ConfiguredGraphFactory.createConfiguration(new MapConfiguration(map));
Please include in your configuration the property "graph.graphname".

gremlin> map = new HashMap();
gremlin> map.put("storage.backend", "cql");
gremlin> map.put("storage.hostname", "127.0.0.1");
gremlin> map.put("graph.graphname", "graph1");
gremlin> ConfiguredGraphFactory.createConfiguration(new MapConfiguration(map));
==>null

gremlin> ConfiguredGraphFactory.open("graph1").vertices();

gremlin> map = new HashMap(); map.put("storage.backend",
"cql"); map.put("storage.hostname", "127.0.0.1");
gremlin> map.put("graph.graphname", "graph1");
gremlin> ConfiguredGraphFactory.createTemplateConfiguration(new MapConfiguration(map));
Your template configuration may not contain the property
"graph.graphname".

gremlin> map = new HashMap();
gremlin> map.put("storage.backend",
"cql"); map.put("storage.hostname", "127.0.0.1");
gremlin> ConfiguredGraphFactory.createTemplateConfiguration(new MapConfiguration(map));
==>null

// Each graph is now acting in unique keyspaces equivalent to the
graphnames.
gremlin> g1 = ConfiguredGraphFactory.open("graph1");
gremlin> g2 = ConfiguredGraphFactory.create("graph2");
gremlin> g3 = ConfiguredGraphFactory.create("graph3");
gremlin> g2.addVertex();
gremlin> l = [];
gremlin> l << g1.vertices().size();
==>0
gremlin> l << g2.vertices().size();
==>1
gremlin> l << g3.vertices().size();
==>0

// After a graph is created, you must access it using .open()
gremlin> g2 = ConfiguredGraphFactory.create("graph2"); g2.vertices().size();
Configuration for graph "graph2" already exists.

gremlin> g2 = ConfiguredGraphFactory.open("graph2"); g2.vertices().size();
==>1

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值