2016-05-04 29 views
3

我是ArangoDB的全新品牌。我對Neo4J很熟悉,但是被ArangoDB的性能和多模型設計所吸引。文檔看起來很深,但是我很難入門。ArangoDB - 圖表製作基礎知識

我想知道一個簡單的方法來做一些基本的圖形操作。到目前爲止,我發現的所有內容都告訴我如何將整個集合連接在一起,但我希望能夠定義一個節點,定義另一個節點並定義它們之間的邊界。

通過HTTP理想

,我怎麼能:

  • 添加一個節點到圖形
  • 我已經添加到我的圖形
  • 創建集合並添加現有節點兩個節點之間創建邊緣該集合

舉個例子,我想創建一個簡單的圖形像這裏所示的樹:https://www.arangodb.com/2015/07/data-modeling-with-multi-model-databases/

Aircraft fleet maintenance example tree

我想了解如何創建此圖的子集的基本說明。我想:

  • 創建節點稱爲一個名爲fleets集合airline0airline1
  • 在名爲planes的集合中創建節點plane0,plane1,plane2。 - 在每個平面的文檔中放置一個任意屬性 - 比如顏色。
  • 在名爲pilots的集合中創建一個名爲Jennifer的節點。

接下來,我想連接圖。根據文檔,它看起來像邊緣本身就是文檔,因此可以具有屬性。我想創建以下的邊緣:

  • (airline0)-[owns]->(plane0)
  • (airline0)-[owns]->(plane1)該邊沿有since: 2013爲屬性
  • (airline1)-[owns]->(plane2)
  • (airline1)-[previouslyOwned]->(plane1)between: [1999,2013]
  • (plane0)-[inFleet]->(airline0)
  • (plane1)-[inFleet]->(airline0)
  • (plane1)-[wasInFleet]->(airline1)between: [1999,2013]
  • (plane2)-[inFleet]->(airline1)
  • (jennifer)-[canfly]->(plane0)
  • (plane0)-[hasPilot]->(jennifer)

請告訴我,我怎麼可以創建通過HTTP這樣的圖。如果不是HTTP,我想知道如何通過arangosh來做到這一點。

回答

8

讓我從arangosh開始,因爲它更易於閱讀。我將給出HTTP命令作爲附錄。

ArangoDB殼牌

您需要三個文件集合「艦隊」,「雙雄」,和你上面所說的「飛行員」和至少一個邊緣 收集保存圖形結構。如果你想遍歷在「擁有」和「inFleet」和「can」「之間跳躍的圖形結構,我建議 使用一個集合」關係「並給邊緣一個屬性」type「。

另一種解決方案是使用三個邊緣集合「擁有」和「inFleet」和「can」「。爲了提出更基於事實的建議,最好多瞭解一下你的用例。

arangosh [_system]> db._create("fleets"); 
[ArangoCollection 139792431, "fleets" (type document, status loaded)] 

arangosh [_system]> db._create("planes"); 
[ArangoCollection 140382255, "planes" (type document, status loaded)] 

arangosh [_system]> db._create("pilots"); 
[ArangoCollection 140972079, "pilots" (type document, status loaded)] 

arangosh [_system]> db._createEdgeCollection("relations"); 
[ArangoCollection 141103151, "relations" (type edge, status loaded)] 

接下來,在集合「車隊」中創建文檔。我將使用航空公司的名稱作爲關鍵。根據您的使用情況,可能會有更好的密鑰。例如,也許有一個通用的縮寫(如漢莎航空LH)。

arangosh [_system]> db.fleets.save({ _key: "airline0", name: "Airline 0" }); 
arangosh [_system]> db.fleets.save({ _key: "airline1", name: "Airline 1" }); 

重複的戰機和飛行員一樣:

arangosh [_system]> db.planes.save({ _key: "plane0", name: "Plane Zero", color: "red" }) 
arangosh [_system]> db.planes.save({ _key: "plane1", name: "Plane One", color: "red" }) 
arangosh [_system]> db.planes.save({ _key: "plane2", name: "Plane One", color: "green" }) 
arangosh [_system]> db.pilots.save({ _key: "jennifer", name: "Jenifer" }); 

您應該根據自己的使用情況選擇鍵。如果沒有「自然」鍵,則省略「_key」屬性。 ArangoDB將爲您生成一個唯一的密鑰。

接下來,添加上面創建的節點之間的關係。 ArangoDB 2.8中的語法與上面創建的文檔類似。另外, 您需要提供要連接的頂點的「from」和「to」鍵。

