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

Pink academical Gopher :)

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
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
Copy
We’ll refer to the host IP address via HOSTIPADDR.Create Docker networkdocker network create dgraph_default
Copy
Run 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
Copy
Run 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
Copy
mkdir ~/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
Copy
Notice 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
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
kubectl port-forward dgraph-0 8080
kubectl port-forward dgraph-0 9080 # grpc
kubectl port-forward dgraph-0 8000 # ui

2. Dgraph graph database Go tutorial

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"
)
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)
}
{
q(func: type(Link)) {
uid
url
}
}

Links & Learning Resources

Software Engineer. Go and Python. Learning Rust. https://kenanbek.github.io/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store