Chapter 9. ConfiguredGraphFactory

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ū)分如下:

  1. 如果你在server啟動(dòng)時(shí)配置了使用ConfigurationManagementGraphAPIS拯啦,才能用ConfiguredGraphFactory

使用ConfiguredGraphFactory好處如下:

  1. 你只需要提供一個(gè)String來訪問你的圖熔任,而JanusGraphFactory在每次打開一個(gè)圖褒链、訪問圖時(shí)需要你指定后端信息。
  2. 如果你的ConfigurationManagementGraph配置了一個(gè)分布式的后端疑苔,你的圖配置在cluster中所有janusgraph節(jié)點(diǎn)可用甫匹。

9.3 ConfiguredGraphFactory如何工作

在下面兩種場(chǎng)景里,ConfiguredGraphFactory提供訪問點(diǎn):

  1. 你已經(jīng)用ConfigurationManagementGraph#createConfiguration給你的圖對(duì)象創(chuàng)建了配置文件。在這種場(chǎng)景兵迅,你的圖是打開的哀墓,基于前面創(chuàng)建的配置。
  2. 你已經(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 更新配置

所有JanusGraphFactoryJanusGraphFactory的交互配置需要定義準(zhǔn)確的graph.graphname八孝,通過JanusGraphManager保持追蹤給定JVM里相關(guān)的圖董朝,可以認(rèn)為是圖緩存。

重要提示

集群中每個(gè)節(jié)點(diǎn)的圖緩存干跛,任何的更新對(duì)相關(guān)的圖影響是相互的子姜,前提是每個(gè)節(jié)點(diǎn)正確配置JanusGraphManagerconsistency

因此用模板創(chuàng)建圖是有重復(fù)的問題的楼入,這意味著:

重要提示

用模板創(chuàng)建的圖的任何更新并不保證生效哥捕,直到滿足:

  1. 相關(guān)配置被移除:ConfiguredGraphFactory.removeConfiguration("graph2")
  2. 用模板重新創(chuàng)建:ConfiguredGraphFactory.create("graph2")
9.8.4 更新例子
  1. 變更使用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");
  1. 增加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");
  1. 更新用模板創(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提供:

  1. 一個(gè)協(xié)作機(jī)制,哪個(gè)節(jié)點(diǎn)實(shí)例化相關(guān)圖
  2. 相關(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.propertiesconf/graph2.properties將不包括準(zhǔn)確的graph.graphname秆撮,圖將存在JanusGraphManager四濒,在gremlin腳本執(zhí)行時(shí)分別對(duì)應(yīng)graph1graph2

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ù)后面。

下面是使用例子:

  1. 給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
  1. 給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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市赞厕,隨后出現(xiàn)的幾起案子艳狐,更是在濱河造成了極大的恐慌,老刑警劉巖皿桑,帶你破解...
    沈念sama閱讀 218,546評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件毫目,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡诲侮,警方通過查閱死者的電腦和手機(jī)唉堪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門误窖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事前酿≡虮保” “怎么了筑舅?”我有些...
    開封第一講書人閱讀 164,911評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵媒吗,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我坝疼,道長(zhǎng)窖杀,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,737評(píng)論 1 294
  • 正文 為了忘掉前任裙士,我火速辦了婚禮入客,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘腿椎。我一直安慰自己桌硫,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,753評(píng)論 6 392
  • 文/花漫 我一把揭開白布啃炸。 她就那樣靜靜地躺著铆隘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪南用。 梳的紋絲不亂的頭發(fā)上膀钠,一...
    開封第一講書人閱讀 51,598評(píng)論 1 305
  • 那天掏湾,我揣著相機(jī)與錄音,去河邊找鬼肿嘲。 笑死融击,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的雳窟。 我是一名探鬼主播尊浪,決...
    沈念sama閱讀 40,338評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼封救!你這毒婦竟也來了拇涤?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,249評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤誉结,失蹤者是張志新(化名)和其女友劉穎鹅士,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體惩坑,經(jīng)...
    沈念sama閱讀 45,696評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡掉盅,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,888評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了旭贬。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,013評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡搪泳,死狀恐怖稀轨,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情岸军,我是刑警寧澤奋刽,帶...
    沈念sama閱讀 35,731評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站艰赞,受9級(jí)特大地震影響佣谐,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜方妖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,348評(píng)論 3 330
  • 文/蒙蒙 一狭魂、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧党觅,春花似錦雌澄、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至魁莉,卻和暖如春睬涧,著一層夾襖步出監(jiān)牢的瞬間募胃,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評(píng)論 1 270
  • 我被黑心中介騙來泰國打工畦浓, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留痹束,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,203評(píng)論 3 370
  • 正文 我出身青樓宅粥,卻偏偏與公主長(zhǎng)得像参袱,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子秽梅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,960評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容