github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/wiki/content/get-started/index.md (about) 1 +++ 2 title = "Get Started" 3 +++ 4 5 ## Dgraph 6 7 Dgraph cluster consists of different nodes (Zero, Alpha & Ratel) and each node serves a different purpose. 8 9 **Dgraph Zero** controls the Dgraph cluster, assigns servers to a group and re-balances data between server groups. 10 11 **Dgraph Alpha** hosts predicates and indexes. 12 13 **Dgraph Ratel** serves the UI to run queries, mutations & altering schema. 14 15 You need at least one Dgraph Zero and one Dgraph Alpha to get started. 16 17 **Here's a 3 step tutorial to get you up and running.** 18 19 This is a quick-start guide to running Dgraph. For an interactive walk through, take the [tour](https://tour.dgraph.io). 20 21 You can see the accompanying [video here](https://www.youtube.com/watch?v=QIIdSp2zLcs). 22 23 ## Step 1: Install Dgraph 24 25 Dgraph can be installed from the install scripts, or run via Docker. 26 27 ### From Docker Image 28 29 Pull the Dgraph Docker images [from here](https://hub.docker.com/r/dgraph/dgraph/). From a terminal: 30 31 ```sh 32 docker pull dgraph/dgraph 33 ``` 34 35 ### From Install Scripts (Linux/Mac) 36 37 Install the binaries with 38 39 ```sh 40 curl https://get.dgraph.io -sSf | bash 41 ``` 42 43 The script automatically installs Dgraph. Once done, jump straight to [step 2]({{< relref "#step-2-run-dgraph" >}}). 44 45 **Alternative:** To mitigate potential security risks, instead try: 46 47 ```sh 48 curl https://get.dgraph.io > /tmp/get.sh 49 vim /tmp/get.sh # Inspect the script 50 sh /tmp/get.sh # Execute the script 51 ``` 52 53 You can check that Dgraph binary installed correctly by running `dgraph` and 54 looking at its output, which includes the version number. 55 56 ### Installing on Windows 57 58 {{% notice "note" %}}Binaries for Windows are available from `v0.8.3`.{{% /notice %}} 59 60 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. 61 62 ## Step 2: Run Dgraph 63 {{% notice "note" %}} This is a set up involving just one machine. For multi-server setup, go to [Deploy](/deploy). {{% /notice %}} 64 65 ### Docker Compose 66 67 The easiest way to get Dgraph up and running is using Docker Compose. Follow the instructions 68 [here](https://docs.docker.com/compose/install/) to install Docker Compose if you don't have it 69 already. 70 71 ``` 72 version: "3.2" 73 services: 74 zero: 75 image: dgraph/dgraph:latest 76 volumes: 77 - type: volume 78 source: dgraph 79 target: /dgraph 80 volume: 81 nocopy: true 82 ports: 83 - 5080:5080 84 - 6080:6080 85 restart: on-failure 86 command: dgraph zero --my=zero:5080 87 server: 88 image: dgraph/dgraph:latest 89 volumes: 90 - type: volume 91 source: dgraph 92 target: /dgraph 93 volume: 94 nocopy: true 95 ports: 96 - 8080:8080 97 - 9080:9080 98 restart: on-failure 99 command: dgraph alpha --my=server:7080 --lru_mb=2048 --zero=zero:5080 100 ratel: 101 image: dgraph/dgraph:latest 102 volumes: 103 - type: volume 104 source: dgraph 105 target: /dgraph 106 volume: 107 nocopy: true 108 ports: 109 - 8000:8000 110 command: dgraph-ratel 111 112 volumes: 113 dgraph: 114 ``` 115 116 Save the contents of the snippet above in a file called `docker-compose.yml`, then run the following 117 command from the folder containing the file. 118 ``` 119 docker-compose up -d 120 ``` 121 122 This would start Dgraph Alpha, Zero and Ratel. You can check the logs using `docker-compose logs` 123 124 ### From Installed Binary 125 126 **Run Dgraph zero** 127 128 Run `dgraph zero` to start Dgraph zero. This process controls Dgraph cluster, 129 maintaining membership information, shard assignment and shard movement, etc. 130 131 ```sh 132 dgraph zero 133 ``` 134 135 **Run Dgraph data server** 136 137 Run `dgraph alpha` to start Dgraph alpha. 138 139 ```sh 140 dgraph alpha --lru_mb 2048 --zero localhost:5080 141 ``` 142 143 **Run Ratel** 144 145 Run 'dgraph-ratel' to start Dgraph UI. This can be used to do mutations and query through UI. 146 147 ```sh 148 dgraph-ratel 149 ``` 150 151 {{% 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 %}} 152 153 ### Docker on Linux 154 155 ```sh 156 # Directory to store data in. This would be passed to `-v` flag. 157 mkdir -p /tmp/data 158 159 # Run Dgraph Zero 160 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 161 162 # Run Dgraph Alpha 163 docker exec -it diggy dgraph alpha --lru_mb 2048 --zero localhost:5080 164 165 # Run Dgraph Ratel 166 docker exec -it diggy dgraph-ratel 167 ``` 168 169 The dgraph alpha listens on ports 8080 and 9080 with log output to the terminal. 170 171 ### Docker on Non Linux Distributions. 172 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. 173 174 {{% notice "tip" %}}If you are using docker on non-linux distribution, please use docker data volumes.{{% /notice %}} 175 176 Create a docker data container named *data* with dgraph/dgraph image. 177 ```sh 178 docker create -v /dgraph --name data dgraph/dgraph 179 ``` 180 181 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. 182 ```sh 183 docker run -it -p 5080:5080 -p 6080:6080 --volumes-from data --name diggy dgraph/dgraph dgraph zero 184 docker exec -it diggy dgraph alpha --lru_mb 2048 --zero localhost:5080 185 186 # Run Dgraph Ratel 187 docker exec -it diggy dgraph-ratel 188 ``` 189 190 {{% notice "tip" %}} 191 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`. 192 193 ```sh 194 dgraph zero --lru_mb=<typically one-third the RAM> --port_offset -2000 195 ``` 196 Ratel's default port is 8081, so override it using -p 8000. 197 198 {{% /notice %}} 199 200 201 ## Step 3: Run Queries 202 {{% 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. 203 204 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 %}} 205 206 ### Dataset 207 The dataset is a movie graph, where and the graph nodes are entities of the type directors, actors, genres, or movies. 208 209 ### Storing data in the graph 210 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. 211 212 213 ```sh 214 curl -H "Content-Type: application/rdf" localhost:8080/mutate?commitNow=true -XPOST -d $' 215 { 216 set { 217 _:luke <name> "Luke Skywalker" . 218 _:luke <dgraph.type> "Person" . 219 _:leia <name> "Princess Leia" . 220 _:leia <dgraph.type> "Person" . 221 _:han <name> "Han Solo" . 222 _:han <dgraph.type> "Person" . 223 _:lucas <name> "George Lucas" . 224 _:lucas <dgraph.type> "Person" . 225 _:irvin <name> "Irvin Kernshner" . 226 _:irvin <dgraph.type> "Person" . 227 _:richard <name> "Richard Marquand" . 228 _:richard <dgraph.type> "Person" . 229 230 _:sw1 <name> "Star Wars: Episode IV - A New Hope" . 231 _:sw1 <release_date> "1977-05-25" . 232 _:sw1 <revenue> "775000000" . 233 _:sw1 <running_time> "121" . 234 _:sw1 <starring> _:luke . 235 _:sw1 <starring> _:leia . 236 _:sw1 <starring> _:han . 237 _:sw1 <director> _:lucas . 238 _:sw1 <dgraph.type> "Film" . 239 240 _:sw2 <name> "Star Wars: Episode V - The Empire Strikes Back" . 241 _:sw2 <release_date> "1980-05-21" . 242 _:sw2 <revenue> "534000000" . 243 _:sw2 <running_time> "124" . 244 _:sw2 <starring> _:luke . 245 _:sw2 <starring> _:leia . 246 _:sw2 <starring> _:han . 247 _:sw2 <director> _:irvin . 248 _:sw2 <dgraph.type> "Film" . 249 250 _:sw3 <name> "Star Wars: Episode VI - Return of the Jedi" . 251 _:sw3 <release_date> "1983-05-25" . 252 _:sw3 <revenue> "572000000" . 253 _:sw3 <running_time> "131" . 254 _:sw3 <starring> _:luke . 255 _:sw3 <starring> _:leia . 256 _:sw3 <starring> _:han . 257 _:sw3 <director> _:richard . 258 _:sw3 <dgraph.type> "Film" . 259 260 _:st1 <name> "Star Trek: The Motion Picture" . 261 _:st1 <release_date> "1979-12-07" . 262 _:st1 <revenue> "139000000" . 263 _:st1 <running_time> "132" . 264 _:st1 <dgraph.type> "Film" . 265 } 266 } 267 ' | python -m json.tool | less 268 ``` 269 270 {{% notice "tip" %}} 271 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. 272 {{% /notice %}} 273 274 ### Adding indexes 275 Alter the schema to add indexes on some of the data so queries can use term matching, filtering and sorting. 276 277 ```sh 278 curl localhost:8080/alter -XPOST -d $' 279 name: string @index(term) . 280 release_date: datetime @index(year) . 281 revenue: float . 282 running_time: int . 283 starring: [uid] . 284 director: [uid] . 285 286 type Person { 287 name: string 288 } 289 290 type Film { 291 name: string 292 release_date: datetime 293 revenue: float 294 running_time: int 295 starring: [uid] 296 director: [uid] 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](/query-language) reference. 402 - See [Deploy](/deploy) 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 410 ## Troubleshooting 411 412 ### 1. Docker: Error response from daemon; Conflict. Container name already exists. 413 414 Remove the diggy container and try the docker run command again. 415 ``` 416 docker rm diggy 417 ```