Dgraph - Part 1: installation & first steps

I was looking for a database system for an internal project in our company. For this reason, I looked at various systems. In addition to old acquaintances from the SQL or NoSQL environments, I came across something very interesting. Graph databases. Without wanting to go into great detail, here are the most interesting aspects for me and the reason I decided to choose a graph database for my project:

  • no fixed database schema required
  • data is represented in the form of nodes and connections
  • strong focus on relationships between data sets
  • precise retrieval of data even across relationships
  • gRPC server built in
  • GraphQL server built in

Start Your Dgraph Server

The easiest way to start Dgraph is with Docker. Since we want persistent data, we create a folder dgraph which we make available to our container as a volume.

1
mkdir ~/dgraph
1
docker run -p 8080:8080 -p 9080:9080 -p 8000:8000 -it -v ~/dgraph:/dgraph dgraph/standalone:latest

This starts dgraph. If the container does not yet exist, the image is downloaded. Currentlythe version is 2.0. An overview of the versions available on Docker Hub can bou found at https://hub.docker.com/r/dgraph/dgraph/tags What do we get here?

  • an HTTP service on port 8080 with the endpoints /admin and /graphql
  • an HTTP service on port 8000 with the web interface for Dgraph called Ratel
  • a gRPC service on port 9080

Save and retrieve the first records

Now you can already interact with the database in one of the 3 ways mentioned above. We can login at http://localhost:8000/ and use Ratel to start storing the first data using the menu item Console. To do this, we first click on Mutation. Our first mutation looks like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
  set{
    _:Peter <name> "Peter Parker" .
    _:Peter <hometown> "New York" .
    _:Mary <name> "Mary Jane Watson" .
    _:Mary <hometown> "New York" .
    _:Mary <friend_of> _:Peter .
    _:Harry <name> "Harry Osborne" .
    _:Harry <hometown> "New York" .
    _:Harry <friend_of> _:Peter .
    _:Harry <friend_of> _:Mary .
  }
}

We use this to create several people with the fields name and hometown. The friend_of field is a connection to another data record. If you are wondering about the format, this is the RDF N-Quads format. Don’t worry, we’ll get to that and the format is relatively simple. Now we can query the data. To do this, we select the query in the console and enter the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
  all(func: has(name)){
    uid
    name
    hometown
    friend_of{
      uid
      name
      hometown
    }
  }
}

We create a query all using the has function. We use it to search for all data records that have the field name. Then we indicate that we want to have the fields ui, name, hometown and friend_of. Since friend_of is a connection to another data record, we indicate here which fields we would like to have.

We have just seen how to start Dgraph and take the first steps. So far we have only worked with Ratel. The GraphQL endpoint is started, but complains that we don’t have a schema. We also got to know RDF N-Quads. We definitely have to pick up here 😊

0%