PostgreSQL 作为图数据库存储引擎

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
简介: PostgreSQL 与开源图数据库CayLey的结合。
CayLey是GO语言写的一个图数据库引擎,支持RESTful API,内置查询编辑器和可视化,支持MQL和JAVASCRIPT查询接口,后端存储支持文件格式,PostgreSQL,mongodb,LevelDB,Bolt。模块化设计,扩展后端存储非常容易。
本文将以PostgreSQL为例,演示一下CayLey的使用。
c34233c39a8d52a7bfc00b3362baa40d63a7a5d4

安装go:
yum install -y go

执行以下命令,克隆cayley和依赖:
mkdir -p ~/cayley && cd ~/cayley
export GOPATH=`pwd`
export PATH=$PATH:~/cayley/bin
mkdir -p bin pkg src/github.com/google
cd src/github.com/google
git clone https://github.com/google/cayley
cd cayley
go get github.com/tools/godep
godep restore
go build ./cmd/cayley

样本数据:
$ ll data
-rw-rw-r--. 1 postgres postgres 26M Jan 17 21:45 30kmoviedata.nq.gz
-rw-rw-r--. 1 postgres postgres 463 Jan 17 21:45 testdata.nq
$ gunzip 30kmoviedata.nq.gz

cayley使用帮助:
$ ./cayley --help
No command --help

Usage:
  cayley COMMAND [flags]

Commands:
  init      Create an empty database.
  load      Bulk-load a quad file into the database.
  http      Serve an HTTP endpoint on the given host and port.
  dump      Bulk-dump the database into a quad file.
  repl      Drop into a REPL of the given query language.
  version   Version information.

Flags:
  -alsologtostderr=false: log to standard error as well as files
  -assets="": Explicit path to the HTTP assets.
  -config="": Path to an explicit configuration file.
  -db="memstore": Database Backend.
  -dbpath="/tmp/testdb": Path to the database.
  -dump="dbdump.nq": Quad file to dump the database to (".gz" supported, "-" for stdout).
  -dump_type="quad": Quad file format ("json", "quad", "gml", "graphml").
  -format="cquad": Quad format to use for loading ("cquad" or "nquad").
  -host="127.0.0.1": Host to listen on (defaults to all).
  -ignoredup=false: Don't stop loading on duplicated key on add
  -ignoremissing=false: Don't stop loading on missing key on delete
  -init=false: Initialize the database before using it. Equivalent to running `cayley init` followed by the given command.
  -load_size=10000: Size of quadsets to load
  -log_backtrace_at=:0: when logging hits line file:N, emit a stack trace
  -log_dir="": If non-empty, write log files in this directory
  -logstashtype="": enable logstash logging and define the type
  -logstashurl="172.17.42.1:5042": logstash url and port
  -logtostderr=false: log to standard error instead of files
  -port="64210": Port to listen on.
  -prof="": Output profiling file.
  -quads="": Quad file to load before going to REPL.
  -query_lang="gremlin": Use this parser as the query language.
  -read_only=false: Disable writing via HTTP.
  -replication="single": Replication method.
  -stderrthreshold=0: logs at or above this threshold go to stderr
  -timeout=30s: Elapsed time until an individual query times out.
  -v=0: log level for V logs
  -vmodule=: comma-separated list of pattern=N settings for file-filtered logging

假设已有一个PostgreSQL数据库。
IP : 192.168.150.132
PORT : 1921
DBNAME : postgres
USER : digoal
PWD : digoal_pwd

初始化
./cayley init -db=sql -dbpath="postgres://digoal:digoal_pwd@192.168.150.132:1921/postgres?sslmode=disable"

导入数据
./cayley load -quads="data/" -db=sql -dbpath="postgres://digoal:digoal_pwd@192.168.150.132:1921/postgres?sslmode=disable"
50亿测试数据约2TB。

开启repl或http接口服务。
./cayley repl -db=sql -dbpath="postgres://digoal:digoal_pwd@192.168.150.132:1921/postgres?sslmode=disable" -host="0.0.0.0" -port="64210"
./cayley http -db=sql -dbpath="postgres://digoal:digoal_pwd@192.168.150.132:1921/postgres?sslmode=disable" -host="0.0.0.0" -port="64210"

使用http接口的图例:
65a18ac0601d02c0e3b815593896dcf6e54cfa2f

 Query Shape:
63f6027f023a3dc03c89891897245f6179f2610c
后端是PostgreSQL时,Cayley自动将MQL或JAVASCRIPT自动转换成SQL到数据库查询,并返回结果。

