如何用 PolarDB 整合age算法插件, 实现图式搜索加速 - 刑侦、社交、风控、族谱、推荐等业务图谱类关系数据搜索

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: 背景PolarDB 的云原生存算分离架构, 具备低廉的数据存储、高效扩展弹性、高速多机并行计算能力、高速数据搜索和处理; PolarDB与计算算法结合, 将实现双剑合璧, 推动业务数据的价值产出, 将数据变成生产力.本文将介绍PolarDB结合图式算法, 实现高效率的刑侦、社交、风控、族谱、推荐等业...

背景

PolarDB 的云原生存算分离架构, 具备低廉的数据存储、高效扩展弹性、高速多机并行计算能力、高速数据搜索和处理; PolarDB与计算算法结合, 将实现双剑合璧, 推动业务数据的价值产出, 将数据变成生产力.

本文将介绍PolarDB结合图式算法, 实现高效率的刑侦、社交、风控、族谱、推荐等业务图谱类关系数据搜索.

age是什么

https://age.apache.org/age-manual/master/intro/overview.html

Apache AGE is a PostgreSQL extension that provides graph database functionality. AGE is an acronym for A Graph Extension, and is inspired by Bitnine’s fork of PostgreSQL 10, AgensGraph, which is a multi-model database. The goal of the project is to create single storage that can handle both relational and graph model data so that users can use standard ANSI SQL along with openCypher, the Graph query language.

简单来说就是一个支持图式数据和搜索的多模数据库插件.

将age整合到PolarDB

https://age.apache.org/age-manual/master/intro/setup.html

https://age.apache.org/download

https://github.com/apache/age

https://github.com/apache/age/tree/release/1.1.0

  1. 下载最新分支并安装

git clone --branch release/1.1.0 --depth 1 https://github.com/apache/age  
  
cd age  
  
git branch  
* release/1.1.0  
  
which pg_config  
~/tmp_basedir_polardb_pg_1100_bld/bin/pg_config  
  1. 修复代码错误, 原因是RTE解析未兼容.

USE_PGXS=1 PG_CONFIG=~/tmp_basedir_polardb_pg_1100_bld/bin/pg_config make  
  
报错如下:  
src/backend/parser/cypher_analyze.c: In function ‘convert_cypher_walker’:  
src/backend/parser/cypher_analyze.c:178:17: error: ‘QTW_EXAMINE_RTES’ undeclared (first use in this function); did you mean ‘QTW_EXAMINE_RTES_AFTER’?  
  178 |         flags = QTW_EXAMINE_RTES | QTW_IGNORE_RT_SUBQUERIES |  
      |                 ^~~~~~~~~~~~~~~~  
      |                 QTW_EXAMINE_RTES_AFTER  
src/backend/parser/cypher_analyze.c:178:17: note: each undeclared identifier is reported only once for each function it appears in  
make: *** [<builtin>: src/backend/parser/cypher_analyze.o] Error 1  

原因如下:

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=18c0da88a5d9da566c3bfac444366b73bd0b57da

Split QTW_EXAMINE_RTES flag into QTW_EXAMINE_RTES_BEFORE/_AFTER.  
  
This change allows callers of query_tree_walker() to choose whether  
to visit an RTE before or after visiting the contents of the RTE  
(i.e., prefix or postfix tree order).  All existing users of  
QTW_EXAMINE_RTES want the QTW_EXAMINE_RTES_BEFORE behavior, but  
an upcoming patch will want QTW_EXAMINE_RTES_AFTER, and it seems  
like a potentially useful change on its own.  
  
Andreas Karlsson (extracted from CTE inlining patch)  
  
Discussion: https://postgr.es/m/8810.1542402910@sss.pgh.pa.us  

