github.com/dgraph-io/dgraph@v1.2.8/wiki/content/get-started-old/index.md (about) 1 +++ 2 title = "Get Started (Old)" 3 +++ 4 5 {{% notice "note" %}}This is an older version of the Getting Started page. You can see the latest page [here]({{< relref "get-started/index.md" >}}). {{% /notice %}} 6 7 ## Dgraph 8 9 Dgraph cluster consists of different nodes (Zero, Alpha & Ratel) and each node serves a different purpose. 10 11 **Dgraph Zero** controls the Dgraph cluster, assigns servers to a group and re-balances data between server groups. 12 13 **Dgraph Alpha** hosts predicates and indexes. 14 15 **Dgraph Ratel** serves the UI to run queries, mutations & altering schema. 16 17 You need at least one Dgraph Zero and one Dgraph Alpha to get started. 18 19 **Here's a 3 step tutorial to get you up and running.** 20 21 This is a quick-start guide to running Dgraph. For an interactive walk through, take the [tour](https://tour.dgraph.io). 22 23 You can see the accompanying [video here](https://www.youtube.com/watch?v=QIIdSp2zLcs). 24 25 ## Step 1: Install Dgraph 26 27 Dgraph can be installed from the install scripts, or run via Docker. 28 29 ### From Docker Image 30 31 Pull the Dgraph Docker images [from here](https://hub.docker.com/r/dgraph/dgraph/). From a terminal: 32 33 ```sh 34 docker pull dgraph/dgraph 35 ``` 36 37 ### From Install Scripts (Linux/Mac) 38 39 Install the binaries with 40 41 ```sh 42 curl https://get.dgraph.io -sSf | bash 43 ``` 44 45 The script automatically installs Dgraph. Once done, jump straight to [step 2]({{< relref "#step-2-run-dgraph" >}}). 46 47 **Alternative:** To mitigate potential security risks, instead try: 48 49 ```sh 50 curl https://get.dgraph.io > /tmp/get.sh 51 vim /tmp/get.sh # Inspect the script 52 sh /tmp/get.sh # Execute the script 53 ``` 54 55 You can check that Dgraph binary installed correctly by running `dgraph` and 56 looking at its output, which includes the version number. 57 58 ### Installing on Windows 59 60 {{% notice "note" %}}Binaries for Windows are available from `v0.8.3`.{{% /notice %}} 61 62 If you wish to install the binaries on Windows, you can get them from the [Github releases](https://github.com/dgraph-io/dgraph/releases), extract and install them manually. The file `dgraph-windows-amd64-v1.x.y.tar.gz` contains the dgraph binary. 63 64 ## Step 2: Run Dgraph 65 {{% notice "note" %}} This is a set up involving just one machine. For multi-server setup, go to [Deploy]({{< relref "deploy/index.md" >}}). {{% /notice %}} 66 67 ### Docker Compose 68 69 The easiest way to get Dgraph up and running is using Docker Compose. Follow the instructions 70 [here](https://docs.docker.com/compose/install/) to install Docker Compose if you don't have it 71 already. 72 73 ``` 74 version: "3.2" 75 services: 76 zero: 77 image: dgraph/dgraph:latest 78 volumes: 79 - type: volume 80 source: dgraph 81 target: /dgraph 82 volume: 83 nocopy: true 84 ports: 85 - 5080:5080 86 - 6080:6080 87 restart: on-failure 88 command: dgraph zero --my=zero:5080 89 server: 90 image: dgraph/dgraph:latest 91 volumes: 92 - type: volume 93 source: dgraph 94 target: /dgraph 95 volume: 96 nocopy: true 97 ports: 98 - 8080:8080 99 - 9080:9080 100 restart: on-failure 101 command: dgraph alpha --my=server:7080 --lru_mb=2048 --zero=zero:5080 102 ratel: 103 image: dgraph/dgraph:latest 104 volumes: 105 - type: volume 106 source: dgraph 107 target: /dgraph 108 volume: 109 nocopy: true 110 ports: 111 - 8000:8000 112 command: dgraph-ratel 113 114 volumes: 115 dgraph: 116 ``` 117 118 Save the contents of the snippet above in a file called `docker-compose.yml`, then run the following 119 command from the folder containing the file. 120 ``` 121 docker-compose up -d 122 ``` 123 124 This would start Dgraph Alpha, Zero and Ratel. You can check the logs using `docker-compose logs` 125 126 ### From Installed Binary 127 128 **Run Dgraph zero** 129 130 Run `dgraph zero` to start Dgraph zero. This process controls Dgraph cluster, 131 maintaining membership information, shard assignment and shard movement, etc. 132 133 ```sh 134 dgraph zero 135 ``` 136 137 **Run Dgraph data server** 138 139 Run `dgraph alpha` to start Dgraph alpha. 140 141 ```sh 142 dgraph alpha --lru_mb 2048 --zero localhost:5080 143 ``` 144 145 **Run Ratel** 146 147 Run 'dgraph-ratel' to start Dgraph UI. This can be used to do mutations and query through UI. 148 149 ```sh 150 dgraph-ratel 151 ``` 152 153 {{% notice "tip" %}}You need to set the estimated memory Dgraph alpha can take through `lru_mb` flag. This is just a hint to the Dgraph alpha and actual usage would be higher than this. It's recommended to set lru_mb to one-third the available RAM.{{% /notice %}} 154 155 ### Docker on Linux 156 157 ```sh 158 # Directory to store data in. This would be passed to `-v` flag. 159 mkdir -p /tmp/data 160 161 # Run Dgraph Zero 162 docker run -it -p 5080:5080 -p 6080:6080 -p 8080:8080 -p 9080:9080 -p 8000:8000 -v /tmp/data:/dgraph --name diggy dgraph/dgraph dgraph zero 163 164 # Run Dgraph Alpha 165 docker exec -it diggy dgraph alpha --lru_mb 2048 --zero localhost:5080 166 167 # Run Dgraph Ratel 168 docker exec -it diggy dgraph-ratel 169 ``` 170 171 The dgraph alpha listens on ports 8080 and 9080 with log output to the terminal. 172 173 ### Docker on Non Linux Distributions. 174 File access in mounted filesystems is slower when using docker. Try running the command `time dd if=/dev/zero of=test.dat bs=1024 count=100000` on mounted volume and you will notice that it's horribly slow when using mounted volumes. We recommend users to use docker data volumes. The only downside of using data volumes is that you can't access the files from the host, you have to launch a container for accessing it. 175 176 {{% notice "tip" %}}If you are using docker on non-linux distribution, please use docker data volumes.{{% /notice %}} 177 178 Create a docker data container named *data* with dgraph/dgraph image. 179 ```sh 180 docker create -v /dgraph --name data dgraph/dgraph 181 ``` 182 183 Now if we run Dgraph container with `--volumes-from` flag and run Dgraph with the following command, then anything we write to /dgraph in Dgraph container will get written to /dgraph volume of datacontainer. 184 ```sh 185 docker run -it -p 5080:5080 -p 6080:6080 --volumes-from data --name diggy dgraph/dgraph dgraph zero 186 docker exec -it diggy dgraph alpha --lru_mb 2048 --zero localhost:5080 187 188 # Run Dgraph Ratel 189 docker exec -it diggy dgraph-ratel 190 ``` 191 192 {{% notice "tip" %}} 193 If you are using Dgraph v1.0.2 (or older) then the default ports are 7080, 8080 for zero, so when following instructions for different setup guides override zero port using `--port_offset`. 194 195 ```sh 196 dgraph zero --lru_mb=<typically one-third the RAM> --port_offset -2000 197 ``` 198 Ratel's default port is 8081, so override it using -p 8000. 199 200 {{% /notice %}} 201 202 203 ## Step 3: Run Queries 204 {{% notice "tip" %}}Once Dgraph is running, you can access Ratel at [`http://localhost:8000`](http://localhost:8000). It allows browser-based queries, mutations and visualizations. 205 206 The mutations and queries below can either be run from the command line using `curl -H "Content-Type: application/rdf" localhost:8080/query -XPOST -d $'...'` or by pasting everything between the two `'` into the running user interface on localhost.{{% /notice %}} 207 208 ### Dataset 209 The dataset is a movie graph, where and the graph nodes are entities of the type directors, actors, genres, or movies. 210 211 ### Storing data in the graph 212 Changing the data stored in Dgraph is a mutation. The following mutation stores information about the first three releases of the the ''Star Wars'' series and one of the ''Star Trek'' movies. Running this mutation, either through the UI or on the command line, will store the data in Dgraph. 213 214 215 ```sh 216 curl -H "Content-Type: application/rdf" localhost:8080/mutate?commitNow=true -XPOST -d $' 217 { 218 set { 219 _:luke <name> "Luke Skywalker" . 220 _:luke <dgraph.type> "Person" . 221 _:leia <name> "Princess Leia" . 222 _:leia <dgraph.type> "Person" . 223 _:han <name> "Han Solo" . 224 _:han <dgraph.type> "Person" . 225 _:lucas <name> "George Lucas" . 226 _:lucas <dgraph.type> "Person" . 227 _:irvin <name> "Irvin Kernshner" . 228 _:irvin <dgraph.type> "Person" . 229 _:richard <name> "Richard Marquand" . 230 _:richard <dgraph.type> "Person" . 231 232 _:sw1 <name> "Star Wars: Episode IV - A New Hope" . 233 _:sw1 <release_date> "1977-05-25" . 234 _:sw1 <revenue> "775000000" . 235 _:sw1 <running_time> "121" . 236 _:sw1 <starring> _:luke . 237 _:sw1 <starring> _:leia . 238 _:sw1 <starring> _:han . 239 _:sw1 <director> _:lucas . 240 _:sw1 <dgraph.type> "Film" . 241 242 _:sw2 <name> "Star Wars: Episode V - The Empire Strikes Back" . 243 _:sw2 <release_date> "1980-05-21" . 244 _:sw2 <revenue> "534000000" . 245 _:sw2 <running_time> "124" . 246 _:sw2 <starring> _:luke . 247 _:sw2 <starring> _:leia . 248 _:sw2 <starring> _:han . 249 _:sw2 <director> _:irvin . 250 _:sw2 <dgraph.type> "Film" . 251 252 _:sw3 <name> "Star Wars: Episode VI - Return of the Jedi" . 253 _:sw3 <release_date> "1983-05-25" . 254 _:sw3 <revenue> "572000000" . 255 _:sw3 <running_time> "131" . 256 _:sw3 <starring> _:luke . 257 _:sw3 <starring> _:leia . 258 _:sw3 <starring> _:han . 259 _:sw3 <director> _:richard . 260 _:sw3 <dgraph.type> "Film" . 261 262 _:st1 <name> "Star Trek: The Motion Picture" . 263 _:st1 <release_date> "1979-12-07" . 264 _:st1 <revenue> "139000000" . 265 _:st1 <running_time> "132" . 266 _:st1 <dgraph.type> "Film" . 267 } 268 } 269 ' | python -m json.tool | less 270 ``` 271 272 {{% notice "tip" %}} 273 To run an RDF mutation via curl, you can use the curl option `--data-binary @/path/to/mutation.txt` instead of `-d $''`. The `--data-binary` option skips curl's default URL-encoding. 274 {{% /notice %}} 275 276 ### Adding indexes 277 Alter the schema to add indexes on some of the data so queries can use term matching, filtering and sorting. 278 279 ```sh 280 curl localhost:8080/alter -XPOST -d $' 281 name: string @index(term) . 282 release_date: datetime @index(year) . 283 revenue: float . 284 running_time: int . 285 286 type Person { 287 name 288 } 289 290 type Film { 291 name 292 release_date 293 revenue 294 running_time 295 starring 296 director 297 } 298 299 ' | python -m json.tool | less 300 ``` 301 302 ### Get all movies 303 Run this query to get all the movies. The query works below all the movies have a starring edge 304 305 ```sh 306 curl -H "Content-Type: application/graphql+-" localhost:8080/query -XPOST -d $' 307 { 308 me(func: has(starring)) { 309 name 310 } 311 } 312 ' | python -m json.tool | less 313 ``` 314 315 ### Get all movies released after "1980" 316 Run this query to get "Star Wars" movies released after "1980". Try it in the user interface to see the result as a graph. 317 318 319 ```sh 320 curl -H "Content-Type: application/graphql+-" localhost:8080/query -XPOST -d $' 321 { 322 me(func:allofterms(name, "Star Wars")) @filter(ge(release_date, "1980")) { 323 name 324 release_date 325 revenue 326 running_time 327 director { 328 name 329 } 330 starring { 331 name 332 } 333 } 334 } 335 ' | python -m json.tool | less 336 ``` 337 338 Output 339 340 ```json 341 { 342 "data":{ 343 "me":[ 344 { 345 "name":"Star Wars: Episode V - The Empire Strikes Back", 346 "release_date":"1980-05-21T00:00:00Z", 347 "revenue":534000000.0, 348 "running_time":124, 349 "director":[ 350 { 351 "name":"Irvin Kernshner" 352 } 353 ], 354 "starring":[ 355 { 356 "name":"Han Solo" 357 }, 358 { 359 "name":"Luke Skywalker" 360 }, 361 { 362 "name":"Princess Leia" 363 } 364 ] 365 }, 366 { 367 "name":"Star Wars: Episode VI - Return of the Jedi", 368 "release_date":"1983-05-25T00:00:00Z", 369 "revenue":572000000.0, 370 "running_time":131, 371 "director":[ 372 { 373 "name":"Richard Marquand" 374 } 375 ], 376 "starring":[ 377 { 378 "name":"Han Solo" 379 }, 380 { 381 "name":"Luke Skywalker" 382 }, 383 { 384 "name":"Princess Leia" 385 } 386 ] 387 } 388 ] 389 } 390 } 391 ``` 392 393 That's it! In these three steps, we set up Dgraph, added some data, set a schema 394 and queried that data back. 395 396 ## Where to go from here 397 398 - Go to [Clients]({{< relref "clients/index.md" >}}) to see how to communicate 399 with Dgraph from your application. 400 - Take the [Tour](https://tour.dgraph.io) for a guided tour of how to write queries in Dgraph. 401 - A wider range of queries can also be found in the [Query Language]({{< relref "query-language/index.md" >}}) reference. 402 - See [Deploy]({{< relref "deploy/index.md" >}}) if you wish to run Dgraph 403 in a cluster. 404 405 ## Need Help 406 407 * Please use [discuss.dgraph.io](https://discuss.dgraph.io) for questions, feature requests and discussions. 408 * Please use [Github Issues](https://github.com/dgraph-io/dgraph/issues) if you encounter bugs or have feature requests. 409 * You can also join our [Slack channel](http://slack.dgraph.io). 410 411 ## Troubleshooting 412 413 ### 1. Docker: Error response from daemon; Conflict. Container name already exists. 414 415 Remove the diggy container and try the docker run command again. 416 ``` 417 docker rm diggy 418 ```