arangosh [_system]> db.relations.save("fleets/airline0", "planes/plane0", { type: 'owns' }); 
arangosh [_system]> db.relations.save("fleets/airline0", "planes/plane1", { type: 'owns', since: 2013 }); 
arangosh [_system]> db.relations.save("fleets/airline1", "planes/plane2", { type: 'owns' }); 
arangosh [_system]> db.relations.save("fleets/airline1", "planes/plane1", { type: 'previouslyOwned', begin: 1999, end: 2013 }); 
arangosh [_system]> db.relations.save("pilots/jennifer", "planes/plane0", { type: 'canfly' }); 

如果「inFleet」 /「wasInFleet」 /「hasPilot」是的反向「擁有」 /「previouslyOwned」 /「canfly」,比你並不需要創建一個單獨的邊緣了,因爲邊緣是定向的。

如果有「擁有」和「inFleet」你可以創建類似上述的關係之間的差異:

arangosh [_system]> db.relations.save("planes/plane0", "fleets/airline0", { type: 'inFleet' }); 
... 

現在,爲了遵循路徑「詹妮弗能飛PLANEX通過airlineY擁有」使用:

arangosh> db._query("FOR v, e IN OUTBOUND 'pilots/jennifer' relations FILTER e.type == 'canfly' FOR w, f IN INBOUND v relations FILTER f.type == 'owns' RETURN { plane: v, airline: w }") 
[ 
    { 
    "plane" : { 
     "color" : "red", 
     "name" : "Plane Zero", 
     "_id" : "planes/plane0", 
     "_rev" : "153686063", 
     "_key" : "plane0" 
    }, 
    "airline" : { 
     "name" : "Airline 0", 
     "_id" : "fleets/airline0", 
     "_rev" : "149884975", 
     "_key" : "airline0" 
    } 
    } 
] 

或者扭轉路徑(不使用 'inFleet' 和 'hasPilot'):

arangosh> db._query("FOR v, e IN OUTBOUND 'fleets/airline0' relations FILTER e.type == 'owns' FOR w, f IN INBOUND v relations FILTER f.type == 'canfly' RETURN { plane: v, w: w }") 
[ 
    { 
    "plane" : { 
     "color" : "red", 
     "name" : "Plane Zero", 
     "_id" : "planes/plane0", 
     "_rev" : "153686063", 
     "_key" : "plane0" 
    }, 
    "w" : { 
     "_id" : "pilots/jennifer", 
     "_rev" : "330240047", 
     "_key" : "jennifer" 
    } 
    } 
] 

HTTP

讓我舉例說明上面執行的各種類型的命令。

arangosh [_system]> db._create("fleets"); 

這轉化爲

curl -X POST --data-binary @- --dump - http://localhost:8529/_api/collection <<EOF 
{ 
    "name" : "fleets" 
} 
EOF 

接着

arangosh [_system]> db._createEdgeCollection("relations"); 

轉化爲

curl -X POST --data-binary @- --dump - http://localhost:8529/_api/collection <<EOF 
{ 
    "name" : "relations", "type": 3 
} 
EOF 

接着

arangosh [_system]> db.fleets.save({ _key: "airline0", name: "Airline 0" }); 

轉化爲

curl -X POST --data-binary @- --dump - http://localhost:8529/_api/document?collection=products <<EOF 
{ "_key": "airline0", "name": "Airline 0" } 
EOF 

下一頁

db.relations.save("pilots/jennifer", "planes/plane0", { type: 'canfly' }); 

轉化爲

curl -X POST --data-binary @- --dump - http://localhost:8529/_api/edge/?collection=relations&from=pilots/jennifer&to=planes/plane0 <<EOF 
{ 
    "type" : "canfly" 
} 
EOF 
+1

完美!謝謝!在深入研究文檔後,我發現了其中的一些內容,但直到現在,我還不清楚這種遍歷。我很想了解更多有關單個邊緣集合與多個集合的優勢。我可以遍歷多個邊緣集合嗎?或者我只能在一個內部遍歷? –

+1

另一個問題:'graph._addVertexCollection'和'db._create'有什麼區別? –

+0

ArangoDB支持有名和無名圖形。命名圖形用「圖形...」創建。您還可以使用使用「db ...」創建的邊緣集合的匿名圖形。是的,你可以使用多個邊緣集合來遍歷。由於評論的大小受到限制,因此可以通過https://groups.google.com/forum/#!forum/arangodb繼續進行討論。 – fceller