Skip to content
Snippets Groups Projects
index.md 11.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • ## Dgraph
    
    Dgraph cluster consists of different nodes (zero, server & ratel) and each node serves a different purpose.
    
    **Dgraph Zero** controls the Dgraph cluster, assigns servers to a group and re-balances data between server groups.
    
    **Dgraph Server** hosts predicates and indexes.
    
    **Dgraph Ratel** serves the UI to run queries, mutations & altering schema.
    
    You need atleast need one Dgraph zero and one Dgraph Server to get started.
    
    **Here's a 3 step tutorial to get you up and running.**
    
    This is a quick-start guide to running Dgraph. For an interactive walk through, take the [tour](https://tour.dgraph.io).
    
    You can see the accompanying [video here](https://www.youtube.com/watch?v=QIIdSp2zLcs).
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    ## Step 1: Install Dgraph
    
    
    Manish R Jain's avatar
    Manish R Jain committed
    Dgraph can be installed from the install scripts, or run via Docker.
    
    {{% notice "note" %}}These instructions will install the latest release version.  To instead install our nightly build see [these instructions](/deploy).{{% /notice %}}
    
    ### From Docker Image
    
    Pull the Dgraph Docker images [from here](https://hub.docker.com/r/dgraph/dgraph/). From a terminal:
    
    ```sh
    docker pull dgraph/dgraph
    ```
    
    
    ### From Install Scripts (Linux/Mac)
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    Install the binaries with
    
    Pawan Rawal's avatar
    Pawan Rawal committed
    curl https://get.dgraph.io -sSf | bash
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    The script automatically installs Dgraph. Once done, jump straight to [step 2]({{< relref "#step-2-run-dgraph" >}}).
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    **Alternative:** To mitigate potential security risks, instead try:
    
    Pawan Rawal's avatar
    Pawan Rawal committed
    curl https://get.dgraph.io > /tmp/get.sh
    vim /tmp/get.sh  # Inspect the script
    sh /tmp/get.sh   # Execute the script
    
    Manish R Jain's avatar
    Manish R Jain committed
    You can check that Dgraph binary installed correctly by running `dgraph` and
    looking at its output, which includes the version number.
    
    
    ### Installing on Windows
    
    {{% notice "note" %}}Binaries for Windows are available from `v0.8.3`.{{% /notice %}}
    
    
    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.
    
    ## Step 2: Run Dgraph
    
    {{% notice "note" %}} This is a set up involving just one machine. For multi-server setup, go to [Deploy](/deploy). {{% /notice %}}
    
    ### Docker Compose
    
    The easiest way to get Dgraph up and running is using Docker Compose. Follow the instructions
    [here](https://docs.docker.com/compose/install/) to install Docker Compose if you don't have it
    already.
    
    ```
    
    version: "3.2"
    
    services:
      zero:
        image: dgraph/dgraph:latest
        volumes:
    
          - type: volume
            source: dgraph
            target: /dgraph
            volume:
              nocopy: true
    
          - 6080:6080
        restart: on-failure
    
        command: dgraph zero --my=zero:5080
    
      server:
        image: dgraph/dgraph:latest
        volumes:
    
          - type: volume
            source: dgraph
            target: /dgraph
            volume:
              nocopy: true
    
        ports:
          - 8080:8080
          - 9080:9080
        restart: on-failure
    
        command: dgraph server --my=server:7080 --lru_mb=2048 --zero=zero:5080
    
      ratel:
        image: dgraph/dgraph:latest
        volumes:
    
          - type: volume
            source: dgraph
            target: /dgraph
            volume:
              nocopy: true
    
        command: dgraph-ratel
    
    
    volumes:
      dgraph:
    
    ```
    
    Save the contents of the snippet above in a file called `docker-compose.yml`, then run the following
    command from the folder containing the file.
    ```
    docker-compose up -d
    ```
    
    This would start Dgraph Server, Zero and Ratel. You can check the logs using `docker-compose logs`
    
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    ### From Installed Binary
    
    Manish R Jain's avatar
    Manish R Jain committed
    
    **Run Dgraph zero**
    
    Run `dgraph zero` to start Dgraph zero. This process controls Dgraph cluster,
    maintaining membership information, shard assignment and shard movement, etc.
    
    Manish R Jain's avatar
    Manish R Jain committed
    **Run Dgraph data server**
    
    
    Run `dgraph server` to start Dgraph server.
    
    dgraph server --lru_mb 2048 --zero localhost:5080
    
    Janardhan Reddy's avatar
    Janardhan Reddy committed
    **Run Ratel**
    
    Janardhan Reddy's avatar
    Janardhan Reddy committed
    Run 'dgraph-ratel' to start Dgraph UI. This can be used to do mutations and query through UI.
    
    Janardhan Reddy's avatar
    Janardhan Reddy committed
    dgraph-ratel
    
    {{% notice "tip" %}}You need to set the estimated memory Dgraph server can take through `lru_mb` flag. This is just a hint to the Dgraph server and actual usage would be higher than this. It's recommended to set lru_mb to one-third the available RAM.{{% /notice %}}
    
    Manish R Jain's avatar
    Manish R Jain committed
    ### Docker on Linux
    
    Manish R Jain's avatar
    Manish R Jain committed
    # Directory to store data in. This would be passed to `-v` flag.
    mkdir -p /tmp/data
    
    Manish R Jain's avatar
    Manish R Jain committed
    # Run Dgraph Zero
    
    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
    
    Manish R Jain's avatar
    Manish R Jain committed
    # Run Dgraph Server
    
    docker exec -it diggy dgraph server --lru_mb 2048 --zero localhost:5080
    
    Janardhan Reddy's avatar
    Janardhan Reddy committed
    docker exec -it diggy dgraph-ratel
    
    Manish R Jain's avatar
    Manish R Jain committed
    The dgraph server listens on ports 8080 and 9080  with log output to the terminal.
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    
    
    Manish R Jain's avatar
    Manish R Jain committed
    ### Docker on Non Linux Distributions.
    
    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.
    
    Janardhan Reddy's avatar
    Janardhan Reddy committed
    
    
    Manish R Jain's avatar
    Manish R Jain committed
    {{% notice "tip" %}}If you are using docker on non-linux distribution, please use docker data volumes.{{% /notice %}}
    
    
    Create a docker data container named *data* with dgraph/dgraph image.
    
    ```sh
    
    Manish R Jain's avatar
    Manish R Jain committed
    docker create -v /dgraph --name data dgraph/dgraph
    
    Janardhan Reddy's avatar
    Janardhan Reddy committed
    
    
    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.
    
    ```sh
    
    docker run -it -p 5080:5080 -p 6080:6080 --volumes-from data --name diggy dgraph/dgraph dgraph zero
    
    docker exec -it diggy dgraph server --lru_mb 2048 --zero localhost:5080
    
    
    # Run Dgraph Ratel
    docker exec -it diggy dgraph-ratel
    
    Janardhan Reddy's avatar
    Janardhan Reddy committed
    
    
    {{% notice "tip" %}}
    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`.
    
    ```sh
    
    dgraph zero --lru_mb=<typically one-third the RAM> --port_offset -2000
    
    ```
    Ratel's default port is 8081, so override it using -p 8000.
    
    {{% /notice %}}
    
    
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    ## Step 3: Run Queries
    
    {{% 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.
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    
    The mutations and queries below can either be run from the command line using `curl localhost:8080/query -XPOST -d $'...'` or by pasting everything between the two `'` into the running user interface on localhost.{{% /notice %}}
    
    Graphpinto's avatar
    Graphpinto committed
    ### Dataset
    
    The dataset is a movie graph, where and the graph nodes are entities of the type directors, actors, genres, or movies.
    
    ### Storing data in the graph
    
    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.
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    
    
    Manish R Jain's avatar
    Manish R Jain committed
    curl localhost:8080/mutate -H "X-Dgraph-CommitNow: true" -XPOST -d $'
    {
    
      set {
       _:luke <name> "Luke Skywalker" .
       _:leia <name> "Princess Leia" .
       _:han <name> "Han Solo" .
       _:lucas <name> "George Lucas" .
       _:irvin <name> "Irvin Kernshner" .
       _:richard <name> "Richard Marquand" .
    
       _:sw1 <name> "Star Wars: Episode IV - A New Hope" .
       _:sw1 <release_date> "1977-05-25" .
       _:sw1 <revenue> "775000000" .
       _:sw1 <running_time> "121" .
       _:sw1 <starring> _:luke .
       _:sw1 <starring> _:leia .
       _:sw1 <starring> _:han .
       _:sw1 <director> _:lucas .
    
       _:sw2 <name> "Star Wars: Episode V - The Empire Strikes Back" .
       _:sw2 <release_date> "1980-05-21" .
       _:sw2 <revenue> "534000000" .
       _:sw2 <running_time> "124" .
       _:sw2 <starring> _:luke .
       _:sw2 <starring> _:leia .
       _:sw2 <starring> _:han .
       _:sw2 <director> _:irvin .
    
       _:sw3 <name> "Star Wars: Episode VI - Return of the Jedi" .
       _:sw3 <release_date> "1983-05-25" .
       _:sw3 <revenue> "572000000" .
       _:sw3 <running_time> "131" .
       _:sw3 <starring> _:luke .
       _:sw3 <starring> _:leia .
       _:sw3 <starring> _:han .
       _:sw3 <director> _:richard .
    
       _:st1 <name> "Star Trek: The Motion Picture" .
       _:st1 <release_date> "1979-12-07" .
       _:st1 <revenue> "139000000" .
       _:st1 <running_time> "132" .
      }
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    }
    ' | python -m json.tool | less
    
    Graphpinto's avatar
    Graphpinto committed
    ### Adding indexes
    
    Alter the schema to add indexes on some of the data so queries can use term matching, filtering and sorting.
    
    Manish R Jain's avatar
    Manish R Jain committed
    curl localhost:8080/alter -XPOST -d $'
      name: string @index(term) .
      release_date: datetime @index(year) .
      revenue: float .
      running_time: int .
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    ' | python -m json.tool | less
    
    Graphpinto's avatar
    Graphpinto committed
    Run this query to get all the movies. The query works below all the movies have a starring edge
    
    ```sh
    curl localhost:8080/query -XPOST -d $'
    {
     me(func: has(starring)) {
       name@en
      }
    }
    ' | python -m json.tool | less
    ```
    
    ### Get all movies released after "1980"
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    Run this query to get "Star Wars" movies released after "1980".  Try it in the user interface to see the result as a graph.
    
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    curl localhost:8080/query -XPOST -d $'
    {
    
    Janardhan Reddy's avatar
    Janardhan Reddy committed
      me(func:allofterms(name, "Star Wars")) @filter(ge(release_date, "1980")) {
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    }
    ' | python -m json.tool | less
    
    Pawan Rawal's avatar
    Pawan Rawal committed
    Output
    
    
      "data":{
        "me":[
          {
            "name":"Star Wars: Episode V - The Empire Strikes Back",
            "release_date":"1980-05-21T00:00:00Z",
            "revenue":534000000.0,
            "running_time":124,
            "director":[
              {
                "name":"Irvin Kernshner"
              }
            ],
            "starring":[
              {
                "name":"Han Solo"
              },
              {
                "name":"Luke Skywalker"
              },
              {
                "name":"Princess Leia"
              }
            ]
          },
          {
            "name":"Star Wars: Episode VI - Return of the Jedi",
            "release_date":"1983-05-25T00:00:00Z",
            "revenue":572000000.0,
            "running_time":131,
            "director":[
              {
                "name":"Richard Marquand"
              }
            ],
            "starring":[
              {
                "name":"Han Solo"
              },
              {
                "name":"Luke Skywalker"
              },
              {
                "name":"Princess Leia"
              }
            ]
          }
    
    Manish R Jain's avatar
    Manish R Jain committed
    That's it! In these three steps, we set up Dgraph, added some data, set a schema
    and queried that data back.
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    
    
    Manish R Jain's avatar
    Manish R Jain committed
    ## Where to go from here
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    
    
    Manish R Jain's avatar
    Manish R Jain committed
    - Go to [Clients]({{< relref "clients/index.md" >}}) to see how to communicate
    with Dgraph from your application.
    
    Manish R Jain's avatar
    Manish R Jain committed
    - Take the [Tour](https://tour.dgraph.io) for a guided tour of how to write queries in Dgraph.
    
    - A wider range of queries can also be found in the [Query Language](/query-language) reference.
    - See [Deploy](/deploy) if you wish to run Dgraph
    
    Manish R Jain's avatar
    Manish R Jain committed
      in a cluster.
    
    MichaelJCompton's avatar
    MichaelJCompton committed
    
    
    * Please use [discuss.dgraph.io](https://discuss.dgraph.io) for questions, feature requests and discussions.
    * Please use [Github Issues](https://github.com/dgraph-io/dgraph/issues) if you encounter bugs or have feature requests.
    * You can also join our [Slack channel](http://slack.dgraph.io).
    
    ## Troubleshooting
    
    
    ### 1. Docker: Error response from daemon; Conflict. Container name already exists.
    
    Remove the diggy container and try the docker run command again.