https://git.postgresql.org/gitweb/?p=postgresql.git;a=blobdiff;f=src/include/nodes/nodeFuncs.h;h=a9f76bbb330a3a271363be317fd8caea3e09fe7d;hp=7739600db26e55628778d93d1e2a3833d90954d9;hb=18c0da88a5d9da566c3bfac444366b73bd0b57da;hpb=ff750ce2d82979e9588c629955e161a9379b05f3

-#define QTW_EXAMINE_RTES           0x10    /* examine RTEs */  
-#define QTW_DONT_COPY_QUERY            0x20    /* do not copy top Query */  
+#define QTW_EXAMINE_RTES_BEFORE        0x10    /* examine RTE nodes before their  
+                                            * contents */  
+#define QTW_EXAMINE_RTES_AFTER     0x20    /* examine RTE nodes after their  
+                                            * contents */  
+#define QTW_DONT_COPY_QUERY            0x40    /* do not copy top Query */  

修复如下:

cd age  
vi src/backend/parser/cypher_analyze.c  
  
  
        /*  
         * QTW_EXAMINE_RTES  
         *     We convert RTE_FUNCTION (cypher()) to RTE_SUBQUERY (SELECT)  
         *     in-place.  
         *  
         * QTW_IGNORE_RT_SUBQUERIES  
         *     After the conversion, we don't need to traverse the resulting  
         *     RTE_SUBQUERY. However, we need to traverse other RTE_SUBQUERYs.  
         *     This is done manually by the RTE_SUBQUERY case above.  
         *  
         * QTW_IGNORE_JOINALIASES  
         *     We are not interested in this.  
         */  
        // flags = QTW_EXAMINE_RTES | QTW_IGNORE_RT_SUBQUERIES |  
        flags = QTW_EXAMINE_RTES_BEFORE | QTW_IGNORE_RT_SUBQUERIES |  
                QTW_IGNORE_JOINALIASES;   

以上参考12分支:

https://github.com/apache/age/blob/release/PG12/1.1.0/src/backend/parser/cypher_analyze.c

        /*  
         * QTW_EXAMINE_RTES  
         *     We convert RTE_FUNCTION (cypher()) to RTE_SUBQUERY (SELECT)  
         *     in-place.  
         *  
         * QTW_IGNORE_RT_SUBQUERIES  
         *     After the conversion, we don't need to traverse the resulting  
         *     RTE_SUBQUERY. However, we need to traverse other RTE_SUBQUERYs.  
         *     This is done manually by the RTE_SUBQUERY case above.  
         *  
         * QTW_IGNORE_JOINALIASES  
         *     We are not interested in this.  
         */  
        flags = QTW_EXAMINE_RTES_BEFORE | QTW_IGNORE_RT_SUBQUERIES |  
                QTW_IGNORE_JOINALIASES;  

修复后继续安装即可

USE_PGXS=1 PG_CONFIG=~/tmp_basedir_polardb_pg_1100_bld/bin/pg_config make  
USE_PGXS=1 PG_CONFIG=~/tmp_basedir_polardb_pg_1100_bld/bin/pg_config make install  
  1. 使用age

postgres=# LOAD 'age';  
LOAD  
postgres=# SET search_path = ag_catalog, "$user", public;  
SET  
  
-- 以上也可以根据需要配置到数据库参数postgresql.conf 中, 即自动加载age:   
-- #shared_preload_libraries = ''  # (change requires restart)  
-- #local_preload_libraries = ''  
-- #session_preload_libraries = ''  
-- #search_path = '"$user", public'        # schema names  
  
postgres=# create extension age ;  
CREATE EXTENSION  
  1. 一些图式查询语法例子

postgres=# SELECT * FROM ag_catalog.create_graph('graph_name');  
NOTICE:  graph "graph_name" has been created  
 create_graph   
--------------  
   
(1 row)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$#     RETURN 1  
postgres$# $$) AS (int_result agtype);  
 int_result   
------------  
 1  
(1 row)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$#     WITH [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as lst  
postgres$#     RETURN lst  
postgres$# $$) AS (lst agtype);  
                lst                   
