golang 源碼分析:cayley-1-

        https://github.com/cayleygraph/cayley 是 go 實現的一個圖數據庫,它支持多種後端存儲,包括 mysql,boltdb 甚至是 elasticsearch。下面我們先學習下如何使用它。

        首先下載源碼並下載依賴的靜態文件,然後編譯。

# clone project
git clone https://github.com/cayleygraph/cayley
cd cayley
# Download dependencies
go mod download
# Download web files (optional)
go run cmd/download_ui/download_ui.go
# Install packr 2
go get -u github.com/gobuffalo/packr/v2/packr2
% go install github.com/gobuffalo/packr/v2/packr2
Generate static files go modules
packr2
build the binary
go build ./cmd/cayley

會報錯

missing go.sum entry for module providing package github.com/gogo/protobuf/proto (imported by github.com/cayleygraph/quad/pquads); to add:
        go get github.com/cayleygraph/quad/pquads@v1.2.4

所以我們下載依賴然後編譯。

%   go get github.com/cayleygraph/cayley/cmd/cayley
go: downloading google.golang.org/protobuf v1.26.0
 %  go get github.com/dop251/goja@v0.0.0-20190105122144-6d5bf35058fa
% go build ./cmd/cayley

編譯完成後測試下命令

% ./cayley help
I0521 20:10:47.158113   58688 command.go:915] Cayley version: v0.8.x-dev (dev snapshot)
Cayley is a graph store and graph query layer.
Usage:
  cayley [command]
Available Commands:
  completion  Generate the autocompletion script for the specified shell
  convert     Convert quad files between supported formats.
  dedup       Deduplicate bnode values
  dump        Bulk-dump the database into a quad file.
  health      Health check HTTP server
  help        Help about any command
  http        Serve an HTTP endpoint on the given host and port.
  init        Create an empty database.
  load        Bulk-load a quad file into the database.
  query       Run a query in a specified database and print results.
  repl        Drop into a REPL of the given query language.
  schema      Commands related to RDF schema
  upgrade     Upgrade Cayley database to current supported format.
  version     Prints the version of Cayley.
Flags:
      --alsologtostderr string   log to standard error as well as files
      --backtrace string         when logging hits line file:N, emit a stack trace (default ":0")
      --batch int                size of quads batch to load at once (default 10000)
  -c, --config string            path to an explicit configuration file
      --cpuprofile string        path to output cpu profile
  -d, --db string                database backend to use: badger, bbolt, bolt, btree, cockroach, couch, elastic, leveldb, memstore, mongo, mysql, postgres, sqlite (default "memstore")
  -a, --dbpath string            path or address string for database
      --dup                      don't stop loading on duplicated on add (default true)
  -h, --help                     help for cayley
  -l, --log string               logs at or above this threshold go to stderr (default "2")
      --logs string              If non-empty, write log files in this directory
      --logtostderr string       log to standard error instead of files (default "true")
      --memprofile string        path to output memory profile
      --metrics string           host to serve metrics on (disabled by default)
      --missing                  don't stop loading on missing key on delete
      --pprof string             host to serve pprof on (disabled by default)
      --read_only                open database in read-only mode
  -v, --verbose string           log level for V logs
      --vmodule string           comma-separated list of pattern=N settings for file-filtered logging
Use "cayley [command] --help" for more information about a command.

我們可以用測試文件測試下

% ./cayley repl -i data/testdata.nq
I0521 20:11:21.013740   59131 command.go:915] Cayley version: v0.8.x-dev (dev snapshot)
I0521 20:11:21.014013   59131 repl.go:54] using backend "memstore"
I0521 20:11:21.015741   59131 repl.go:58] loaded "data/testdata.nq" in 1.666345ms
creating new history file: ".cayley_history"
cayley>

當然也可以啓用 http 服務

% ./cayley http -i data/testdata.nq
I0521 20:11:50.735282   59357 command.go:915] Cayley version: v0.8.x-dev (dev snapshot)
I0521 20:11:50.735518   59357 http.go:20] using backend "memstore"
I0521 20:11:50.736100   59357 http.go:24] loaded "data/testdata.nq" in 532.817µs
I0521 20:11:50.736170   59357 command.go:940] listening on 127.0.0.1:64210, web interface at http://127.0.0.1:64210

我們也可以加載源碼中帶的例子文件

