PostgreSQL 9.5 连接redis及其使用

来源:互联网 发布:聚类分析法要编程 编辑:程序博客网 时间:2024/05/16 11:46
今天有部分数据被同事放到redis上面了,需要同步过来。发现pg有连接redis插件,就参考德哥的文章(https://yq.aliyun.com/articles/14609)安装了一下,不过也遇到一些原文没有遇到问题。下面是我的安装过程:
首先redis_fdw有不同的branch,要根据自己的pg版本下载不同的branch
这里下载的是
redis_fdw-REL9_5_STABLE.zip

redis相关的库
hiredis-master.zip

解压后安装
cd hiredis
make
make PREFIX=/data/redis_fdw/hiredis_bin install

修改redis_fdw的Makefile

vi Makefile
# 末尾追加

LDFLAGS += -L/data/redis_fdw/hiredis_bin/lib
安装redis_fdw

source /home/pg9.5.2/.bash_profile
make USE_PGXS=1

参考德哥的博客安装的,死活报如下错误

redis_fdw.c: In function ‘redis_fdw_handler’:
redis_fdw.c:276: warning: assignment from incompatible pointer type
redis_fdw.c: In function ‘redisGetForeignPaths’:
redis_fdw.c:797: error: too many arguments to function ‘create_foreignscan_path’
redis_fdw.c: In function ‘redisGetForeignPlan’:
redis_fdw.c:833: error: too many arguments to function ‘make_foreignscan’
make: *** [redis_fdw.o] Error 1
反复换了各种版本redis_fdw的branch还是报错,其实上面的意思
上述的错误是指对应行的函数参数多了,我们删除几个,空值的参数
第一个函数删除一个NULL参数
第二个函数删除两个NIL参数
再次编译通过(不要问我为什么,就是这么简单:)
make USE_PGXS=1 install

继续安装
[pg9.5.2@postgres ~]$ psql -dpostgres -Upostgres -p5432
psql (9.5alpha2)
Type "help" for help.

postgres=# create extension redis_fdw;
ERROR:  could not load library "/opt/pgsql9.5.2/lib/redis_fdw.so": libhiredis.so.0.12: cannot open shared object file: No such file or directory

修改库地址
shared_preload_libraries = '/opt/pgsql9.5.2/lib/redis_fdw'   

启动还是报错
[pg9.5.2@postgres pg9.5.2data]$ FATAL:  XX000: could not load library "/opt/pgsql9.5.2/lib/redis_fdw.so": libhiredis.so.0.12: cannot open shared object file: No such file or directory
LOCATION:  internal_load_library, dfmgr.c:239

把这些库考进来
cp * /opt/pgsql9.5.2/lib/  

启动依然报上面的错误
修改权限:chown pg9.5.2:pg9.5.2 *

启动成功
[pg9.5.2@postgres pg9.5.2data]$ pg_ctl restart
pg_ctl: PID file "/data/pg9.5.2data/postmaster.pid" does not exist
Is server running?
starting server anyway
server starting
[pg9.5.2@postgres pg9.5.2data]$ LOG:  00000: redirecting log output to logging collector process
HINT:  Future log output will appear in directory "pg_log".
LOCATION:  SysLogger_Start, syslogger.c:622


继续操作
[pg9.5.2@postgres ~]$ psql -dpostgres -Upostgres -p5432
psql (9.5alpha2)
Type "help" for help.

postgres=# create extension redis_fdw;
CREATE EXTENSION
postgres=# CREATE SERVER redis_server
postgres-# FOREIGN DATA WRAPPER redis_fdw
postgres-# OPTIONS (address '127.0.0.1', port '6379');
CREATE SERVER
postgres=# CREATE FOREIGN TABLE redis_db0 (key text, val text)
postgres-# SERVER redis_server
postgres-# OPTIONS (database '0');
CREATE FOREIGN TABLE
postgres=# CREATE USER MAPPING FOR PUBLIC
postgres-# SERVER redis_server
postgres-# OPTIONS ();
ERROR:  syntax error at or near ")"
LINE 3: OPTIONS ();
                 ^
如果是无密码就写成空,但password参数还是需要的
postgres=# CREATE USER MAPPING FOR PUBLIC
postgres-# SERVER redis_server
postgres-# OPTIONS (password '');
CREATE USER MAPPING
postgres=# CREATE FOREIGN TABLE myredishash (key text, val text[])
postgres-# SERVER redis_server
postgres-# OPTIONS (database '0', tabletype 'hash', tablekeyprefix 'pack_config:');
CREATE FOREIGN TABLE
postgres=# select * from myredishash limit 10;
                    key                    |                                                val

-------------------------------------------+---------------------------------------------------
------------------------------------------------
 pack_config:2160070603:app:15158180750    | {1,"2016-06-08 15:46:23"}
 pack_config:2160150608:app:18970345322    | {1,"2016-06-11 13:20:24"}
 pack_config:2160150608:app:13777834990    | {1,"2016-06-16 15:09:18"}
 pack_config:2160320622:app:13857143019    | {0,"2016-06-24 15:40:01"}
 pack_config:2160070603:app:13575478184    | {1,"2016-06-04 19:23:06"}
 pack_config:2160050527:app:13023698286    | {1,"2016-06-02 07:41:49"}
 pack_config:2160150608:app:15382332310    | {4,"2016-06-09 08:15:12"}
 pack_config:2160220616:wechat:13867456883 | {1,"2016-06-24 13:14:13",2,"2016-06-24 13:14:19",4
,"2016-06-24 13:14:30"}
 pack_config:2160150608:app:13588335935    | {2,"2016-06-11 15:32:51",1,"2016-06-11 15:33:33"}
 pack_config:2160150608:app:18755998181    | {1,"2016-06-16 09:53:42",2,"2016-06-16 09:53:45",3
,"2016-06-16 09:53:46",4,"2016-06-16 09:53:48"}
(10 rows)

postgres=# select * from myredishash where key like '%15158180750%';
                  key                   |            val            
----------------------------------------+---------------------------
 pack_config:2160070603:app:15158180750 | {1,"2016-06-08 15:46:23"}
(1 row)


postgres=# select count(*) from myredishash    
;
 count 
-------
    10
(1 row)

参考文章:
https://yq.aliyun.com/articles/14609

转载地址:https://yq.aliyun.com/articles/57145
0 0