SequoiaDB对接PosgreSQL

部署PosgreSQL

SequoidDB目前只支持9.3.4版本的PosgreSQL

  1. 安装readline和zlib依赖
  • Ubuntu:apt-get install libreadline6-dev apt-get install zliblg-dev
  • RedHat:yum install zlib-devel readline-devel
  1. 创建安装目录
    mkdir -p /opt/postgresql
    chown -R sdbadmin:sdbadmin_group /opt/postgresql
    解压安装包
    cp postgresql-9.3.4.tar.gz /opt
    cd /opt
    tar -zxvf postgresql-9.3.4.tar.gz
    源码编译配置
    su - sdbadmin
    $ cd /opt/postgresql-9.3.4/
    $ ./configure --prefix=/opt/postgresql
    安装
    make && make install
    编辑$HOME/.bash_profile,将如下变量添加至 sdbadmin 用户的环境变量中:
    export PGSQL_HOME=/opt/postgresql
    export PATH=$PGSQL_HOME/bin:$PATH
    export LD_LIBRARY_PATH=$PGSQL_HOME/lib:$LD_LIBRARY_PATH
    注意:不同的 Linux 操作系统需要编辑的启动文件存在差异。大多数 Linux 操作系统只用以下3个启动文件中的一个:
    $HOME/.bash_profile
    $HOME/.bash_login
    $HOME/.profile
    使环境变量生效:
    $ source ~/.bash_profile
    创建 PostgreSQL 的数据目录
    $ mkdir /opt/postgresql/data
    初始化数据目录(该操作只能操作一次)
    $ /opt/postgresql/bin/initdb -D /opt/postgresql/data

安装PosgreSQL-SequoiaDB插件

  1. 创建 PostgreSQL 的 lib 目录
  2. 获取 PostgreSQL 的 libdir 路径
    $ cd /opt/postgresql
    $ PGLIBDIR=$(bin/pg_config --libdir)
    如果显示的 libdir 目录不存在,则创建该目录:
    $ mkdir -p ${PGLIBDIR}
  3. 创建PostgreSQL的extension目录
  4. 获取 PostgreSQL 的 sharedir 路径
    $ PGSHAREDIR=$(bin/pg_config --sharedir)
  5. 检查 sharedir 目录下是否存在 extension 目录,若不存在,则创建该目录:
    $ mkdir -p ${PGSHAREDIR}/extension
  6. 安装 PostgreSQL 的扩展文件
    从 SequoiaDB 安装后的 postgresql 目录(默认为/opt/sequoiadb/postgresql)中拷贝 sdb_fdw.so 文件到PostgreSQL 的 lib 目录,并添加软链接。
    sdb_fdw.so 文件名如 sdb_fdw.so_2.2_23000 ,2.2 代表对应的 SequoiaDB 版本,23000 代表 Release 号
    $ cp -f /opt/sequoiadb/postgresql/sdb_fdw.so_2.2_23000 ${PGLIBDIR}
    $ cd ${PGLIBDIR}
    $ ln -s sdb_fdw.so_2.2_23000 sdb_fdw.so
  7. 将 sdb_fdw.control 和 sdb_fdw–1.0.sql 脚本拷贝到 extension 目录中:
    $ cp -f /opt/sequoiadb/postgresql/sdb_fdw.control ${PGSHAREDIR}/extension/
    $ cp -f /opt/sequoiadb/postgresql/sdb_fdw--1.0.sql ${PGSHAREDIR}/extension/

sdb_fdw.control 脚本内容:

# sdb_fdw extension
comment = ‘foreign data wrapper for SequoiaDB access’
default_version = ‘1.0’
module_pathname = ‘$libdir/sdb_fdw’
relocatable = true

sdb_fdw–1.0.sql 脚本内容:

/* contrib/sdb_fdw/sdb_fdw–1.0.sql */
– complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use “CREATE EXTENSION sdb_fdw” to load this file.\quit
CREATE FUNCTION sdb_fdw_handler()
RETURNS fdw_handler
AS ‘MODULE_PATHNAME’
LANGUAGE C STRICT;
CREATE FUNCTION sdb_fdw_validator(text[], oid)
RETURNS void
AS ‘MODULE_PATHNAME’
LANGUAGE C STRICT;
CREATE FOREIGN DATA WRAPPER sdb_fdw
HANDLER sdb_fdw_handler
VALIDATOR sdb_fdw_validator;

部署PosgreSQL

修改 PostgreSQL 的日志配置,日志中增加打印时间信息、连接信息等
$ cd /opt/postgresql
$ vi data/postgresql.conf

打印连接信息
log_connections = on

打印断连信息

log_disconnections = on

日志中打印时间,进程id,客户端地址信息
log_line_prefix = '%m %p %r'

出现错误时,断开当前连接
exit_on_error = on

检查端口是否被占用
PostgreSQL 默认启动端口为”5432”,检查端口是否被占用(检查操作建议使用 root 用户操作,只有检查端口需要 root 权限,其余操作还是需要在 sdbadmin 用户下操作)
# netstat -nap | grep 5432

