|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +description: SmartGraphs enable you to manage graphs at scale. |
| 4 | +title: ArangoDB SmartGraphs - Getting Started |
| 5 | +--- |
| 6 | +Getting started |
| 7 | +--------------- |
| 8 | + |
| 9 | +SmartGraphs **cannot use existing collections**. When switching to SmartGraph from an |
| 10 | +existing dataset you have to import the data into a fresh SmartGraph. |
| 11 | + |
| 12 | +All collections that are being used in SmartGraphs need to be part of the same |
| 13 | +`distributeShardslike` group. The `smartGraphAttribute` and the number of shards are immutable. |
| 14 | +The `smartGraphAttribute` attribute is used to inform the database how to shard data and, as a |
| 15 | +consequence, all vertices must have this attribute. The `_from` and `_to` attributes that |
| 16 | +point _from_ one document _to_ another document stored in vertex collections are set by |
| 17 | +default, following the same smart sharding pattern. |
| 18 | + |
| 19 | +## Create a SmartGraph |
| 20 | + |
| 21 | +In contrast to General Graphs we have to add more options when creating the |
| 22 | +SmartGraph. The two options `smartGraphAttribute` and `numberOfShards` are |
| 23 | +required and cannot be modified later. |
| 24 | + |
| 25 | +{% arangoshexample examplevar="examplevar" script="script" result="result" %} |
| 26 | + @startDocuBlockInline smartGraphCreateGraphHowTo1_cluster |
| 27 | + @EXAMPLE_ARANGOSH_OUTPUT{smartGraphCreateGraphHowTo1_cluster} |
| 28 | + var graph_module = require("@arangodb/smart-graph"); |
| 29 | + var graph = graph_module._create("myGraph", [], [], {smartGraphAttribute: "region", numberOfShards: 9}); |
| 30 | + graph_module._graph("myGraph"); |
| 31 | + ~graph_module._drop("myGraph"); |
| 32 | + @END_EXAMPLE_ARANGOSH_OUTPUT |
| 33 | + @endDocuBlock smartGraphCreateGraphHowTo1_cluster |
| 34 | +{% endarangoshexample %} |
| 35 | +{% include arangoshexample.html id=examplevar script=script result=result %} |
| 36 | + |
| 37 | +## Create a Disjoint SmartGraph |
| 38 | + |
| 39 | +In contrast to regular SmartGraphs we have to add one option when creating the |
| 40 | +graph. The boolean option `isDisjoint` is required, needs to be set to `true` |
| 41 | +and cannot be modified later. |
| 42 | + |
| 43 | +{% arangoshexample examplevar="examplevar" script="script" result="result" %} |
| 44 | + @startDocuBlockInline smartGraphCreateGraphHowToDisjoint1_cluster |
| 45 | + @EXAMPLE_ARANGOSH_OUTPUT{smartGraphCreateGraphHowToDisjoint1_cluster} |
| 46 | + var graph_module = require("@arangodb/smart-graph"); |
| 47 | + var graph = graph_module._create("myGraph", [], [], {smartGraphAttribute: "region", numberOfShards: 9, isDisjoint: true}); |
| 48 | + graph_module._graph("myGraph"); |
| 49 | + ~graph_module._drop("myGraph"); |
| 50 | + @END_EXAMPLE_ARANGOSH_OUTPUT |
| 51 | + @endDocuBlock smartGraphCreateGraphHowToDisjoint1_cluster |
| 52 | +{% endarangoshexample %} |
| 53 | +{% include arangoshexample.html id=examplevar script=script result=result %} |
| 54 | + |
| 55 | +## Add vertex collections |
| 56 | + |
| 57 | +This is analogous to General Graphs. Unlike with General Graphs, the |
| 58 | +**collections must not exist** when creating the SmartGraph. The SmartGraph |
| 59 | +module will create them for you automatically to set up the sharding for all |
| 60 | +these collections correctly. If you create collections via the SmartGraph |
| 61 | +module and remove them from the graph definition, then you may re-add them |
| 62 | +without trouble however, as they will have the correct sharding. |
| 63 | + |
| 64 | +{% arangoshexample examplevar="examplevar" script="script" result="result" %} |
| 65 | + @startDocuBlockInline smartGraphCreateGraphHowTo2_cluster |
| 66 | + @EXAMPLE_ARANGOSH_OUTPUT{smartGraphCreateGraphHowTo2_cluster} |
| 67 | + ~var graph_module = require("@arangodb/smart-graph"); |
| 68 | + ~var graph = graph_module._create("myGraph", [], [], {smartGraphAttribute: "region", numberOfShards: 9}); |
| 69 | + graph._addVertexCollection("shop"); |
| 70 | + graph._addVertexCollection("customer"); |
| 71 | + graph._addVertexCollection("pet"); |
| 72 | + graph_module._graph("myGraph"); |
| 73 | + ~graph_module._drop("myGraph", true); |
| 74 | + @END_EXAMPLE_ARANGOSH_OUTPUT |
| 75 | + @endDocuBlock smartGraphCreateGraphHowTo2_cluster |
| 76 | +{% endarangoshexample %} |
| 77 | +{% include arangoshexample.html id=examplevar script=script result=result %} |
| 78 | + |
| 79 | +## Define relations on the Graph |
| 80 | + |
| 81 | +Adding edge collections works the same as with General Graphs, but again, the |
| 82 | +collections are created by the SmartGraph module to set up sharding correctly |
| 83 | +so they must not exist when creating the SmartGraph (unless they have the |
| 84 | +correct sharding already). |
| 85 | + |
| 86 | +{% arangoshexample examplevar="examplevar" script="script" result="result" %} |
| 87 | + @startDocuBlockInline smartGraphCreateGraphHowTo3_cluster |
| 88 | + @EXAMPLE_ARANGOSH_OUTPUT{smartGraphCreateGraphHowTo3_cluster} |
| 89 | + ~var graph_module = require("@arangodb/smart-graph"); |
| 90 | + ~var graph = graph_module._create("myGraph", [], [], {smartGraphAttribute: "region", numberOfShards: 9}); |
| 91 | + ~graph._addVertexCollection("shop"); |
| 92 | + ~graph._addVertexCollection("customer"); |
| 93 | + ~graph._addVertexCollection("pet"); |
| 94 | + var rel = graph_module._relation("isCustomer", ["shop"], ["customer"]); |
| 95 | + graph._extendEdgeDefinitions(rel); |
| 96 | + graph_module._graph("myGraph"); |
| 97 | + ~graph_module._drop("myGraph", true); |
| 98 | + @END_EXAMPLE_ARANGOSH_OUTPUT |
| 99 | + @endDocuBlock smartGraphCreateGraphHowTo3_cluster |
| 100 | +{% endarangoshexample %} |
| 101 | +{% include arangoshexample.html id=examplevar script=script result=result %} |
| 102 | + |
| 103 | +## Using SatelliteCollections in SmartGraphs |
| 104 | + |
| 105 | +When creating a collection, you can decide whether it's a SatelliteCollection |
| 106 | +or not. For example, a vertex collection can be satellite as well. |
| 107 | +SatelliteCollections don't require sharding as the data will be distributed |
| 108 | +globally on all DB-Servers. The `smartGraphAttribute` is also not required. |
| 109 | + |
| 110 | +### Create a SmartGraph using SatelliteCollections |
| 111 | + |
| 112 | +In addition to the attributes you would set to create a SmartGraph, there is an |
| 113 | +additional attribute `satellites` you can optionally set. It needs to be an array of |
| 114 | +one or more collection names. These names can be used in edge definitions |
| 115 | +(relations) and these collections will be created as SatelliteCollections. |
| 116 | +However, all vertex collections on one side of the relation have to be of |
| 117 | +the same type - either all satellite or all smart. This is because `_from` |
| 118 | +and `_to` can have different types based on the sharding pattern. |
| 119 | + |
| 120 | +In this example, both vertex collections are created as SatelliteCollections. |
| 121 | + |
| 122 | +{% hint 'info' %} |
| 123 | +When providing a satellite collection that is not used in a relation, |
| 124 | +it will not be created. If you create the collection in a following |
| 125 | +request, only then the option will count. |
| 126 | +{% endhint %} |
| 127 | + |
| 128 | +{% arangoshexample examplevar="examplevar" script="script" result="result" %} |
| 129 | + @startDocuBlockInline hybridSmartGraphCreateGraphHowTo1_cluster |
| 130 | + @EXAMPLE_ARANGOSH_OUTPUT{hybridSmartGraphCreateGraphHowTo1_cluster} |
| 131 | + var graph_module = require("@arangodb/smart-graph"); |
| 132 | + var rel = graph_module._relation("isCustomer", "shop", "customer") |
| 133 | + var graph = graph_module._create("myGraph", [rel], [], {satellites: ["shop", "customer"], smartGraphAttribute: "region", numberOfShards: 9}); |
| 134 | + graph_module._graph("myGraph"); |
| 135 | + ~graph_module._drop("myGraph", true); |
| 136 | + @END_EXAMPLE_ARANGOSH_OUTPUT |
| 137 | + @endDocuBlock hybridSmartGraphCreateGraphHowTo1_cluster |
| 138 | +{% endarangoshexample %} |
| 139 | +{% include arangoshexample.html id=examplevar script=script result=result %} |
| 140 | + |
| 141 | +### Create a Disjoint SmartGraph using SatelliteCollections |
| 142 | + |
| 143 | +The option `isDisjoint` needs to be set to `true` in addition to the other |
| 144 | +options for a SmartGraph using SatelliteCollections. Only the `shop` vertex collection is created |
| 145 | +as a SatelliteCollection in this example: |
| 146 | + |
| 147 | +{% arangoshexample examplevar="examplevar" script="script" result="result" %} |
| 148 | + @startDocuBlockInline hybridSmartGraphCreateGraphHowTo2_cluster |
| 149 | + @EXAMPLE_ARANGOSH_OUTPUT{hybridSmartGraphCreateGraphHowTo2_cluster} |
| 150 | + var graph_module = require("@arangodb/smart-graph"); |
| 151 | + var rel = graph_module._relation("isCustomer", "shop", "customer") |
| 152 | + var graph = graph_module._create("myGraph", [rel], [], {satellites: ["shop"], smartGraphAttribute: "region", isDisjoint: true, numberOfShards: 9}); |
| 153 | + graph_module._graph("myGraph"); |
| 154 | + ~graph_module._drop("myGraph", true); |
| 155 | + @END_EXAMPLE_ARANGOSH_OUTPUT |
| 156 | + @endDocuBlock hybridSmartGraphCreateGraphHowTo2_cluster |
| 157 | +{% endarangoshexample %} |
| 158 | +{% include arangoshexample.html id=examplevar script=script result=result %} |
0 commit comments