> 文章列表 > 【YMatrix数据联邦】使用PXF访问S3对象存储

【YMatrix数据联邦】使用PXF访问S3对象存储

【YMatrix数据联邦】使用PXF访问S3对象存储

一、PXF安装

  • Platform Extension Framework(PXF)使用Foreign Data Wrapper(FDW)的机制访问存储在 YMatrix 数据库外部源中的数据。有关PXF的详细介绍可以参考 PXF中文文档 。
  • PXF需要Java 8或Java 11的运行环境,并且需要环境变量 $JAVA_HOME 已设置。PXF RPM默认安装路径为 /usr/local/pxf-matrixdb3。
  • 集群内所有机器均需要安装和配置PXF。

1. 安装

在所有节点上执行,需使用 root 用户或者具有 sudo 权限的普通用户

sudo yum install pxf-matrixdb3-5.16.0-1.el7.x86_64.rpm

2. 配置环境变量

在所有节点上执行,需使用 root 用户或者具有 sudo 权限的普通用户

sudo vim /etc/profileexport PXF_HOME=/usr/local/pxf-matrixdb3
export PXF_CONF=/usr/local/pxfconf
export PATH=$PXF_HOME/bin:$PATH

刷新环境变量,需使用 root 用户或者具有 sudo 权限的普通用户

source /etc/profile

3. 更改属主和属组

在所有节点上执行,需使用 root 用户或者具有 sudo 权限的普通用户

sudo chown -R mxadmin:mxadmin $PXF_HOME

4. 初始化PXF

前提是 YMatrix 已经启动,在主节点以 mxadmin 用户执行以下命令

pxf cluster init

如下,说明pxf配置文件已经同步成功

Initializing PXF on master host and 2 segment hosts...
PXF initialized successfully on 3 out of 3 hosts

5. 从 master 同步 PXF 配置文件到所有节点

在所有节点上执行
安装 rsync 用于同步pxf配置文件

sudo yum install -y rsync

在 YMatrix 集群的主节点上面,以 mxadmin 用户执行以下命令,同步pxf配置文件到集群所有节点上。

pxf cluster sync

如下,说明pxf配置文件已经同步成功

Syncing PXF configuration files from master host to 2 segment hosts...
PXF configs synced successfully on 2 out of 2 hosts

6. 启动 PXF

在 YMatrix 集群的主节点上面,以 mxadmin 用户执行以下命令,启动 pxf 服务。

pxf cluster start

如下,说明pxf启动成功

Starting PXF on 2 segment hosts...
PXF started successfully on 2 out of 2 hosts

7. Master 上单独启动

pxf 实例需要在 YMatrix 集群的主节点上面单独启动。
在 YMatrix 集群的主节点上,以 mxadmin 用户执行以下命令

pxf start

如下,表示 pxf 已经启动成功,pxf 相关的日志文件路径为/usr/local/pxfconf/logs,如果在使用过程中出现报错,可以到相应目录下查看日志。

Using CATALINA_BASE:   /usr/local/pxf-matrixdb3/pxf-service
Using CATALINA_HOME:   /usr/local/pxf-matrixdb3/pxf-service
Using CATALINA_TMPDIR: /usr/local/pxf-matrixdb3/pxf-service/temp
Using JRE_HOME:        /data/jdk1.8.0_151/jre
Using CLASSPATH:       /usr/local/pxf-matrixdb3/pxf-service/bin/bootstrap.jar:/usr/local/pxf-matrixdb3/pxf-service/bin/tomcat-juli.jar
Using CATALINA_PID:    /usr/local/pxf-matrixdb3/run/catalina.pid
Existing PID file found during start.
Removing/clearing stale PID file.
Tomcat started.
Checking if tomcat is up and running...
Server: PXF Server
Checking if PXF webapp is up and running...
PXF webapp is listening on port 5888

二、访问 S3

1. 配置 PXF 服务

在 YMatrix 集群的主节点上面,以 mxadmin 用户编辑 pxf 访问 s3 服务的配置文件 s3-site.xml,路径为/usr/local/pxfconf/servers/s3server_online/s3-site.xml

vim /usr/local/pxfconf/servers/s3server_online/s3-site.xml<?xml version="1.0" encoding="UTF-8"?>
<configuration><property><name>fs.s3a.endpoint</name><value>http:xxx</value></property><property><name>fs.s3a.access.key</name><value>xxx</value></property><property><name>fs.s3a.secret.key</name><value>xxx</value></property><property><name>fs.s3a.fast.upload</name><value>true</value></property>
</configuration>

