Nat*_*ner 3 http graph-databases arangodb
我是ArangoDB的新手.我对Neo4J很熟悉,但他对ArangoDB的性能和多模型设计很感兴趣.文档似乎非常深刻,但我开始时遇到了麻烦.
我想知道一种简单的方法来做一些基本的图形操作.到目前为止我发现的所有内容都告诉我如何将整个集合连接在一起,但我希望能够简单地定义一个节点,定义另一个节点,并定义它们之间的边缘.
理想情况下,通过HTTP,我该怎么做:
作为一个例子,我想创建一个简单的图形,如下图所示的树:https://www.arangodb.com/2015/07/data-modeling-with-multi-model-databases/
我想了解如何创建此图的子集的基本说明.我想要:
airline0和airline1一个名为集合fleets.plane0,plane1,plane2集合中叫planes. - 在每个平面的文档中放置一个任意属性 - 比方说颜色.Jennifer的集合中创建一个调用的节点pilots.接下来,我想连接图表.根据文档,它看起来像边缘本身是文档,因此可以具有属性.我想创建以下边缘:
(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知道如何做到这一点.
让我从arangosh开始,因为它更容易阅读.我将把HTTP命令作为附录.
ArangoDB Shell
如上所述,您将需要三个文档集"fleets","plane"和"pilot"以及至少一个用于保存图形结构的边集合.如果你想遍历在"拥有"和"inFleet"和"canfly"之间跳跃的图形结构,我建议使用一个集合"关系"并给边缘一个属性"类型".
另一种解决方案是使用三个边缘集合"拥有"和"inFleet"和"canfly".为了进行更基于事实的推荐,最好了解一下您的用例.
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)]
Run Code Online (Sandbox Code Playgroud)
接下来,在集合"fleets"中创建文档.我将使用航空公司的名称作为关键.根据您的使用情况,可能有更好的密钥.例如,可能存在通用缩写(如汉莎航空的LH).
arangosh [_system]> db.fleets.save({ _key: "airline0", name: "Airline 0" });
arangosh [_system]> db.fleets.save({ _key: "airline1", name: "Airline 1" });
Run Code Online (Sandbox Code Playgroud)
对飞机和飞行员重复相同的操作:
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" });
Run Code Online (Sandbox Code Playgroud)
您应该根据您的用例选择密钥.如果没有"自然"键,则省略"_key"属性.ArangoDB将为您生成一个唯一的密钥.
接下来,添加上面创建的节点之间的关系.ArangoDB 2.8中的语法类似于上面创建的文档.此外,您需要提供要连接的顶点的"从"和"到"键.
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' });
Run Code Online (Sandbox Code Playgroud)
如果'inFleet'/'wasInFleet'/'hasPilot'与'owns'/'previousOwned'/'canfly'相反,则不需要为它创建单独的边,因为边是有方向性的.
如果'owns'和'inFleet'之间存在差异,您可以创建类似于上述的关系:
arangosh [_system]> db.relations.save("planes/plane0", "fleets/airline0", { type: 'inFleet' });
...
Run Code Online (Sandbox Code Playgroud)
现在为了遵循"jennifer可以乘飞机Y拥有的飞机X"的路径,使用:
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"
}
}
]
Run Code Online (Sandbox Code Playgroud)
或者反转路径(不使用'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"
}
}
]
Run Code Online (Sandbox Code Playgroud)
HTTP
让我举例说明上面执行的各种类型的命令.
arangosh [_system]> db._create("fleets");
Run Code Online (Sandbox Code Playgroud)
这转化为
curl -X POST --data-binary @- --dump - http://localhost:8529/_api/collection <<EOF
{
"name" : "fleets"
}
EOF
Run Code Online (Sandbox Code Playgroud)
下一个
arangosh [_system]> db._createEdgeCollection("relations");
Run Code Online (Sandbox Code Playgroud)
翻译成
curl -X POST --data-binary @- --dump - http://localhost:8529/_api/collection <<EOF
{
"name" : "relations", "type": 3
}
EOF
Run Code Online (Sandbox Code Playgroud)
下一个
arangosh [_system]> db.fleets.save({ _key: "airline0", name: "Airline 0" });
Run Code Online (Sandbox Code Playgroud)
翻译成
curl -X POST --data-binary @- --dump - http://localhost:8529/_api/document?collection=products <<EOF
{ "_key": "airline0", "name": "Airline 0" }
EOF
Run Code Online (Sandbox Code Playgroud)
下一个
db.relations.save("pilots/jennifer", "planes/plane0", { type: 'canfly' });
Run Code Online (Sandbox Code Playgroud)
翻译成
curl -X POST --data-binary @- --dump - http://localhost:8529/_api/edge/?collection=relations&from=pilots/jennifer&to=planes/plane0 <<EOF
{
"type" : "canfly"
}
EOF
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1497 次 |
| 最近记录: |