对于PostgreSQL作为后端的场景,优化的手段:
1. 使用GPU加速HASH JOIN和数据扫描。
2. 使用分区表,减少无用块扫描。
3. 其他通用的PG优化手段

如果数据量大到单库的计算资源和IO资源性能支撑不住,可以用Greenplum来实现分布式查询。

查询接口:
Javascript/Gremlin API documentation
图对象
  根据节点ID,检索,返回路径
路径对象
  路径相交,节点匹配等
查询路径对象
  数值转换,等。

[参考]
相关实践学习
阿里云图数据库GDB入门与应用
图数据库(Graph Database,简称GDB)是一种支持Property Graph图模型、用于处理高度连接数据查询与存储的实时、可靠的在线数据库服务。它支持Apache TinkerPop Gremlin查询语言,可以帮您快速构建基于高度连接的数据集的应用程序。GDB非常适合社交网络、欺诈检测、推荐引擎、实时图谱、网络/IT运营这类高度互连数据集的场景。 GDB由阿里云自主研发,具备如下优势: 标准图查询语言:支持属性图,高度兼容Gremlin图查询语言。 高度优化的自研引擎:高度优化的自研图计算层和存储层,云盘多副本保障数据超高可靠,支持ACID事务。 服务高可用:支持高可用实例,节点故障迅速转移,保障业务连续性。 易运维:提供备份恢复、自动升级、监控告警、故障切换等丰富的运维功能,大幅降低运维成本。 产品主页:https://www.aliyun.com/product/gdb
目录
相关文章
|
19天前
|
NoSQL 关系型数据库 MySQL
微服务架构下的数据库选择:MySQL、PostgreSQL 还是 NoSQL?
在微服务架构中,数据库的选择至关重要。不同类型的数据库适用于不同的需求和场景。在本文章中,我们将深入探讨传统的关系型数据库(如 MySQL 和 PostgreSQL)与现代 NoSQL 数据库的优劣势,并分析在微服务架构下的最佳实践。
|
12天前
|
SQL 关系型数据库 数据库
PostgreSQL数据库报错 ERROR: multiple default values specified for column "" of table "" 如何解决?
PostgreSQL数据库报错 ERROR: multiple default values specified for column "" of table "" 如何解决?
167 59
|
6天前
|
Oracle NoSQL 关系型数据库
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
17 2
|
16天前
|
缓存 关系型数据库 数据库
如何优化 PostgreSQL 数据库性能?
如何优化 PostgreSQL 数据库性能?
19 2
|
1月前
|
关系型数据库 分布式数据库 数据库
开源云原生数据库PolarDB PostgreSQL 15兼容版本正式发布
PolarDB进行了深度的内核优化,从而实现以更低的成本提供商业数据库的性能。
|
2月前
|
存储 Ubuntu 关系型数据库
如何创建数据库 PostgreSQL
【8月更文挑战第12天】
39 4
如何创建数据库 PostgreSQL
|
2月前
|
关系型数据库 MySQL Linux
在Linux中,如何配置数据库服务器(如MySQL或PostgreSQL)?
在Linux中,如何配置数据库服务器(如MySQL或PostgreSQL)?
|
28天前
|
关系型数据库 数据库 网络虚拟化
Docker环境下重启PostgreSQL数据库服务的全面指南与代码示例
由于时间和空间限制,我将在后续的回答中分别涉及到“Python中采用lasso、SCAD、LARS技术分析棒球运动员薪资的案例集锦”以及“Docker环境下重启PostgreSQL数据库服务的全面指南与代码示例”。如果你有任何一个问题的优先顺序或需要立即回答的,请告知。
47 0
|
2月前
|
存储 Ubuntu 关系型数据库
如何在 Ubuntu VPS 上备份 PostgreSQL 数据库
如何在 Ubuntu VPS 上备份 PostgreSQL 数据库
20 1
|
3月前
|
JavaScript 关系型数据库 API
Nest.js 实战 (二):如何使用 Prisma 和连接 PostgreSQL 数据库
这篇文章介绍了什么是Prisma以及如何在Node.js和TypeScript后端应用中使用它。Prisma是一个开源的下一代ORM,包含PrismaClient、PrismaMigrate、PrismaStudio等部分。文章详细叙述了安装PrismaCLI和依赖包、初始化Prisma、连接数据库、定义Prisma模型、创建Prisma模块的过程,并对比了Prisma和Sequelize在Nest.js中的使用体验,认为Prisma更加便捷高效,没有繁琐的配置。
116 7
Nest.js 实战 (二):如何使用 Prisma 和连接 PostgreSQL 数据库

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版
  • 下一篇
    无影云桌面