如果5432端口被占用或者希望修改 PostgreSQL 的启动端口,可继续修改/opt/postgresql/data/postgresql.conf文件。如把启动端口改为11700。
port = 11700

启动 PostgreSQL 服务进程(需要使用 sdbadmin 用户执行以下命令)
$ bin/postgres -D data/ >> logfile 2>&1 &

检查 PostgreSQL 是否启动成功
# netstat -nap | grep 5432

tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 20502/postgres
unix 2 [ ACC ] STREAM LISTENING 40776754 20502/postgres /tmp/.s.PGSQL.5432

创建 PostgreSQL 的 database
$ bin/createdb -p 5432 foo

运行 PostgreSQL shell 命令行客户端
$ bin/psql -p 5432 foo

连接

以下操作均在PostgreSQL shell 环境下执行。
PostgreSQL与SequoiaDB建立关联
加载SequoiaDB连接驱动
foo=# create extension sdb_fdw;

配置与SequoiaDB连接参数
foo=# create server sdb_server foreign data wrapper sdb_fdw options(address '127.0.0.1', service '11810', user 'sdbUserName', password 'sdbPassword', preferedinstance 'A', transaction 'off');

Note:

  • 如果没有配置数据库密码验证,可以忽略user与password字段。

  • 如果需要提供多个协调节点地址,options 中的 address 字段可以按格式 'ip1:port1,ip2:port2,ip3:port3’填写。此时,service 字段可填写任意一个非空字符串。

  • preferedinstance 设置 SequoiaDB 的连接属性。多个属性以逗号分隔,如:preferedinstance ‘1,2,A’。详细配置请参考 preferedinstance 取值

  • preferedinstancemode 设置 preferedinstance 的选择模式

  • sessiontimeout 设置会话超时时间 如:sessiontimeout ‘100’

  • transaction 设置 SequoiaDB 是否开启事务,默认为off。开启为on

  • cipher 设置是否使用加密文件输入密码,默认为off。开启为on

  • token 设置加密口令

  • cipherfile 设置加密文件,默认为 ./passwd

关联SequoiaDB的集合空间与集合

foo=# create foreign table test (name text, id numeric) server sdb_server options ( collectionspace 'foo', collection 'bar', decimal 'on' ) ;
Note:

  • 集合空间与集合必须已经存在于SequoiaDB,否则查询出错。
  • 如果需要对接SequoiaDB的decimal字段,则需要在options中指定 decimal ‘on’ 。
  • pushdownsort 设置是否下压排序条件到 SequoiaDB,默认为on,关闭为off。
  • pushdownlimit 设置是否下压 limit 和 offset 条件到 SequoiaDB,默认为on,关闭为off。
  • 开启 pushdownlimit 时,必须同时开启 pushdownsort ,否则可能会造成结果非预期的问题。
  • 默认情况下,表的字段映射到SequoiaDB中为小写字符,如果强制指定字段为大写字符,创建方式参考“注意事项1”。
  • 映射 SequoiaDB 的数组类型,创建方式参考“注意事项2”。

开发

  1. 修改 PostgreSQL 的连接配置
    修改 PostgreSQL 的监听地址
    编辑 /opt/postgresql/data/postgresql.conf 文件,将
    listen_addresses = 'localhost'
    改为:
    listen_addresses = '0.0.0.0'
  2. 修改信任的机器列表
    编辑 /opt/postgresql/data/pg_hba.conf 文件,将
    # IPv4 local connections:
    host all all 127.0.0.1/32 trust
    改为:
    # IPv4 local connections:
    host all all 127.0.0.1/32 trust
    host all all 0.0.0.1/0 trust

重启 PostgreSQL
$ bin/pg_ctl stop -s -D pg_data/ -m fast
$ bin/postgres -D pg_data/ >> logfile 2>&1 &

JDBC连接程序

    package com.sequoiadb.sample;
    import java.sql.*;
    public class postgresql_sample {
        static{
            try {
                Class.forName("org.postgresql.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        public static void main( String[] args ) throws SQLException{
            String pghost = "192.168.30.182";
            String port = "5432";
            String databaseName = "foo";
            // postgresql process is running in which user
            String pgUser = "sdbadmin";
            String url = "jdbc:postgresql://"+pghost+":"+port+"/" + databaseName;
            Connection conn = DriverManager.getConnection(url, pgUser, null);
            Statement stmt = conn.createStatement();
            String sql = "select * from sdb_upcase_field ";
            ResultSet rs = stmt.executeQuery(sql);
            boolean isHeaderPrint = false;
            while (rs.next()) {
                ResultSetMetaData md = rs.getMetaData();
                int col_num = md.getColumnCount();
                if (!isHeaderPrint){
                    for (int i = 1; i  <= col_num; i++) {
                        System.out.print(md.getColumnName(i) + "|");
                        isHeaderPrint = true;
                    }
                }
                for (int i = 1; i <= col_num; i++) {
                    System.out.print(rs.getString(i) + "|");
                }
                System.out.println();
            }
            stmt.close();
            conn.close();
        }
    }
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值