zombodb 低级api 操作
zombodb 低级api 允许直接从zombodb 索引中进行insert、delete 文档,同时保留了mvcc 的特性,但是数据没有存储在
pg 中,但是也带来数据上的风险,我们需要注意进行es 数据的备份
api 说明
FUNCTION zdb.llapi_direct_insert(index_name regclass, data json) RETURNS void
FUNCTION zdb.llapi_direct_delete(index_name regclass, _id text) RETURNS void
参考例子
这是一个审计日志的功能,数据直接到es 中,而不用存储在pg 中
- 表创建
CREATE TABLE audit (
id serial8 NOT NULL PRIMARY KEY,
action_type char(2),
username varchar(64),
when_happened timestamp DEFAULT now(),
description text
);
- 索引创建
CREATE INDEX idxaudit
ON audit
USING zombodb ((audit.*))
WITH (llapi=true,url='http://elasticsearch:9200/');
- 创建rule
进行数据操作的处理规则定义
CREATE RULE audit_rule
AS ON INSERT TO audit
DO INSTEAD (
SELECT zdb.llapi_direct_insert('idxaudit', to_json(NEW))
);
- 数据添加操作
INSERT INTO audit (action_type, username, description)
VALUES ('aa', 'someuser', 'this is the first audit entry');
INSERT INTO audit (action_type, username, description)
VALUES ('qr', 'otheruser', 'this is the second audit entry');
INSERT INTO audit (action_type, username, description)
VALUES ('zy', 'anotheruser', 'this is the third audit entry');
- 数据查询
select * from audit;
数据为空,说明数据没有存储在pg 中
es 信息查看,从es 中可以看出数据都存储在es 中
- 使用zombodb api 查询数据
聚合查询
SELECT zdb.count('idxaudit', dsl.match_all()); count ;
-[ RECORD 1 ]
count | 3
记录查询
SELECT _id,
(source->>'id')::bigint AS id,
source->>'username' AS username,
source->>'action_type' AS action_type,
(source->>'when_happened')::timestamp AS timestamp,
source->>'description' AS description
FROM zdb.top_hits_with_id('idxaudit', ARRAY['*'], dsl.match_all(), 100);
[ RECORD 1 ]-------------------------------
_id | B8DQ22kBYb7kkl7X_khC
id | 2
username | otheruser
action_type | qr
timestamp | 2019-04-02 02:11:40.731217
description | this is the second audit entry
-[ RECORD 2 ]-------------------------------
_id | BsDQ22kBYb7kkl7X_khC
id | 1
username | someuser
action_type | aa
timestamp | 2019-04-02 02:11:40.731217
description | this is the first audit entry
-[ RECORD 3 ]-------------------------------
_id | CMDQ22kBYb7kkl7X_khC
id | 3
username | anotheruser
action_type | zy
timestamp | 2019-04-02 02:11:40.731217
description | this is the third audit entry
说明
zombodb 提供的低级api 还是很有意义的,用来作为一个日志审计功能,同时较少了pg 数据的存储
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2017-04-02 web 优化原则