参数说明:

  • **fs.s3a.endpoint :**对象存储的endpoint,私有部署的情况下为 http://:<>port。如果是公有云,那么需要指定网络可以连通的域名。
  • **fs.s3a.access.key:**访问对象存储的 Access_key
  • **fs.s3a.secret.key:**访问对象存储的 secret_key

2. 将文件同步到所有节点并重启 PXF

以下均在主节点上执行
在 YMatrix 集群的主节点上面,以 mxadmin 用户执行以下命令,同步pxf配置文件到集群所有节点上。

pxf cluster sync

如下,说明 pxf 配置文件已经同步成功

Syncing PXF configuration files from master host to 2 segment hosts...
PXF configs synced successfully on 2 out of 2 hosts

注意:需要检查一下其它节点上面的配置文件是否同步成功。

重启pxf集群服务
在 YMatrix 集群的主节点上面,以 mxadmin 用户执行以下命令,重启 pxf 服务。

pxf cluster restart

如下,说明 pxf 重启成功

Restarting PXF on 2 segment hosts...
PXF restarted successfully on 2 out of 2 hosts

pxf 实例需要在 YMatrix 集群的主节点上面单独重新启动。
在 YMatrix 集群的主节点上,以 mxadmin 用户执行以下命令。

pxf restart

如下,说明 pxf 重启成功

Using CATALINA_BASE:   /usr/local/pxf-matrixdb3/pxf-service
Using CATALINA_HOME:   /usr/local/pxf-matrixdb3/pxf-service
Using CATALINA_TMPDIR: /usr/local/pxf-matrixdb3/pxf-service/temp
Using JRE_HOME:        /usr/local/jdk1805/jre
Using CLASSPATH:       /usr/local/pxf-matrixdb3/pxf-service/bin/bootstrap.jar:/usr/local/pxf-matrixdb3/pxf-service/bin/tomcat-juli.jar
Using CATALINA_PID:    /usr/local/pxf-matrixdb3/run/catalina.pid
Tomcat stopped.
Using CATALINA_BASE:   /usr/local/pxf-matrixdb3/pxf-service
Using CATALINA_HOME:   /usr/local/pxf-matrixdb3/pxf-service
Using CATALINA_TMPDIR: /usr/local/pxf-matrixdb3/pxf-service/temp
Using JRE_HOME:        /usr/local/jdk1805/jre
Using CLASSPATH:       /usr/local/pxf-matrixdb3/pxf-service/bin/bootstrap.jar:/usr/local/pxf-matrixdb3/pxf-service/bin/tomcat-juli.jar
Using CATALINA_PID:    /usr/local/pxf-matrixdb3/run/catalina.pid
Tomcat started.
Checking if tomcat is up and running...
Server: PXF Server
Checking if PXF webapp is up and running...
PXF webapp is listening on port 5888

3. 创建测试表

在 YMatrix 集群的主节点上,以 mxadmin 用户使用 psql 进入到数据库。

psql dbname

创建 pxf_fdw 扩展
YMatrix 创建扩展需要用户具有 superuser 权限

CREATE EXTENSION pxf_fdw;

超级用户创建 S3 服务
config 需要和上文创建的文件夹 s3server_online 相对应

CREATE SERVER s3server_online FOREIGN DATA WRAPPER s3_pxf_fdw OPTIONS(config 's3server_online');

普通用户创建 S3 服务
若想使用普通用户创建 S3 服务需要给普通用户添加以下权限
执行以下命令需要用户具有 superuser 权限

 grant usage on FOREIGN DATA WRAPPER s3_pxf_fdw to username;

使用普通用户创建 S3 服务
config 需要和上文创建的文件夹 s3server_online 相对应

CREATE SERVER s3server_online FOREIGN DATA WRAPPER s3_pxf_fdw OPTIONS(config 's3server_online');

创建用户映射

CREATE USER MAPPING FOR mxadmin server s3server_online;

创建外部表

CREATE FOREIGN TABLE public.test (c1 jsonb,c2 text,c3 text,c4 text,c5 text,c6 jsonb,c7 jsonb
)
SERVER s3server_online
OPTIONS (format 'csv',resource '/<bucket_name>/<prefix>/<filename.csv>',JSONIFY_ARRAY 'TRUE',JSONIFY_MAP 'TRUE',JSONIFY_RECORD 'TRUE'
);

参数解释

  • **format:**对象存储上面存储的文件格式,支持的文件格式有 CSV、TXT、Parquet、ORC、Avro等。
  • **resource:**文件在 S3 上的存储的绝对路径。