% ./cayley http --load ./data/30kmoviedata.nq.gz 
I0521 20:17:40.920658   62301 command.go:915] Cayley version: v0.8.x-dev (dev snapshot)
I0521 20:17:40.921659   62301 http.go:20] using backend "memstore"
I0521 20:17:44.913698   62301 http.go:24] loaded "./data/30kmoviedata.nq.gz" in 3.991845522s
I0521 20:17:44.913809   62301 command.go:940] listening on 127.0.0.1:64210, web interface at http://127.0.0.1:64210

訪問地址 http://127.0.0.1:64210 就可以看到下面的頁面

可以嘗試使用 Gizmo 語法來進行查詢,例如:

g.V().getLimit(5);

獲取圖中的 5 個端點

{
    "result": [
        {
            "id": {
                "@id": "_:100000"
            }
        },
        {
            "id": {
                "@id": "/film/performance/actor"
            }
        },
        {
            "id": {
                "@id": "/en/larry_fine_1902"
            }
        },
        {
            "id": {
                "@id": "_:100001"
            }
        },
        {
            "id": {
                "@id": "/en/samuel_howard"
            }
        }
    ]
}
g.V()
  .has("<name>", "Humphrey Bogart")
  .all();

獲取名字包含 Humphrey Bogart 的所有端點

{
    "result": [
        {
            "id": {
                "@id": "/en/humphrey_bogart"
            }
        }
    ]
}
g.V()
  .has("<name>", "Casablanca")
  .out("</film/film/starring>")
  .out("</film/performance/actor>")
  .out("<name>")
  .all();
{
    "result": [
        {
            "id": "Humphrey Bogart"
        },
        {
            "id": "Ingrid Bergman"
        },
        {
            "id": "Paul Henreid"
        },
        {
            "id": "Claude Rains"
        },
        {
            "id": "Conrad Veidt"
        },
        {
            "id": "Sydney Greenstreet"
        },
        {
            "id": "Peter Lorre"
        },
        {
            "id": "S.Z. Sakall"
        },
        {
            "id": "Madeleine LeBeau"
        },
        {
            "id": "Dooley Wilson"
        },
        {
            "id": "Joy Page"
        },
        {
            "id": "John Qualen"
        },
        {
            "id": "Leonid Kinskey"
        },
        {
            "id": "Helmut Dantine"
        },
        {
            "id": "Lou Marcelle"
        }
    ]
}
var filmToActor = g
  .Morphism()
  .out("</film/film/starring>")
  .out("</film/performance/actor>");
g.V()
  .has("<name>", "Casablanca")
  .follow(filmToActor)
  .out("<name>")
  .all();
{
    "result": [
        {
            "id": "Humphrey Bogart"
        },
        {
            "id": "Ingrid Bergman"
        },
        {
            "id": "Paul Henreid"
        },
        {
            "id": "Claude Rains"
        },
        {
            "id": "Conrad Veidt"
        },
        {
            "id": "Sydney Greenstreet"
        },
        {
            "id": "Peter Lorre"
        },
        {
            "id": "S.Z. Sakall"
        },
        {
            "id": "Madeleine LeBeau"
        },
        {
            "id": "Dooley Wilson"
        },
        {
            "id": "Joy Page"
        },
        {
            "id": "John Qualen"
        },
        {
            "id": "Leonid Kinskey"
        },
        {
            "id": "Helmut Dantine"
        },
        {
            "id": "Lou Marcelle"
        }
    ]
}

以上就是基本使用,啓動服務端的時候也可以指定各種參數,比如後端存儲類型,store.backend,默認是 memory 即內存存儲。當然也還可以支持其它類型:

Key-Value backends

btree: An in-memory store, used mostly to quickly verify KV backend functionality.

leveldb: A persistent on-disk store backed by LevelDB.

bolt: Stores the graph data on-disk in a Bolt file

mongo: Stores the graph data and indices in a MongoDB instance.

elastic: Stores the graph data and indices in a ElasticSearch instance.

couch: Stores the graph data and indices in a CouchDB instance.

pouch: Stores the graph data and indices in a PouchDB. Requires building with GopherJS.

SQL backends

postgres: Stores the graph data and indices in a PostgreSQL instance.

cockroach: Stores the graph data and indices in a CockroachDB cluster.

mysql: Stores the graph data and indices in a MySQL or MariaDB instance.

sqlite: Stores the graph data and indices in a SQLite database.

通過 store.address 參數來制定參數的地址。

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/Vtdkew0pk8hqhrlNESAsjQ