前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >golang源码分析:cayley(1)

golang源码分析:cayley(1)

作者头像
golangLeetcode
发布2023-09-06 19:29:40
3290
发布2023-09-06 19:29:40
举报
文章被收录于专栏:golang算法架构leetcode技术php

https://github.com/cayleygraph/cayley是go实现的一个图数据库,它支持多种后端存储,包括mysql,boltdb甚至是elasticsearch。下面我们先学习下如何使用它。

首先下载源码并下载依赖的静态文件,然后编译。

代码语言:javascript
复制
# 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

会报错

代码语言:javascript
复制
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

所以我们下载依赖然后编译。

代码语言:javascript
复制
%   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

编译完成后测试下命令

代码语言:javascript
复制




% ./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.

我们可以用测试文件测试下

代码语言:javascript
复制
% ./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服务

代码语言:javascript
复制
% ./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

我们也可以加载源码中带的例子文件

代码语言:javascript
复制
% ./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语法来进行查询,例如:

代码语言:javascript
复制
g.V().getLimit(5);

获取图中的5个端点

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

获取名字包含Humphrey Bogart的所有端点

代码语言:javascript
复制
{
    "result": [
        {
            "id": {
                "@id": "/en/humphrey_bogart"
            }
        }
    ]
}
代码语言:javascript
复制
g.V()
  .has("<name>", "Casablanca")
  .out("</film/film/starring>")
  .out("</film/performance/actor>")
  .out("<name>")
  .all();
代码语言:javascript
复制
{
    "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"
        }
    ]
}
代码语言:javascript
复制
var filmToActor = g
  .Morphism()
  .out("</film/film/starring>")
  .out("</film/performance/actor>");
代码语言:javascript
复制
g.V()
  .has("<name>", "Casablanca")
  .follow(filmToActor)
  .out("<name>")
  .all();
代码语言:javascript
复制
{
    "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参数来制定参数的地址。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-05-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 golang算法架构leetcode技术php 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档