PostgreSQL use redis_fdw connect to Redis

前端之家收集整理的这篇文章主要介绍了PostgreSQL use redis_fdw connect to Redis前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
首先要感谢Dave先生的贡献才有了redis_fdw。Dave贡献了两个FDW模块,MysqL_fdw和redis_fdw分别用于Postgresql9.1连接到MysqL和Redis。
接下来通过redis_fdw来测试一下Postgresql 9.1是怎么连接到Redis的。
首先到 http://redis.io下载一个redis2.4.1的稳定版源码。
安装参考README

[root@digoal redis-2.4.1]# tar -zxvf redis-2.4.1.tar.gz
[root@digoal redis-2.4.1]# cd redis-2.4.1
[root@digoal redis-2.4.1]# make
[root@digoal redis-2.4.1]# vi redis.conf

加上密码校验,并放置后台运行,用作测试其他参数不修改了。

daemonize yes
requirepass DIGOAL

启动redis

# src/redis-server ./redis.conf

确认已启动
# netstat -anp|grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 26647/redis-server

接下来在另一台Postgresql数据库服务器上配置redis_fdw.
首先是下载redis_fdw http://www.pgxn.org/user/pgsnake
解压到postgresql源码包的 postgresql-9.1.0/contrib/ 目录
然后把redis-2.4.1/deps/hiredis 目录拷贝到postgresql-9.1.0/contrib/redis_fdw-1.0.0
接下来编译hiredis
cd hiredis
make
make install

确认安装OK之后。编译redis_fdw
root@digoal redis_fdw1.00# . /home/postgres/.bash_profile
(导入两个环境变量 PATH,LD_LIBRARY_PATH)
PATH和LD_LIBRARY_PATH如下 : export PGHOME=/opt/pgsql91 LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usrlib64/usr/local/lib:/lib PATHbin:$PATH:.
#USE_PGXS=1 make clean
[root@digoal redis_fdw-1.0.0]#USE_PGXS=1 make
[root@digoal redis_fdw-1.0.0]#USE_PGXS=1 make install
-- 以下步骤可能可以省略,如果没有才需要拷贝.
#cpredis_fdw.so /opt/pgsql/lib/
[root@digoal redis_fdw-1.0.0]#cpredis_fdw--1.0.sql /opt/pgsql/share/extension/
[root@digoal redis_fdw-1.0.0]#cpredis_fdw.control/opt/pgsql/share/extension/

OK,至此就算安装好了。
接下来体验一下。
先到Postgresql数据库里面创建redis_fdw这个extension .
psql -h 127.00.1 digoal postgres
(9.1)
Type "help" for help.
digoal=# create extension redis_fdw;

然后到redis服务端插入一些数据。
vi ins.sh
set visitsdigoal1totals 1
digoal22
digoal33
digoal44
digoal55
digoal66
digoal77
digoal88
digoal99
digoal10totals 10
digoal1111
digoal1212
digoal1313
digoal1414
digoal1515
digoal1616
digoal1717
digoal1818
digoal1919
digoal2020
digoal2121
digoal2222
digoal2323
digoal2424
digoal2525
digoal2626
digoal2727
digoal2828
digoal2929
digoal3030
digoal3131
digoal3232
digoal3333
digoal3434
digoal3535
digoal3636
digoal3737
digoal3838
digoal3939
digoal4040
digoal4141
digoal4242
digoal4343
digoal4444
digoal4545
digoal4646
digoal4747
digoal4848
digoal4949
digoal5050
digoal5151
digoal5252
digoal5353
digoal5454
digoal5555
digoal5656
digoal5757
digoal5858
digoal5959
digoal6060
digoal6161
digoal6262
digoal6363
digoal6464
digoal6565
digoal6666
digoal6767
digoal6868
digoal6969
digoal7070
digoal7171
digoal7272
digoal7373
digoal7474
digoal7575
digoal7676
digoal7777
digoal7878
digoal7979
digoal8080
digoal8181
digoal8282
digoal8383
digoal8484
digoal8585
digoal8686
digoal8787
digoal8888
digoal8989
digoal9090
digoal9191
digoal9292
digoal9393
digoal9494
digoal9595
digoal9696
digoal9797
digoal9898
digoal9999
digoal100totals 100

导入到库0和库1
root@digoal src# cat ./ins.sh|./redis-cli -h 127.0.0.1 -p 6379 -a DIGOAL -n 0
[root@digoal src]# cat ./ins.sh|./redis-cli -h 127.0.0.1 -p 6379 -a DIGOAL -n 1

接下来在Postgresql数据库创建连接REDIS的外部表:

# CREATE SERVER redis_server1
FOREIGN DATA WRAPPER redis_fdw
OPTIONS (address '172.xxx.xxx.xxx',port '6379');
digoal=# CREATE USER MAPPING FOR digoal
SERVER redis_server1
OPTIONS (password 'DIGOAL');
CREATE USER MAPPING
digoal=# grant usage on FOREIGN server redis_server1 to digoal;
GRANT
digoal=> CREATE FOREIGN TABLE redis_db0 (key text,value text)
SERVER redis_server1
OPTIONS (database '0');
CREATE FOREIGN TABLE
digoal=> CREATE FOREIGN TABLE redis_db1 (key text,0)">OPTIONS (database '1');
CREATE FOREIGN TABLE
digoal=> select * from redis_db0 limit 5;
key | value
------------------------+-------
visits:digoal13:totals | 13
visits:digoal23:totals | 23
visits:digoal33:totals | 33
visits:digoal43:totals | 43
visits:digoal17:totals | 17
(5 rows)
=>select*from redis_db1 limit 5;
key | value
------------------------+-------
visitstotals 17
5 rows)
count(*) redis_db0count
-------
100
1 row redis_db1)

注意事项请参考redis_fdw README.
【参考】
http://redis.io
http://www.pgxn.org/dist/redis_fdw/
【其他FDW】
1. hive
https://github.com/youngwookim/hive-fdw-for-postgresql
2. Postgresql Foreign Table - pgsql_fdw
http://blog.163.com/digoal@126/blog/static/163877040201231514057303/
3. Postgresql Foreign Table - oracle_fdw 1
http://blog.163.com/digoal@126/blog/static/163877040201181505331588/
4. Postgresql Foreign Table - oracle_fdw 2
http://blog.163.com/digoal@126/blog/static/16387704020118151162340/
5. Postgresql Foreign Table - oracle_fdw 3
http://blog.163.com/digoal@126/blog/static/16387704020118951953408/
6. Postgresql Foreign Table - file_fdw
http://blog.163.com/digoal@126/blog/static/163877040201141641148311/
7. Postgresql Foreign Table - redis_fdw
http://blog.163.com/digoal@126/blog/static/16387704020119181188247/
8. Postgresql Foreign Table - MysqL_fdw 1
http://blog.163.com/digoal@126/blog/static/1638770402011111233524987/
9. Postgresql Foreign Table - MysqL_fdw 2
http://blog.163.com/digoal@126/blog/static/16387704020121108551698/


转帖:http://blog.163.com/digoal@126/blog/static/16387704020119181188247/
原文链接:https://www.f2er.com/postgresql/195825.html

猜你在找的Postgre SQL相关文章