Basic operations with Dgraph Database

Tram Ho

The reason for this series

Simply want to ask Viblo to note the knowledge learned in a single day. These are not complete blog posts, so people may not care about them

Reason to learn Dgraph

It rained today and COVID showed no sign of stopping. It is expected that there will be a long distance to avoid epidemic, so in addition to the daily work I still have to work, the time is quite a lot more (1-2 hours a day). So popping up on Github as a habit of checking Github discovery saw that this was trending in the GraphQL topic, so I decided to spend a few hours learning how it might be used later.

Start learning

Get started with Dgraph

  • Install Dgraph with Docker

  • The concept of nodes : In the Graph Database all entities or concepts are represented as nodes. It can be transactions, items, people … all of these entities are represented as nodes.
  • The concept of edge edges : All relationships between notes are represented by edges. Example in the following image:

Then we have two nodes representing 2 people and an edge representing the follows relationship between them. It can be seen that these two nodes have two attributes, age and name . All properties in a node are collectively referred to as predicates in Dgraph. The follows edge between these two nodes is also called a predicate although it is not a string or an integer but it points to another node.

Create a new node

All operations such as create, update, delete in Dgraph are called mutations. We try to add a new piece of data by going into the mutate tab

Add the following data:

This data segment will initialize two nodes representing 2 people. However, it does not show the relationship between the two nodes. To show that relationship, we need a little bit of change:

Query with the has function

Try rdungj mode query

Will list all the lists of nodes with predicate name and in the data returned will have people like RESTful

Create dynamic schema

As soon as predicates are added to the graph, the default Dgraph will generate corresponding schemas. This makes our application quite flexible, but if we want to bind the input fields must follow a fixed schema, there will be a way (will learn in the next article).

Basic data processing operations

Manipulate with UID

Once a node is created on the graph there will exist a corresponding uid and the addition of predicate or updates can be made to the uid . This is similar to using the primary key in a relational database. Let’s try the following example:

This example will create two users and let them follow each other. When we need to update, we also need to pass the corresponding uid . To query we will use

and get the following result:

Add edges with uid

We want to add another user to the follow list of the user whose uid = 0x2 as follows:

Browse through the edges.

So we have seen User 1 follow User 2 and User 2 follow User 3. We can browse through these edges by following as follows:

We get the following result

But this way is really not very neat. We can use recursive to call

We have the same result

Delete a predicate in the node

To delete an attribute in a node we use the following syntax

Similarly, to delete a follow we can also use the syntax

The basic data types in DGraph

To illustrate this section, we will build a small blog application with the following model:

The graph above consists of 3 entities: Author, Blog Post and Tags. There are several constraints in this graph as follows

  • All authors in graph may have one or more blog posts. The published edge represents the relationship between a post and its author. This edge has root in Author and vertex in Blog node
  • All blog posts may have one or more tags. The tagged edge shows the relationship between blog posts and their tags. This edge starts from the blog post node and points to the tag node .

Now let’s build the graph

After erecting the graph to the query to test

Then obtain the graph as follows:

In the graph above we see there

  • 3 node author blue
  • Each author has 2 blog posts green color
  • Each post has some pink tags, maybe individual tags or multiple post shares with some common tags

The data type for predicates

Normally, the data types for predicates will be automatically detected after graph creation is successful. Can see on the picture

The basic data types including string, float, int or uid and Dgraph also have many other data such as geo, datetime, boolean in it.

  • The uid type provides the linking edge between the two nodes
  • The array [uid] represents a collection of uid representing the representation of multiple relationships

Query the value of predicates

In addition to using the has function as above, we also have other functions used to query data with the meaning as shown below.

Let’s try the example of finding the best_author with the rule that the ratings are higher than 4.0

However, we see the results of the rating field have not been indexed

Note : We cannot query the value of a predicate unless the predicate has been indexed

Type the index for predicate

Indexx is a technique that enables queries on predicates to be performed at a faster rate and is required when querying the value of a predicate. Dgraph provides different index types for each specific data type as shown in the following table

To set the index can use the Schema tab on the interface

After indexing, we run the query again and we will get a successful result

Filter edges on the query

The above query helps us retrieve author information. Now we want to get more corresponding blog posts of that author

We obtained the following result

Now I want to filter articles with dislike number less than 12 then how. We use @filter it is like a Python decorator. At this point the query will become

And we also need to index this predicate dislikes. The following result is obtained:

Query with string predicate

For predicates that the data type is string , it is first necessary to index them. However, this predicate string index has many different types

in which the meaning of each type is as follows:

  • fulltext, term, trigram are some of the more superior types (like tokenizer in NLP)
  • exact index is only used for queries like eq, gt, lt, ge, le
  • hash index will make querying for eq faster but only for eq

Then query to try

Query with reverse edges

In the graph we see at the beginning, the relationship is one-way. So if you want to tag tagged posts from node tags, the following query will not work

To do so, you need to add reverse mode to the tagged by adding ~tagged . Query has encountered an error because the user has not reversed

After finishing rerverse, the result is obtained

Summary and comments

  • Using Dgraph is quite easy
  • Operation on RavelUI is very convenient and intuitive
  • The operation of the data is quite simple
  • Expect something better, but the general feeling is that it is quite easy to use and intuitive
Share the news now

Source : Viblo