------------------------------------  
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  
(1 row)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$#     WITH {listKey: [{inner: 'Map1'}, {inner: 'Map2'}], mapKey: {i: 0}} as m  
postgres$#     RETURN m.listKey[0]  
postgres$# $$) AS (m agtype);  
         m           
-------------------  
 {"inner": "Map1"}  
(1 row)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$# WITH {id: 0, label: "label_name", properties: {i: 0}}::vertex as v  
postgres$# RETURN v  
postgres$# $$) AS (v agtype);  
                                v                                   
------------------------------------------------------------------  
 {"id": 0, "label": "label_name", "properties": {"i": 0}}::vertex  
(1 row)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$# WITH {id: 2, start_id: 0, end_id: 1, label: "label_name", properties: {i: 0}}::edge as e  
postgres$# RETURN e  
postgres$# $$) AS (e agtype);  
                                             e                                                
--------------------------------------------------------------------------------------------  
 {"id": 2, "label": "label_name", "end_id": 1, "start_id": 0, "properties": {"i": 0}}::edge  
(1 row)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$# WITH [{id: 0, label: "label_name_1", properties: {i: 0}}::vertex,  
postgres$#             {id: 2, start_id: 0, end_id: 1, label: "edge_label", properties: {i: 0}}::edge,  
postgres$#            {id: 1, label: "label_name_2", properties: {}}::vertex  
postgres$#            ]::path as p  
postgres$# RETURN p  
postgres$# $$) AS (p agtype);  
                                                                                                                  p                                                                                         
                              
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  
----------------------------  
 [{"id": 0, "label": "label_name_1", "properties": {"i": 0}}::vertex, {"id": 2, "label": "edge_label", "end_id": 1, "start_id": 0, "properties": {"i": 0}}::edge, {"id": 1, "label": "label_name_2", "prop  
erties": {}}::vertex]::path  
(1 row)  
  
postgres=# WITH graph_query as (  
postgres(#     SELECT *  
postgres(#         FROM cypher('graph_name', $$  
postgres$#         MATCH (n)  
postgres$#         RETURN n.name, n.age  
postgres$#     $$) as (name agtype, age agtype)  
postgres(# )  
postgres-# SELECT * FROM graph_query;  
 name | age   
------+-----  
(0 rows)  
  
postgres=# SELECT *  
postgres-# FROM cypher('graph_name', $$  
postgres$# MATCH (n)  
postgres$# RETURN n.name  
postgres$# ORDER BY n.name  
postgres$# SKIP 3  
postgres$# $$) as (names agtype);  
 names   
-------  
(0 rows)  

更多用法请参考age文档:

https://age.apache.org/age-manual/master/intro/overview.html

参考

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
8月前
|
关系型数据库 分布式数据库 数据库
沉浸式学习PostgreSQL|PolarDB 10: 社交、刑侦等业务, 关系图谱搜索
业务场景1 介绍: 社交、刑侦等业务, 关系图谱搜索 - 营销、分销、流量变现、分佣、引爆流行、裂变式传播、家谱、选课、社交、人才库、刑侦、农产品溯源、药品溯源 图式搜索是PolarDB | PostgreSQL在(包括流计算、全文检索、图式搜索、K-V存储、图像搜索、指纹搜索、空间数据、时序数据、推荐等)诸多特性中的一个。 采用CTE语法,可以很方便的实现图式搜索(N度搜索、最短路径、点、边属性等)。 其中图式搜索中的:层级深度,是否循环,路径,都是可表述的。
203 0
沉浸式学习PostgreSQL|PolarDB 10: 社交、刑侦等业务, 关系图谱搜索
|
8月前
|
关系型数据库 分布式数据库 数据库
沉浸式学习PostgreSQL|PolarDB 8: 电商|短视频|新闻|内容推荐业务(根据用户行为推荐相似内容)、监控预测报警系统(基于相似指标预判告警)、音视图文多媒体相似搜索、人脸|指纹识别|比对 - 向量搜索应用
1、在电商业务中, 用户浏览商品的行为会构成一组用户在某个时间段的特征, 这个特征可以用向量来表达(多维浮点数组), 同时商品、店铺也可以用向量来表达它的特征. 那么为了提升用户的浏览体验(快速找到用户想要购买的商品), 可以根据用户向量在商品和店铺向量中进行相似度匹配搜索. 按相似度来推荐商品和店铺给用户. 2、在短视频业务中, 用户浏览视频的行为, 构成了这个用户在某个时间段的兴趣特征, 这个特征可以用向量来表达(多维浮点数组), 同时短视频也可以用向量来表达它的特征. 那么为了提升用户的观感体验(推荐他想看的视频), 可以在短视频向量中进行与用户特征向量的相似度搜索.
230 0
|
12月前
|
容灾 算法 数据可视化
闲鱼技术2022年度白皮书-服务端主题-电商搜索里都有啥?详解闲鱼搜索系统(中)
闲鱼技术2022年度白皮书-服务端主题-电商搜索里都有啥?详解闲鱼搜索系统
161 0
|
12月前
|
存储 移动开发 自然语言处理
|
12月前
|
搜索推荐 UED 索引
闲鱼技术2022年度白皮书-服务端主题-电商搜索里都有啥?详解闲鱼搜索系统(下)
闲鱼技术2022年度白皮书-服务端主题-电商搜索里都有啥?详解闲鱼搜索系统
173 0
|
10天前
|
算法 关系型数据库 分布式数据库
使用 PolarDB 开源版 smlar 插件进行高效率相似文本搜索、自助选药、相似人群圈选等业务
背景PolarDB 的云原生存算分离架构, 具备低廉的数据存储、高效扩展弹性、高速多机并行计算能力、高速数据搜索和处理; PolarDB与计算算法结合, 将实现双剑合璧, 推动业务数据的价值产出, 将数据变成生产力。本文将介绍使用 PolarDB 开源版 smlar 插件进行高效率相似文本搜索、自助...
22 0
|
存储 SQL 并行计算
如何用 PolarDB 整合age算法插件, 实现图式搜索加速 - 刑侦、社交、风控、族谱、推荐等业务图谱类关系数据搜索
PolarDB 的云原生存算分离架构, 具备低廉的数据存储、高效扩展弹性、高速多机并行计算能力、高速数据搜索和处理; PolarDB与计算算法结合, 将实现双剑合璧, 推动业务数据的价值产出, 将数据变成生产力. 本文将介绍PolarDB结合图式算法, 实现高效率的刑侦、社交、风控、族谱、推荐等业务图谱类关系数据搜索.
317 0
|
存储 算法 搜索推荐
使用 PolarDB 开源版 smlar 插件进行高效率相似文本搜索、自助选药、相似人群圈选等业务
PolarDB 的云原生存算分离架构, 具备低廉的数据存储、高效扩展弹性、高速多机并行计算能力、高速数据搜索和处理; PolarDB与计算算法结合, 将实现双剑合璧, 推动业务数据的价值产出, 将数据变成生产力. 本文将介绍使用 PolarDB 开源版 smlar 插件进行高效率相似文本搜索、自助选药、相似人群圈选等业务
348 0
|
算法
《海量数据场景下的淘宝搜索智能——算法及实践》电子版地址
海量数据场景下的淘宝搜索智能——算法及实践
84 0
《海量数据场景下的淘宝搜索智能——算法及实践》电子版地址
|
机器学习/深度学习 运维 搜索推荐
智能引擎搜索-基于问天引擎的智能搜索推荐算法开发|学习笔记
快速学习智能引擎搜索-基于问天引擎的智能搜索推荐算法开发
212 0
智能引擎搜索-基于问天引擎的智能搜索推荐算法开发|学习笔记