server能被配置ConfiguredGraphFactory朵夏。ConfiguredGraphFactory是一個(gè)圖的訪問點(diǎn)士鸥,類似于JanusGraphFactory赁项。這些factories提供方法動(dòng)態(tài)管理圖籽暇。
9.1 總覽
JanusGraphFactory是一個(gè)提供訪問點(diǎn)到你的圖的class蛾洛,通過你每次訪問圖提供一個(gè)配置object养铸。
ConfiguredGraphFactory提供是一個(gè)提供訪問點(diǎn)到你的圖,用于你之前ConfigurationManagementGraph創(chuàng)建的配置轧膘。它也提供訪問點(diǎn)給管理圖配置钞螟。
ConfigurationManagementGraph允許你管理圖配置。
JanusGraphManager是一個(gè)內(nèi)部的服務(wù)器組件谎碍,用來追蹤圖相關(guān)鳞滨,前提是您的圖形被配置為使用它。
9.2 JanusGraphManager與JanusGraphFactory
這里蟆淀,兩個(gè)工廠類有重要的區(qū)分如下:
- 如果你在server啟動(dòng)時(shí)配置了使用ConfigurationManagementGraphAPIS拯啦,才能用ConfiguredGraphFactory。
使用ConfiguredGraphFactory好處如下:
- 你只需要提供一個(gè)String來訪問你的圖熔任,而JanusGraphFactory在每次打開一個(gè)圖褒链、訪問圖時(shí)需要你指定后端信息。
- 如果你的ConfigurationManagementGraph配置了一個(gè)分布式的后端疑苔,你的圖配置在cluster中所有janusgraph節(jié)點(diǎn)可用甫匹。
9.3 ConfiguredGraphFactory如何工作
在下面兩種場(chǎng)景里,ConfiguredGraphFactory提供訪問點(diǎn):
- 你已經(jīng)用ConfigurationManagementGraph#createConfiguration給你的圖對(duì)象創(chuàng)建了配置文件。在這種場(chǎng)景兵迅,你的圖是打開的哀墓,基于前面創(chuàng)建的配置。
- 你已經(jīng)用ConfigurationManagementGraph#createTemplateConfiguration創(chuàng)建了模板配置喷兼。在這種場(chǎng)景,我們創(chuàng)建一個(gè)配置文件給你創(chuàng)建的圖后雷,通過拷貝模板配置中所有的參數(shù)季惯,并且附加相關(guān)的圖名稱屬性,然后會(huì)根據(jù)這個(gè)配置打開圖臀突。
9.4 訪問圖
你可以用ConfiguredGraphFactory.create("graphName")或者ConfiguredGraphFactory.open("graphName")勉抓。兩種的區(qū)別可以參考下面的ConfigurationManagementGraph。
你可以可以訪問圖通過binding候学,可以參考Graph and Traversal Bindings藕筋。
9.5 列表展示圖
ConfiguredGraphFactory.getGraphNames()將返回圖名稱的集合,這些圖根據(jù)你用ConfigurationManagementGraphAPIs創(chuàng)建的梳码。
JanusGraphFactory.getGraphNames()返回圖名稱的集合隐圾,那些你用JanusGraphManager實(shí)例化的,和參考引入的掰茶。
9.6 丟棄圖
ConfiguredGraphFactory.drop("graphName")會(huì)丟棄圖暇藏,刪除所有存儲(chǔ)和索引后端數(shù)據(jù),圖可以是打開或者關(guān)閉狀態(tài)(drop會(huì)先close)濒蒋。而且盐碱,還將移除ConfigurationManagementGraph里的所有圖配置。
重要提示
這將是不可恢復(fù)的沪伙,將刪除所有圖和索引的數(shù)據(jù)瓮顽。
重要提示
確保所有圖表達(dá)持續(xù)可用于集群中所有節(jié)點(diǎn),這個(gè)操作會(huì)從集群中所有節(jié)點(diǎn)的JanusGraphManager緩存中移除围橡,前提是所有節(jié)點(diǎn)的JanusGraphManager配置正確暖混。consistency來了解更多。
9.7為ConfiguredGraphFactory配置你的server
為能用ConfiguredGraphFactory某饰,你必須用ConfigurationManagementGraphAPIs配置你的server儒恋,你必須插入名為ConfigurationManagementGraph的圖變量到你的server yaml文件,例如:
graphManager: org.janusgraph.graphdb.management.JanusGraphManager
graphs: {
ConfigurationManagementGraph: conf/JanusGraph-configurationmanagement.properties
}
這個(gè)例子里黔漂,我們的ConfigurationManagementGraph圖用conf/JanusGraph-configurationmanagement.properties作為配置文件诫尽,內(nèi)容如下:
gremlin.graph=org.janusgraph.core.JanusGraphFactory
storage.backend=cql
graph.graphname=ConfigurationManagementGraph
storage.hostname=127.0.0.1
假設(shè)server啟動(dòng)成功,ConfigurationManagementGraph完成實(shí)例化炬守,所有ConfigurationManagementGraph Singleton里的APIs都可用牧嫉,將實(shí)現(xiàn)上面的圖。這個(gè)圖將使用ConfiguredGraphFactory來create/open圖。
重要提示
JanusGraph發(fā)行版中包含的pom.xml將此依賴項(xiàng)列為可選項(xiàng)酣藻,但ConfiguredGraphFactory使用了JanusGraphManager,它需要聲明對(duì)org.apache.tinkerpop:gremlin-server的依賴曹洽。因此,如果您遇到NoClassDefFoundError錯(cuò)誤辽剧,那么請(qǐng)確保根據(jù)此消息進(jìn)行更新送淆。
9.8 ConfigurationManagementGraph
ConfigurationManagementGraph是一個(gè)單例,允許你create/update/remove配置怕轿,用ConfiguredGraphFactory訪問圖的配置偷崩,看上面的內(nèi)容確保你能使用APIs。
重要提示
ConfiguredGraphFactory提供一個(gè)訪問點(diǎn)撞羽,去管理你的ConfigurationManagementGraph管理的配置阐斜,所以并不是直接操作你的單例,而是通過ConfiguredGraphFactory靜態(tài)方法操作诀紊。例如你可以用ConfiguredGraphFactory.removeTemplateConfiguration()谒出,而不是ConfiguredGraphFactory.getInstance().removeTemplateConfiguration()。
9.8.1 圖配置
ConfigurationManagementGraph單例允許你創(chuàng)建配置打開指定的圖邻奠,根據(jù)graph.graphname property笤喳,例如:
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));
然后你可以在任意節(jié)點(diǎn)訪問圖:
ConfiguredGraphFactory.open("graph1");
9.8.2 配置模板
ConfigurationManagementGraph也允許你創(chuàng)建一個(gè)配置模板,你可以用同一個(gè)模板創(chuàng)建許多圖碌宴,例如:
map = new HashMap<String, Object>();
map.put("storage.backend", "cql");
map.put("storage.hostname", "127.0.0.1");
ConfiguredGraphFactory.createTemplateConfiguration(new MapConfiguration(map));
這之后莉测,你可以用模板創(chuàng)建圖:
ConfiguredGraphFactory.create("graph2");
這個(gè)方法將創(chuàng)建一個(gè)graph2的新配置,通過拷貝模板配置中所有相關(guān)參數(shù)唧喉,和為這個(gè)圖存儲(chǔ)一個(gè)配置捣卤。這樣這個(gè)圖就可以被訪問,下一步可以操作:
ConfiguredGraphFactory.open("graph2");
9.8.3 更新配置
所有JanusGraphFactory和JanusGraphFactory的交互配置需要定義準(zhǔn)確的graph.graphname八孝,通過JanusGraphManager保持追蹤給定JVM里相關(guān)的圖董朝,可以認(rèn)為是圖緩存。
重要提示
集群中每個(gè)節(jié)點(diǎn)的圖緩存干跛,任何的更新對(duì)相關(guān)的圖影響是相互的子姜,前提是每個(gè)節(jié)點(diǎn)正確配置JanusGraphManager。consistency
因此用模板創(chuàng)建圖是有重復(fù)的問題的楼入,這意味著:
重要提示
用模板創(chuàng)建的圖的任何更新并不保證生效哥捕,直到滿足:
- 相關(guān)配置被移除:ConfiguredGraphFactory.removeConfiguration("graph2")
- 用模板重新創(chuàng)建:ConfiguredGraphFactory.create("graph2")
9.8.4 更新例子
- 變更使用Cassandra數(shù)據(jù)到新的ip server
map = new HashMap();
map.put("storage.backend", "cql");
map.put("storage.hostname", "127.0.0.1");
map.put("graph.graphname", "graph1");
ConfiguredGraphFactory.createConfiguration(new
MapConfiguration(map));
g1 = ConfiguredGraphFactory.open("graph1");
// Update configuration
map = new HashMap();
map.put("storage.hostname", "10.0.0.1");
ConfiguredGraphFactory.updateConfiguration("graph1",
map);
// We are now guaranteed to use the updated configuration
g1 = ConfiguredGraphFactory.open("graph1");
- 增加es節(jié)點(diǎn)到配置里
map = new HashMap();
map.put("storage.backend", "cql");
map.put("storage.hostname", "127.0.0.1");
map.put("graph.graphname", "graph1");
ConfiguredGraphFactory.createConfiguration(new
MapConfiguration(map));
g1 = ConfiguredGraphFactory.open("graph1");
// Update configuration
map = new HashMap();
map.put("index.search.backend", "elasticsearch");
map.put("index.search.hostname", "127.0.0.1");
map.put("index.search.elasticsearch.transport-scheme", "http");
ConfiguredGraphFactory.updateConfiguration("graph1",
map);
// We are now guaranteed to use the updated configuration
g1 = ConfiguredGraphFactory.open("graph1");
- 更新用模板創(chuàng)建的配置
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");
// Update template configuration
map = new HashMap();
map.put("index.search.backend", "elasticsearch");
map.put("index.search.hostname", "127.0.0.1");
map.put("index.search.elasticsearch.transport-scheme", "http");
ConfiguredGraphFactory.updateTemplateConfiguration(new
MapConfiguration(map));
// Remove Configuration
ConfiguredGraphFactory.removeConfiguration("graph1");
// Recreate
ConfiguredGraphFactory.create("graph1");
// Now this graph's configuration is guaranteed to be updated
9.9 JanusGraphManager
JanusGraphManager是一個(gè)單例,依附于TinkerPop graphManager的規(guī)范嘉熊。
特定的遥赚,JanusGraphManager提供:
- 一個(gè)協(xié)作機(jī)制,哪個(gè)節(jié)點(diǎn)實(shí)例化相關(guān)圖
- 相關(guān)圖追蹤器(或緩存)
任何用graph.graphname屬性創(chuàng)建的圖阐肤,將通過JanusGraphManager確定協(xié)作來實(shí)例化凫佛,相關(guān)圖也會(huì)放到這個(gè)JVM的圖緩存中讲坎。
因而,你用graph.graphname屬性打開的任意圖愧薛,已經(jīng)在指定JVM中實(shí)例化晨炕,并將從緩存中可以復(fù)用。
這也是為什么更新配置時(shí)需要一些步驟保證生效毫炉。
9.9.1 如何使用JanusGraphManager
當(dāng)在配置文件中定義參數(shù)如何訪問圖瓮栗,有一些配置選項(xiàng)可以使用。所有配置將在JanusGraphManager進(jìn)行圖實(shí)例化發(fā)生時(shí)生效瞄勾。
后置兼容性考慮遵馆,任何圖只支持配置在yaml文件中的圖對(duì)象,在server啟動(dòng)時(shí)加載丰榴,那些圖將通過JanusGraphManager加載他們的key和對(duì)象配置,如:
graphManager: org.janusgraph.graphdb.management.JanusGraphManager
graphs {
graph1: conf/graph1.properties,
graph2: conf/graph2.properties
}
conf/graph1.properties和conf/graph2.properties將不包括準(zhǔn)確的graph.graphname秆撮,圖將存在JanusGraphManager四濒,在gremlin腳本執(zhí)行時(shí)分別對(duì)應(yīng)graph1和graph2。
9.9.2 重要的
為了方便职辨,如果你的配置文件用來打開一個(gè)指定graph.graphname的圖盗蟆,但沒有規(guī)定后端存儲(chǔ)目錄,表名和域名空間舒裤,那這些相關(guān)參數(shù)會(huì)自動(dòng)設(shè)置給graph.graphname喳资。然后如果你提供了參數(shù),將會(huì)優(yōu)先使用腾供。如果都沒有提供仆邓,會(huì)使用默認(rèn)值。
storage.root是一個(gè)特殊例子伴鳖,這個(gè)是新配置用來指定根目錄节值,用來給后端存儲(chǔ)需要的本地訪問存儲(chǔ)目錄。如果你提供了這個(gè)參數(shù)榜聂,那么也必須提供準(zhǔn)確的graph.graphname搞疗,存儲(chǔ)的絕對(duì)路徑將等于graph.graphname的參數(shù)附加到storage.root參數(shù)后面。
下面是使用例子:
- 給Cassandra創(chuàng)建模板配置须肆,每個(gè)創(chuàng)建的圖獲得一個(gè)獨(dú)立的域名空間匿乃,等于提供給factory的String <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
- 給BerkeleyJE創(chuàng)建模板配置,每個(gè)圖的存儲(chǔ)目錄等于<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
9.10 圖和遍歷binding
使用ConfiguredGraphFactory創(chuàng)建的圖豌汇,用graph.graphname綁定到server的執(zhí)行器上下文幢炸,而圖的遍歷綁定到上下文<graphname>_traversal。這意味著拒贱,在第一次create/open圖之后阳懂,隨后連接到server你可以訪問或者遍歷圖用<graphname>和<graphname>_traversal。
從這里了解更多bingding
重要提示
如果你用console和session的連接到一個(gè)遠(yuǎn)端server,你將必須重連server來bind變量岩调,這也適用于所有session的連接巷燥。
重要提示
JanusGraphManagerrebindConfigurationManagementGraph里的每個(gè)圖每20秒,這意味著使用ConfigredGraphFactory創(chuàng)建的你的圖和遍歷binding將在所有節(jié)點(diǎn)最多有20秒的延遲后可用号枕。也意味著server重啟后在節(jié)點(diǎn)binding仍然可用缰揪。
9.10.1 binding例子
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]
9.11 例子
當(dāng)創(chuàng)建一個(gè)Configured Graph Factory模板時(shí)建議使用session的連接,如果模板沒有用session連接葱淳,創(chuàng)建必須用;分隔的一行里钝腺。
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