三、案例

1. 创建示例文件并上传

创建示例文件 test.csv 并上传到 bucket ymatrix 文件路径为 ymatrix/test test.csv 文件数据为

1,zhangsan,41
2,lisi,29
3,wangwu,30

2. 配置 PXF 服务

在 YMatrix 集群的主节点上面,以 mxadmin 用户编辑 pxf 访问 s3 服务的配置文件 s3-site.xml,路径为/usr/local/pxfconf/servers/s3server_online/s3-site.xml

vim /usr/local/pxfconf/servers/s3server_online/s3-site.xml<?xml version="1.0" encoding="UTF-8"?>
<configuration><property><name>fs.s3a.endpoint</name><value>xxx</value></property><property><name>fs.s3a.access.key</name><value>xxx</value></property><property><name>fs.s3a.secret.key</name><value>xxx</value></property><property><name>fs.s3a.fast.upload</name><value>true</value></property>
</configuration>

3. 将文件同步到所有节点并重启 PXF

以下均在主节点上执行
在 YMatrix 集群的主节点上面,以 mxadmin 用户执行以下命令,同步pxf配置文件到集群所有节点上。

pxf cluster sync

如下,说明 pxf 配置文件已经同步成功

Syncing PXF configuration files from master host to 2 segment hosts...
PXF configs synced successfully on 2 out of 2 hosts

注意:需要检查一下其它节点上面的配置文件是否同步成功。

重启pxf集群服务
在 YMatrix 集群的主节点上面,以 mxadmin 用户执行以下命令,重启 pxf 服务。

pxf cluster restart

如下,说明 pxf 重启成功

Restarting PXF on 2 segment hosts...
PXF restarted successfully on 2 out of 2 hosts

pxf 实例需要在 YMatrix 集群的主节点上面单独重新启动。
在 YMatrix 集群的主节点上,以 mxadmin 用户执行以下命令。

pxf restart

如下,说明 pxf 重启成功

Using CATALINA_BASE:   /usr/local/pxf-matrixdb3/pxf-service
Using CATALINA_HOME:   /usr/local/pxf-matrixdb3/pxf-service
Using CATALINA_TMPDIR: /usr/local/pxf-matrixdb3/pxf-service/temp
Using JRE_HOME:        /usr/local/jdk1805/jre
Using CLASSPATH:       /usr/local/pxf-matrixdb3/pxf-service/bin/bootstrap.jar:/usr/local/pxf-matrixdb3/pxf-service/bin/tomcat-juli.jar
Using CATALINA_PID:    /usr/local/pxf-matrixdb3/run/catalina.pid
Tomcat stopped.
Using CATALINA_BASE:   /usr/local/pxf-matrixdb3/pxf-service
Using CATALINA_HOME:   /usr/local/pxf-matrixdb3/pxf-service
Using CATALINA_TMPDIR: /usr/local/pxf-matrixdb3/pxf-service/temp
Using JRE_HOME:        /usr/local/jdk1805/jre
Using CLASSPATH:       /usr/local/pxf-matrixdb3/pxf-service/bin/bootstrap.jar:/usr/local/pxf-matrixdb3/pxf-service/bin/tomcat-juli.jar
Using CATALINA_PID:    /usr/local/pxf-matrixdb3/run/catalina.pid
Tomcat started.
Checking if tomcat is up and running...
Server: PXF Server
Checking if PXF webapp is up and running...
PXF webapp is listening on port 5888

4. 创建测试表

在 YMatrix 集群的主节点上,以 mxadmin 用户使用 psql 进入到数据库。

psql test

以下均使用的超级用户
创建 pxf_fdw 扩展

CREATE EXTENSION pxf_fdw;

创建 S3 服务

CREATE SERVER s3server_online FOREIGN DATA WRAPPER s3_pxf_fdw OPTIONS(config 's3server_online');

创建用户映射

CREATE USER MAPPING FOR mxadmin server s3server_online;

创建外部表

CREATE FOREIGN TABLE public.test (c1 int,c2 text,c3 int
)
SERVER s3server_online
OPTIONS (format 'csv',resource 'ymatrix/test/test.csv',JSONIFY_ARRAY 'TRUE',JSONIFY_MAP 'TRUE',JSONIFY_RECORD 'TRUE'
);

查询数据

select * from test;

结果展示

 c1 |    c2    | c3 
----+----------+----1 | zhangsan | 412 | lisi     | 293 | wangwu   | 30
(3 rows)