Fast, Transactional, Distributed Graph Database.

简介

Dgraph is a horizontally scalable and distributed graph database, providing ACID transactions, consistent replication and linearizable reads. It's built from ground up to perform for a rich set of queries. Being a native graph database, it tightly controls how the data is arranged on disk to optimize for query performance and throughput, reducing disk seeks and network calls in a cluster.

Dgraph's goal is to provide Google production level scale and throughput, with low enough latency to be serving real time user queries, over terabytes of structured data. Dgraph supports GraphQL-like query syntax, and responds in JSON and Protocol Buffers over GRPC and HTTP.

入门

To get started with Dgraph, follow:

安装

Running the dgraph/standalone docker image is the quickest way to get started with Dgraph. This standalone image is meant for quickstart purposes only. It is not recommended for production environments.

Ensure that Docker is installed and running on your machine.

Now, it’s just a matter of running the following command, and you have Dgraph up and running.

docker run –rm -it -p 8000:8000 -p 8080:8080 -p 9080:9080 dgraph/standalone:latest

访问

The dgraph/standalone image setup comes with the useful Dgraph UI called Ratel. Just visit http://localhost:8000 from your browser, and you will be able to access it.

Query Language查询语法

GOLANG版本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main

import (
	"context"
	"flag"
	"fmt"
	"log"
    
	"github.com/dgraph-io/dgo/v2"
	"github.com/dgraph-io/dgo/v2/protos/api"
    
	"google.golang.org/grpc"
)

var (
	dgraph = flag.String("d", "127.0.0.1:9080", "Dgraph Alpha address")
)

func main() {
	flag.Parse()
	conn, err := grpc.Dial(*dgraph, grpc.WithInsecure())
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
    
	resp, err := dg.NewTxn().Query(context.Background(), `{
  bladerunner(func: eq(name@en, "Blade Runner")) {
    uid
    name@en
    initial_release_date
    netflix_id
  }
}`)
	
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Response: %s\n", resp.Json)
}

CURL版本

1
2
3
4
5
6
7
8
9
curl -H "Content-Type: application/graphql+-" localhost:8080/query -XPOST -d '
{
  bladerunner(func: uid(0x394c)) {
    uid
    name@en
    initial_release_date
    netflix_id
  }
}' | python -m json.tool | less

javascript

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
const dgraph = require("dgraph-js-http");

async function main() {
  const clientStub = new dgraph.DgraphClientStub("http://localhost:8080");
  const dgraphClient = new dgraph.DgraphClient(clientStub);

  const query = `{
  bladerunner(func: uid(0x394c)) {
    uid
    name@en
    initial_release_date
    netflix_id
  }
}`;
  const response = await dgraphClient.newTxn().query(query);
  console.log("Response: ", JSON.stringify(response.data));
}

main().then().catch((e) => {
  console.log("ERROR: ", e);
});

Dgraph compared to other graph DBs 图数据库比较

Features Dgraph Neo4j Janus Graph
Architecture Sharded and Distributed Single server (+ replicas in enterprise) Layer on top of other distributed DBs
Replication Consistent None in community edition (only available in enterprise) Via underlying DB
Data movement for shard rebalancing Automatic Not applicable (all data lies on each server) Via underlying DB
Language GraphQL inspired Cypher, Gremlin Gremlin
Protocols Grpc / HTTP + JSON / RDF Bolt + Cypher Websocket / HTTP
Transactions Distributed ACID transactions Single server ACID transactions Not typically ACID
Full Text Search Native support Native support Via External Indexing System
Regular Expressions Native support Native support Via External Indexing System
Geo Search Native support External support only Via External Indexing System
License Apache 2.0 GPL v3 Apache 2.0

特点

优点

  1. 支持分布式存储
  2. 支持集群部署
  3. 查询性能优
  4. 开源
  5. 采用GraphQL
  6. 支持grpc、http、rdf
  7. 支持事物

缺点

  1. 集群部署似乎比较复杂
  2. 查询语法偏代码比较重,不简洁
  3. 数据库neo4j迁移至dgraph工作量比较大

其它