redis_fdw使用简介
来源:互联网 发布:nginx配置 编辑:程序博客网 时间:2024/05/16 01:12
转载地址:http://coolcodes.tech/tech/d0c2d2254836b3ebb92c769e576028a
建议大家去原始地址查看,原始地址还有很多作者的原创博文,值得推荐
Redis FDW(Foreign Data Wrapper)为Dave&Andrew实现的Postgresql的FDW扩展插件,可以直接在postgresql里访问redis数据库,支持Posgreql9.1~9.6。
1、安装
系统环境:Fedora25 / postgresql9.5.5 / redis3.0.6 (postgresql&redis均通过dnf安装)
1) 根据个人postgresql版本的clone对应的分支代码:git clone -b REL9_5_STABLE git@github.com:pg-redis-fdw/redis_fdw.git
2) 编译安装:
cd redis_fdw/
make USE_PGXS=1
make USE_PGXS=1 install
如果提示报错:/usr/lib/rpm/redhat/redhat-hardened-cc1 No that file and directory
,运行dnf install redhat-rpm-config -y
即可。
OK,安装完成。
2、实验
假设一个应用场景:在postgresql里有个表categories
记录不同分类下条目的数量:
CREATE TABLE categories
(
catid character varying(32) NOT NULL, --主键
cname character varying(64), --分类名称
nums integer, --条目数量
CONSTRAINT categories_pkey PRIMARY KEY (catid)
)
当这个表里的数据变化时,通过触发器自动更新redis里的catid--->nums
映射。
准备工作:
postgres=# CREATE EXTENSION redis_fdw; --创建redis_fdw扩展
postgres=# \dx
List of installed extensions
Name | Version | Schema | Description
-----------+---------+------------+--------------------------------------------------
redis_fdw | 1.0 | public | Foreign data wrapper for querying a Redis server
-- 创建需要关联的redis服务器
postgres=# CREATE SERVER redis_server
FOREIGN DATA WRAPPER redis_fdw
OPTIONS (address '127.0.0.1', port '6379');
postgres=# \des
List of foreign servers
Name | Owner | Foreign-data wrapper
--------------+----------+----------------------
redis_server | postgres | redis_fdw
--创建redis中存储catid/nums映射的虚拟表
postgres=# CREATE FOREIGN TABLE rd_categories (key text, val text)
SERVER redis_server
OPTIONS (database '0');
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+---------------+---------------+----------
public | categories | table | postgres
public | rd_categories | foreign table | postgres
创建触发器:
CREATE OR REPLACE FUNCTION public.insert_new_item()
RETURNS trigger AS
$BODY$
begin
insert into rd_categories values(NEW.catid, 0);
return NEW;
end
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION public.insert_new_item()
OWNER TO postgres;
CREATE TRIGGER new_category
AFTER INSERT
ON public.categories
FOR EACH ROW
EXECUTE PROCEDURE public.insert_new_item();
测试:
insert into categories values('123', 'ces', 0); --插入一条数据
在redis里查看:
[root@w1 redis_fdw]# redis-cli
127.0.0.1:6379> keys *
1) "123"
127.0.0.1:6379> get 123
"0"
参考
【1】github:pg-redis-fdw/redis_fdw
【2】PostgreSQL的postgres_fdw跨库使用
0 0
- redis_fdw使用简介
- PostgreSQL use redis_fdw connect to Redis
- OKHttp使用简介--使用
- tcpdump nc tcpdump使用简介使用简介
- DOM4J 使用简介[转载]
- Dom4j 使用简介
- DOM4J 使用简介(转)
- Gmail使用简介
- Debug 使用简介
- ant使用简介
- VSS使用简介
- CVS 使用简介
- DOM4J 使用简介
- ant使用简介
- DOM4J 使用简介
- DOM4J 使用简介
- ListView控件使用简介
- Debug 使用简介
- Mysql数据库调优 简单总结
- 正则表达式语法实例详解
- PDO预处理查询,插入
- linux数据库初始化配置
- 网站架构:负载均衡
- redis_fdw使用简介
- 笔记本选购简述
- 深入理解Activity——慕课网学习笔记
- Unity3D unet多人网络示例代码
- webstorm的相关配置
- Could not reserve enough space for 1572864KB object heap
- OpenGL学习笔记2:绘制和混合
- Spiral Matrix
- 设计模式:工厂方法-facotry method