Dgraph Graph Database: How to Install and Use With Go?

Hello coders! In this tutorial, I will cover the following topics:
- How to setup dgraph? This part will include two installation options:
Option 1: Install dgraph using Docker
Option 2: Install dgraph on Kubernetes cluster - How to use dgraph Go client and write to a database?
Before we start, let me quickly explain what Dgraph is. Dgraph.io is a Graph Database written entirely in Go. As per official website it’s distributed by design and has various cool features like ACID transactions and shard re-balancing. It is open source and available under Apache 2.0.
1. How to install dgraph?
Use Docker image locally and run on your local machine:
docker pull dgraph/dgraph:latest
# You can test that it worked fine, by running:
docker run -it dgraph/dgraph:latest dgraph
More detailed version taken from the official documentation:
Dgraph cluster can be setup running as containers on a single host. First, you’d want to figure out the host IP address. You can typically do that viaip addr # On Arch Linux
ifconfig # On Ubuntu/Mac
CopyWe’ll refer to the host IP address via HOSTIPADDR.Create Docker networkdocker network create dgraph_default
CopyRun dgraph zeromkdir ~/zero # Or any other directory where data should be stored.docker run -it -p 5080:5080 --network dgraph_default -p 6080:6080 -v ~/zero:/dgraph dgraph/dgraph:latest dgraph zero --my=HOSTIPADDR:5080
CopyRun dgraph alphamkdir ~/server1 # Or any other directory where data should be stored.docker run -it -p 7080:7080 --network dgraph_default -p 8080:8080 -p 9080:9080 -v ~/server1:/dgraph dgraph/dgraph:latest dgraph alpha --lru_mb=<typically one-third the RAM> --zero=HOSTIPADDR:5080 --my=HOSTIPADDR:7080
Copymkdir ~/server2 # Or any other directory where data should be stored.docker run -it -p 7081:7081 --network dgraph_default -p 8081:8081 -p 9081:9081 -v ~/server2:/dgraph dgraph/dgraph:latest dgraph alpha --lru_mb=<typically one-third the RAM> --zero=HOSTIPADDR:5080 --my=HOSTIPADDR:7081 -o=1
CopyNotice the use of -o for server2 to override the default ports for server2.Run dgraph UIdocker run -it -p 8000:8000 --network dgraph_default dgraph/dgraph:latest dgraph-ratel
Install on your Kubernetes cluster:
kubectl create -f https://raw.githubusercontent.com/dgraph-io/dgraph/master/contrib/config/kubernetes/dgraph-single/dgraph-single.yaml
If you wish to delete the installation, run the following command:
kubectl delete -f https://raw.githubusercontent.com/dgraph-io/dgraph/master/contrib/config/kubernetes/dgraph-single/dgraph-single.yaml# ORkubectl delete pods,statefulsets,services,persistentvolumeclaims,persistentvolumes -l app=dgraph
You can also install by using Helm or in HA Cluster Setup. Please check the official documentation for more information.
Once you installed Dgraph, you can access Graph Dashboard via http://localhost:8000/. In case of Kubernetes installation you might need to forward ports:
kubectl port-forward dgraph-0 8080
kubectl port-forward dgraph-0 9080 # grpc
kubectl port-forward dgraph-0 8000 # ui
The last one is alpha-grpc. We will need it to access Dgraph from our Go client.
2. Dgraph graph database Go tutorial
For this tutorial we will use official dgraph Go client dgo.
Please note that this package has version 1 and version 2. In the official documentation, most of the examples refer to version 2; hence, make sure you use correct import paths. Example:
import (
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
)# or latest version
import (
"github.com/dgraph-io/dgo/v200"
"github.com/dgraph-io/dgo/v200/protos/api"
)
As per official documentation, there is no breaking API change from v2 to v200.
As a first step, let’s try to create and save some data in the database:
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
type Link struct {
Uid string `json:"uid,omitempty"`
URL string `json:"url,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}func main() {
ctx := context.TODO()
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
log.Fatal("failed to dial ", err)
}
defer conn.Close()
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))
txn := dgraphClient.NewTxn()
defer txn.Commit(ctx)
url := fmt.Sprintf("https://example.com/%v", time.Now().UnixNano())
link := Link{
URL: url,
DType: []string{"Link"},
}
lb, err := json.Marshal(link)
if err != nil {
log.Fatal("failed to marshal ", err)
}
mu := &api.Mutation{
SetJson: lb,
}
res, err := txn.Mutate(ctx, mu)
if err != nil {
log.Fatal("failed to mutate ", err)
}
print("res: %v", res)
}
Then we can query saved Link nodes by executing this query in Dgraph’s Ratel Dashboard:
{
q(func: type(Link)) {
uid
url
}
}
Links & Learning Resources
Dgraph GitHub: https://github.com/dgraph-io/dgraph
Dgraph dgo client: https://github.com/dgraph-io/dgo
Dgraph tour: https://dgraph.io/tour/
Thanks for reading this article. If you liked it, show your appreciation by clicking on the Clap button or following my profile. Also, check my YouTube channel for video tutorials: