{{announcement.body}}
{{announcement.title}}

Exploring DGraph: Getting Started

DZone 's Guide to

Exploring DGraph: Getting Started

Want to get started with DGraph? Read on to set it up in your local machine and get your hands dirty with its functionalities.

· Database Zone ·
Free Resource

This article is primarily aimed towards getting DGraph up and running on your local machine, create some schema in DGraph (alter), add/delete data from DGraph (mutation) and get the data from it (query). We are going to keep the article simple, exploring each of the mentioned operations in detail in later blogs.

Installing and Running DGraph on Your Local Machine

There are multiple ways to install and run DGraph on your local machine, but the simplest one according to me is just downloading their docker-compose.yml file from their docs itself and running it with docker-compose. Or, you can copy and paste the below content in a file and name it as docker-compose.yml:

After getting the docker-compose.yml file, run the following command from the folder where you saved the file:

sudo docker-compose up -d

This will run all the components that DGraph uses for running. We will be going through each of the components in detail in later posts. For now, we're just happy to get DGraph up and running. Yay! You will be able to access the UI provided by DGraph (known as ratel) accessible at:

http://localhost:8000

Now, time to get our hands dirty.

Creating a Schema in DGraph

To create or change schema in DGraph, you need to alter the schema in DGraph. While you're on the ratel, you will be able to see three options at the bottom of the provided editor, and one of them will be Alter, as shown in the below diagram.

For creating the schema, we will click on the Alter option. We could have also gone on the Schema option on the left-hand side, as clearly visible in the above picture, but that provides Alter operations from UI without any code. So we could have gone there as well, but since this post is being written from a learning perspective, we are going to go with the Alter option and actually write something up.

So, now we must be on the Alter option and there must be an editor waiting for us to write something in it. Let's create a schema for employees working in a company. Since schema in itself is a pretty big topic, we are going to dive deep into it in later articles. For now, we are just going to create one simple schema as shown below:

employeeName: string @index(exact) .
employeeId: int .
worksFor: uid .
companyName: string @index(exact) .

The above schema consists of four predicates:

  • employeeName — Name of an employee. Its data type has been set to string since any employee's name would obviously be a string. It has been provided an index as exact. You don't need to get into the details of what it means right now. For now, you can remember that an index is required for a predicate to make it queryable and filterable.
  • employeeId — ID of an employee. ID of an employee, in our case, would always be a number. Hence, the data type of int.
  • worksFor — worksFor is an edge that goes from an employee node to a company node. Since worksFor points towards a node, it's data type is uid, the unique identifier of a node.
  • companyName — Name of the company. There will be a node dedicated to the company, containing only the companyName predicate.

Now we will hit Run on the top right of our editor window. After hitting that, we should be able to see Done in the response window. That means that the schema has been created. You can see the created schema by going to the Schema tab (in the left-hand corner). You will be able to see our 4 created predicates. Awesome! Now let's create some data.

Creating Data in DGraph

To create, update, or delete data in DGraph, mutate the data in DGraph. We will mutate some sample data in our DGraph using the schema that we created in the above topic. For mutating data, we will go into the Mutate option, which is one of the options at the bottom of the editor window. Now that we have selected the Mutate option, we will write down what data we want to mutate. We will be using RDF triples to specify our data. This will be done as below:

{
  set {
    <_:employee1> <employeeName> "Ram" .
    <_:employee1> <employeeId> "123" .

    <_:employee2> <employeeName> "Laxman" .
    <_:employee2> <employeeId> "456" .

    <_:employee1> <worksFor> <_:company1> .
    <_:employee2> <worksFor> <_:company2> .

    <_:company1> <companyName> "Google" .
    <_:company2> <companyName> "Microsoft" .
  }
}

<_:variable> is a placeholder that we define to be filled in by a DGraph generated uid. We can name the variable anything, in this case, we have named it employee1, employee2, etc. We can reuse these placeholders, with the variable names, to link the nodes or reference the previously created nodes. For ex. we have created a placeholder value <_:employee1> which we have used multiple times to reference the same node. So, here, the same node, i.e, <_:employee1> will be getting employeeName as Ram and employeeId as 456. Moving further, it will also get a worksFor edge that will go towards a node containing the companyName as Google. We will keep all this wrapped in the set object as here we are creating data. If we wanted to delete data, we would have wrapped it in a delete object.

After entering the above mutation in your editor, hit the Run button. You will see Done as response. This means that we have been successful in creating our data. Yay! Now, to check what we have created in DGraph, let's get the data!

Getting the Data from DGraph

To get the data from DGraph is to query DGraph. We have created some data in DGraph in the above topic, let's now query that data. We will be executing the following query:

{
  getCompanyForEmployee(func: eq(employeeName, "Ram")) {
    employeeId
    worksFor {
      companyName
    }
  }
}

The above code snippet consists of a language known as GraphQL +/-. It is in itself a huge topic to be understood. For the scope of this article, we are just going to understand what the above query does. getCompanyForEmployee is the name of our query and nothing else. Moving further, we define a root function where we specify a condition on the basis of which the nodes would be filtered and returned. In this case, our root function will filter only those nodes which have an employeeName equal to Ram. Once we get those nodes, we further specify that from those filtered nodes, we need an employeeId. Then we traverse the worksFor edge coming out of those nodes, and then the node we get after traversing, we tell DGraph to give us the companyName from that node. So this query basically gets the employee ID and the name of the company Ram works in. With the above query in the editor, when you hit Run, you will get a graph as a response. Pretty amazing, right? The graph should look like something as below:

When you switch to the JSON tab on the left-hand side, you will be able to see the response in the JSON format, which is important as you will always be communicating with DGraph using JSON. You can replace Ram with Laxman and see the results as well. I'll leave that up to you.

Awesome! Now you have gotten a kickstart for using DGraph. You can go ahead and start exploring it further or read their docs and start trying out new stuff! This was just a start; there is much more greatness to be explored, which will surely be done in future articles. So, stay tuned!

References

Topics:
database ,graph database ,tutorial ,dgraph ,creating a schema in dgraph ,creating data in dgraph

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}