可以使用ConfiguredGraphFactory去配置JanusGraph Server。 ConfiguredGraphFactory是图的一种访问方式,类似于JanusGraphFactory。 这些图的工厂类提供了动态管理托管在服务器上的图的方法。
JanusGraphFactory是一个类,它提供了一种图的访问方式,每次通过配置Configuration对象来访问你的图。
ConfiguredGraphFactory提供了一种访问你使用ConfigurationManagementGraph创建配置的图的方式。 它还提供了一种方式去管理图的配置。
ConfigurationManagementGraph允许你去管理图的配置。
JanusGraphManager是一个内部服务组件,可以跟踪图的引用,前提是你的图是使用它配置的。
但是,这两个图工厂类之间存在一个重要区别:
使用ConfiguredGraphFactory的好处是:
ConfiguredGraphFactory在以下两种情况下提供图访问方式:
你可以使用ConfiguredGraphFactory.create(“graphName”)或ConfiguredGraphFactory.open(“graphName”)。 阅读以下有关ConfigurationManagementGraph的部分,了解这两种方式更多的不同之处。
你还可以使用绑定来访问图。 有关详细信息,请参见第9.10节“Graph and Traversal Bindings”部分。
ConfiguredGraphFactory.getGraphNames()将返回一组你使用ConfigurationManagementGraph API为其创建配置的图的名称,。
另一方面,JanusGraphFactory.getGraphNames()返回一组你实例化的图的名称,而且引用存储在JanusGraphManager中。
ConfiguredGraphFactory.drop(“graphName”)将删除图数据库,删除存储和索引中的所有数据。 这个图可以是打开的或着关闭的(将被关闭作为删除操作的一部分)。 此外,它还将删除ConfigurationManagementGraph中的任何已有图配置。
注意:
为了使用ConfiguredGraphFactory,你必须使用ConfigurationManagementGraph API来配置你的服务。 为此,你必须在你的服务的YAML文件的graphs中配置名为“ConfigurationManagementGraph”的图变量。 例如:
graphManager: org.janusgraph.graphdb.management.JanusGraphManager
graphs: {
ConfigurationManagementGraph: conf/JanusGraph-configurationmanagement.properties
}
在此示例中,我们的ConfigurationManagementGraph图将使用conf/JanusGraph-configurationmanagement.properties中存储的属性进行配置,例如,如下所示:
gremlin.graph=org.janusgraph.core.JanusGraphFactory
storage.backend=cql
graph.graphname=ConfigurationManagementGraph
storage.hostname=127.0.0.1
如果GremlinServer启动成功而且ConfigurationManagementGraph被成功实例化,则ConfigurationManagementGraph Singleton上的所有可用API也将对所述图执行操作。 此外,这个图可以使用ConfiguredGraphFactory的create/open来访问图的配置。
注意:
JanusGraph发行版中包含的pom.xml将此依赖项列为可选项,但ConfiguredGraphFactory使用了JanusGraphManager,它需要依赖org.apache.tinkerpop:gremlin-server。 因此,如果你遇到NoClassDefFoundError错误,请务必根据此提示在pom.xml中添加依赖。
ConfigurationManagementGraph是一个Singleton,允许你使用ConfiguredGraphFactory create/update/remove 图的配置。 请参阅上文有关配置服务以使用这些API。
注意:ConfiguredGraphFactory提供了一个通过ConfigurationManagementGraph来管理图配置的方法,因此你可以使用相应的ConfiguredGraphFactory静态方法,而不是对单例本身进行操作。 例如,你可以使用ConfiguredGraphFactory.removeTemplateConfiguration()而不是ConfiguredGraphFactory.getInstance().removeTemplateConfiguration()。
ConfigurationManagementGraph单例允许你通过graph.graphname属性来创建用于打开特定图的配置。 例如:
map = new HashMap<String, Object>();
map.put("storage.backend", "cql");
map.put("storage.hostname", "127.0.0.1");
map.put("graph.graphname", "graph1");
ConfiguredGraphFactory.createConfiguration(new MapConfiguration(map));
然后,你可以使用以下方式在任何JanusGraph节点上访问此图:
ConfiguredGraphFactory.open("graph1");
ConfigurationManagementGraph还允许你创建一个模板配置,你可以使用相同的配置模板创建许多图。 例如:
map = new HashMap<String, Object>();
map.put("storage.backend", "cql");
map.put("storage.hostname", "127.0.0.1");
ConfiguredGraphFactory.createTemplateConfiguration(new MapConfiguration(map));
完成此操作后,你可以使用模板配置创建图:
ConfiguredGraphFactory.create("graph2");
此方法将首先通过复制与模板配置关联的所有属性并将其存储在此特定图的配置上来为“graph2”创建新配置。 这意味着可以通过以下方式在任何JanusGraph节点上访问此图:
ConfiguredGraphFactory.open("graph2");
与JanusGraphFactory和ConfiguredGraphFactory的所有交互都与定义属性graph.graphname的配置交互,通过JanusGraphManager跟踪在给定JVM上创建的图引用。 将其视为图形缓存。 为此原因:
对图配置的任何更新都会导致从JanusGraph集群中每个节点上的图缓存中逐出相关图形,假设每个节点都已正确配置以使用JanusGraphManager。 详细了解此功能以及如何配置服务器以使用此功能。
由于使用模板配置创建的图形首先使用复制和创建方法为该图形创建配置,这意味着:
对使用模板配置创建的特定图表的任何更新都不能保证在特定图表上生效,直到:
<graphname> _traversal
绑定到上下文。 这意味着,在第一次创建/打开图形后,在后续连接到服务器时,你可以通过<graphname>
和<graphname> _traversal
属性访问图形和遍历引用。
详细了解此功能以及如何配置服务器以使用此功能。
注意:
10.1 Binding Example
gremlin> :remote connect tinkerpop.server conf/remote.yaml ==>Configured localhost/127.0.0.1:8182 gremlin> :remote console ==>All scripts will now be sent to Gremlin Server - [localhost/127.0.0.1:8182] - type ':remote console' to return to local mode gremlin> ConfiguredGraphFactory.open("graph1") ==>standardjanusgraph[cassandrathrift:[127.0.0.1]] gremlin> graph1 ==>standardjanusgraph[cassandrathrift:[127.0.0.1]] gremlin> graph1_traversal ==>graphtraversalsource[standardjanusgraph[cassandrathrift:[127.0.0.1]], standard]
11. 示例
建议在创建Configured Graph Factory模板时使用会话连接。 如果未使用会话连接,则必须使用分号将配置的图形工厂模板创建作为单行发送到服务器。 有关会话的详细信息,请参见第7.1.1.1节“连接到Gremlin Server”。
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<graphName>
等效的唯一键空间:
map = new HashMap(); map.put("storage.backend", "cql"); map.put("storage.hostname", "127.0.0.1"); ConfiguredGraphFactory.createTemplateConfiguration(new MapConfiguration(map)); g1 = ConfiguredGraphFactory.create("graph1"); //keyspace === graph1 g2 = ConfiguredGraphFactory.create("graph2"); //keyspace === graph2 g3 = ConfiguredGraphFactory.create("graph3"); //keyspace === graph3<storage.root> / <graph.graphname>
的唯一存储目录:
map = new HashMap(); map.put("storage.backend", "berkeleyje"); map.put("storage.root", "/data/graphs"); ConfiguredGraphFactory.createTemplateConfiguration(new MapConfiguration(map)); g1 = ConfiguredGraphFactory.create("graph1"); //storage directory === /data/graphs/graph1 g2 = ConfiguredGraphFactory.create("graph2"); //storage directory === /data/graphs/graph2 g3 = ConfiguredGraphFactory.create("graph3"); //storage directory === /data/graphs/graph3