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参数来制定参数的地址。
本文分享自 golang算法架构leetcode技术php 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!