DataX与DB2导入导出案例
DataX与DB2导入导出案例
0. 写在前面
- Linux版本:
CentOS-7.5-x86_64-DVD-1804
- DB2版本:
LINUXX8664 11.5.4.0
(node02机器) - DataX版本:
- Python版本:
Python 2.7.5
- DataX模式:
单机
版(node01机器) - 关闭防火墙
- 关闭SELinux
- 配置本地yum源
1. DB2介绍
DB2 是 IBM 公司于 1983 年研制的一种
关系型数据库系统
(Relational Database Management System),主要应用于大型应用系统,具有较好的可伸缩性 。DB2 是 IBM 推出 的第二个关系型数据库,所以称为 db2。DB2 提供了高层次的数据利用性、完整性、安全性 、并行性、可恢复性,以及小规模到大规模应用程序的执行能力,具有与平台无关的基本功能和SQL 命令运行环境。可以同时在不同操作系统使用,包括Linux、UNIX 和 Windows。
2. DB2数据库对象关系
- instance, 同一台机器上可以安装多个DB2 instance。
- database, 同一个 instance 下面可以创建有多个 database。
- schema, 同一个database 下面可以配置多个schema。
- table,同一个 schema 下可以创建多个table。
3. 安装前的准备
3.1 安装依赖
sudo yum install -y bc binutils compat-libcap1 compat-libstdc++33 elfutils-libelf elfutils-libelf-devel fontconfig-devel glibc glibc-devel ksh libaio libaio-devel libX11 libXau libXi libXtst libXrender libXrender-devel libgcc libstdc++ libstdc++- devel libxcb make smartmontools sysstat kmod* gcc-c++ compat-libstdc++-33 libstdc++.so.6 kernel-devel pam-devel.i686 pam.i686 pam32*
3.2 修改配置文件 sysctl.conf
[root@node02 module]# vim /etc/sysctl.conf
删除里面的内容,再添加如下内容:
net.ipv4.ip_local_port_range = 9000 65500
fs.file-max = 6815744
kernel.shmall = 10523004
kernel.shmmax = 6465333657
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
net.core.rmem_default=262144
net.core.wmem_default=262144
net.core.rmem_max=4194304
net.core.wmem_max=1048576
fs.aio-max-nr = 1048576
3.3 修改配置文件 limits.conf
[root@node02 module]# vim /etc/security/limits.conf
在文件末尾添加:
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536
Note:重启node02生效
4. 安装
4.1 预检查
- 执行以下命令,开始预检查
[root@node02 server_dec]# ./db2prereqcheck -l -s
需求与 Db2 数据库 "Server" 不匹配。版本:"11.5.4.0"。 当前系统上未满足的先决条件的摘要: DBT3514W db2prereqcheck 实用程序未能找到以下 32 位库文件:"/lib/libpam.so*"。
需求与 Db2 数据库 "Server" 带 pureScale 功能部件 不匹配。版本:"11.5.4.0"。 当前系统上未满足的先决条件的摘要: DBT3613E db2prereqcheck 实用程序无法验证对应 TSA 的先决条件。请确保您的机器满足所有 TSA 安装先决条件。
DBT3507E db2prereqcheck 实用程序未能找到以下程序包或文件:"kernel-source"。
出现Error:缺少
「 32 位库文件:"/lib/libpam.so*"」
- 关于这个Error,请检查和
pam
相关的依赖是否安装成功
[root@node02 server_dec]# rpm -qa | grep pam
pam-1.1.8-22.el7.x86_64
[root@node02 server_dec]# rpm -qa | grep pam-devel
没有
pam-devel
,重新安装依赖
[root@node02 server_dec]# yum install -y pam-devel.i686
- 再次预检查
[root@node02 server_dec]# ./db2prereqcheck -l -s
DBT3533I db2prereqcheck 实用程序已确认所有安装先决条件均已满足。 需求与 Db2 数据库 "Server" 带 pureScale 功能部件 不匹配。版本:"11.5.4.0"。 当前系统上未满足的先决条件的摘要: DBT3613E db2prereqcheck 实用程序无法验证对应 TSA 的先决条件。请确保您的机器满足所有 TSA 安装先决条件。
DBT3507E db2prereqcheck 实用程序未能找到以下程序包或文件:"kernel-source"。
除了本次命令执行结果中的那两个依赖「DBT3533I、DBT3507E」没安装不会影响到DB2使用之外,如果有其他依赖包未安装成功,则这些依赖都需要先安装。
4.2 添加组和用户
新增用户组db2inst1和db2fenc1、新增用户db2iadm1、db2iadm1,并将新增的两个用户添加到对应的新增的组中,最后为两个新用户设置密码
[root@node02 server_dec]# groupadd -g 2000 db2iadm1
[root@node02 server_dec]# groupadd -g 2001 db2fadm1
[root@node02 server_dec]# useradd -m -g db2iadm1 -d /home/db2inst1 db2inst1
[root@node02 server_dec]# useradd -m -g db2iadm1 -d /home/db2fenc1 db2fenc1
[root@node02 server_dec]# passwd db2inst1
更改用户 db2inst1 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@node02 server_dec]# passwd db2fenc1
更改用户 db2fenc1 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
- db2inst1: 实例所有者
- db2fenc1: 受防护用户
4.3 创建实例
- db2的服务端口默认是50000
- 进入db2安装目录下的instance目录
- 执行
db2icrt
命令创建实例- 看到
The execution completed successfully.
代表实例创建成功
[root@node02 ~]# cd /opt/ibm/db2/V11.5/instance
[root@node02 instance]# ./db2icrt -p 50000 -u db2fenc1 db2inst1
DBI1446I The db2icrt command is running.
DB2 installation is being initialized.
Total number of tasks to be performed: 4
Total estimated time for all tasks to be performed: 309 second(s)
Task #1 start
Description: Setting default global profile registry variables
Estimated time 1 second(s)
Task #1 end
Task #2 start
Description: Initializing instance list
Estimated time 5 second(s)
Task #2 end
Task #3 start
Description: Configuring DB2 instances
Estimated time 300 second(s)
Task #3 end
Task #4 start
Description: Updating global profile registry
Estimated time 3 second(s)
Task #4 end
The execution completed successfully.
For more information see the DB2 installation log at "/tmp/db2icrt.log.55121".
DBI1070I Program db2icrt completed successfully.
4.4 创建实例库、开启服务
创建实例库
- 切换到
db2inst1
用户 - 进入db2安装目录下的instance目录
- 执行
db2sampl
命令创建实例库
[root@node02 instance]# su - db2inst1
上一次登录:六 1月 14 17:07:30 CST 2023pts/0 上
[db2inst1@node02 ~]$ cd /opt/ibm/db2/V11.5/instance/
[db2inst1@node02 instance]$ db2sampl
Note:
db2sampl
命令会自动创建一个名为sample
的数据库实例,如下图所示:
如上图所示.代表实例库创建成功
- 启动服务
db2start
命令用于启动db2服务
[db2inst1@node02 instance]$ db2start
01/14/2023 17:16:08 0 0 SQL1063N DB2START processing was successful.
SQL1063N DB2START processing was successful.
4.5 连接
- 进入交互式环境
[db2inst1@node02 instance]$ db2
(c) Copyright IBM Corporation 1993,2007
Command Line Processor for DB2 Client 11.5.4.0
You can issue database manager commands and SQL statements from the command
prompt. For example:
db2 => connect to sample
db2 => bind sample.bnd
For general help, type: ?.
For command help, type: ? command, where command can be
the first few keywords of a database manager command. For example:
? CATALOG DATABASE for help on the CATALOG DATABASE command
? CATALOG for help on all of the CATALOG commands.
To exit db2 interactive mode, type QUIT at the command prompt. Outside
interactive mode, all commands must be prefixed with 'db2'.
To list the current command option settings, type LIST COMMAND OPTIONS.
For more detailed help, refer to the Online Reference Manual.
db2 =>
- 连接数据库实例
db2 => connect to sample
Database Connection Information
Database server = DB2/LINUXX8664 11.5.4.0
SQL authorization ID = DB2INST1
Local database alias = SAMPLE
- 查看样例库sample下的所有表
Note:sql语句后不要加分号
db2 => list tables
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
ACT DB2INST1 T 2023-01-14-17.14.29.830759
ADEFUSR DB2INST1 S 2023-01-14-17.14.31.218932
CATALOG DB2INST1 T 2023-01-14-17.14.33.002045
CL_SCHED DB2INST1 T 2023-01-14-17.14.29.299734
CUSTOMER DB2INST1 T 2023-01-14-17.14.32.839163
DEPARTMENT DB2INST1 T 2023-01-14-17.14.29.340559
DEPT DB2INST1 A 2023-01-14-17.14.29.422197
EMP DB2INST1 A 2023-01-14-17.14.29.487346
EMPACT DB2INST1 A 2023-01-14-17.14.29.829392
EMPLOYEE DB2INST1 T 2023-01-14-17.14.29.423121
EMPMDC DB2INST1 T 2023-01-14-17.14.31.348910
EMPPROJACT DB2INST1 T 2023-01-14-17.14.29.801185
EMP_ACT DB2INST1 A 2023-01-14-17.14.29.830162
EMP_PHOTO DB2INST1 T 2023-01-14-17.14.29.488199
EMP_RESUME DB2INST1 T 2023-01-14-17.14.29.577152
INVENTORY DB2INST1 T 2023-01-14-17.14.32.792380
IN_TRAY DB2INST1 T 2023-01-14-17.14.29.888552
ORG DB2INST1 T 2023-01-14-17.14.29.914447
PRODUCT DB2INST1 T 2023-01-14-17.14.32.707200
PRODUCTSUPPLIER DB2INST1 T 2023-01-14-17.14.33.135046
PROJ DB2INST1 A 2023-01-14-17.14.29.744731
PROJACT DB2INST1 T 2023-01-14-17.14.29.746236
PROJECT DB2INST1 T 2023-01-14-17.14.29.670584
PURCHASEORDER DB2INST1 T 2023-01-14-17.14.32.919101
SALES DB2INST1 T 2023-01-14-17.14.29.959681
STAFF DB2INST1 T 2023-01-14-17.14.29.936877
STAFFG DB2INST1 T 2023-01-14-17.14.31.033939
STUDENT DB2INST1 T 2023-01-14-17.19.57.468544
SUPPLIERS DB2INST1 T 2023-01-14-17.14.33.069115
VACT DB2INST1 V 2023-01-14-17.14.29.999212
VASTRDE1 DB2INST1 V 2023-01-14-17.14.30.013130
VASTRDE2 DB2INST1 V 2023-01-14-17.14.30.016328
VDEPMG1 DB2INST1 V 2023-01-14-17.14.30.006266
VDEPT DB2INST1 V 2023-01-14-17.14.29.983567
VEMP DB2INST1 V 2023-01-14-17.14.29.992888
VEMPDPT1 DB2INST1 V 2023-01-14-17.14.30.009309
VEMPLP DB2INST1 V 2023-01-14-17.14.30.046463
VEMPPROJACT DB2INST1 V 2023-01-14-17.14.30.004078
VFORPLA DB2INST1 V 2023-01-14-17.14.30.032327
VHDEPT DB2INST1 V 2023-01-14-17.14.29.990353
VPHONE DB2INST1 V 2023-01-14-17.14.30.041997
VPROJ DB2INST1 V 2023-01-14-17.14.29.996689
VPROJACT DB2INST1 V 2023-01-14-17.14.30.001257
VPROJRE1 DB2INST1 V 2023-01-14-17.14.30.018638
VPSTRDE1 DB2INST1 V 2023-01-14-17.14.30.023670
VPSTRDE2 DB2INST1 V 2023-01-14-17.14.30.028481
VSTAFAC1 DB2INST1 V 2023-01-14-17.14.30.035293
VSTAFAC2 DB2INST1 V 2023-01-14-17.14.30.038206
48 record(s) selected.
- sql语句加分号出错(不论大小写,都不要加分号)
db2 => list tables;
SQL0104N An unexpected token "tables;" was found following "LIST". Expected
tokens may include: "ACTIVE". SQLSTATE=42601
- 查询样例库sample下表staff的数据
db2 => select * from staff limit 2;
ID NAME DEPT JOB YEARS SALARY COMM
------ --------- ------ ----- ------ --------- ---------
10 Sanders 20 Mgr 7 98357.50 -
20 Pernal 20 Sales 8 78171.25 612.45
2 record(s) selected.
- 创建表、插入数据
db2 => CREATE TABLE STUDENT(ID int ,NAME varchar(20));
DB20000I The SQL command completed successfully.
db2 => INSERT INTO STUDENT VALUES(11, 'lisi');
DB20000I The SQL command completed successfully.
db2 => commit;
DB20000I The SQL command completed successfully.
表STUDENT数据
5. DataX与DB2导入导入案例
DataX官网并没有DB2专门的读写教程,但是有
通用RDBMS(支持所有关系型数据库)
的读写教程,而DB2就属于通用RDBMS,如下图所示:
官网关系型数据库读写链接地址如下:
https://github.com/alibaba/DataX/blob/master/rdbmsreader/doc/rdbmsreader.md
https://github.com/alibaba/DataX/blob/master/rdbmswriter/doc/rdbmswriter.md
DB2的SAMPLE数据库实例下的STUDENT表数据
5.1 注册 db2 驱动
DataX 暂时没有独立插件支持 db2,需要使用通用的 rdbmsreader 或 rdbmswriter。
rdbmswriter如何增加新的数据库支持:
- 进入rdbmsreader对应目录,这里{DATAX_HOME}/plugin/reader/rdbmsreader`
- 在rdbmsreader插件目录下有plugin.json配置文件,在此文件中注册您具体的数据库驱动,具体放在drivers数组中。rdbmsreader插件在任务执行时会动态选择合适的数据库驱动连接数据库。
- 注册 reader 的 db2 驱动
[whybigdata@node01 datax]$ vim /opt/module/datax/plugin/reader/rdbmsreader/plugin.json
#在 drivers 里添加 db2 的驱动类com.ibm.db2.jcc.DB2Driver
"drivers":["dm.jdbc.driver.DmDriver", "com.sybase.jdbc3.jdbc.SybDriver", "com.edb.Driver","com.ibm.db2.jcc.DB2Driver"]
- 注册 writer 的 db2 驱动
[whybigdata@node01 datax]$ vim /opt/module/datax/plugin/writer/rdbmswriter/plugin.json
#在 drivers 里添加 db2 的驱动类com.ibm.db2.jcc.DB2Driver
"drivers":["dm.jdbc.driver.DmDriver", "com.sybase.jdbc3.jdbc.SybDriver", "com.edb.Driver","com.ibm.db2.jcc.DB2Driver"]
- 在DataX中db2相关依赖包(本人使用的版本中
db2jcc4.jar
是2017年1月14日的)
[whybigdata@node01 libs]$ pwd
/opt/module/datax/plugin/reader/rdbmsreader/libs
[whybigdata@node01 libs]$ ll | grep db2
-rwxr-xr-x 1 whybigdata whybigdata 3528544 1月 14 19:28 db2jcc4.jar
[whybigdata@node01 libs]$ pwd
/opt/module/datax/plugin/writer/rdbmswriter/libs
[whybigdata@node01 libs]$ ll | grep db2
-rwxr-xr-x 1 whybigdata whybigdata 3528544 1月 14 19:37 db2jcc4.jar
Note:如若下方案例从DB2导出不成功,在DB2连接正常且json文件无误的情况下,请更换更新的
db2jcc4.jar
包
5.2 读取 DB2 的数据导入到 HDFS
编写配置文件:进入DataX按照目录
[whybigdata@node01 datax]$ vim job/db2-2-hdfs.json
- 文件内容如下
{
"job": {
"content": [
{
"reader": {
"name": "rdbmsreader",
"parameter": {
"column": [
"ID",
"NAME"
],
"connection": [
{
"jdbcUrl": [
"jdbc:db2://node02:50000/SAMPLE"
],
"table": [
"STUDENT"
]
}
],
"username": "db2inst1",
"password": "123456"
}
},
"writer": {
"name": "hdfswriter",
"parameter": {
"column": [
{
"name": "id",
"type": "int"
},
{
"name": "name",
"type": "string"
}
],
"defaultFS": "hdfs://node01:8020",
"fieldDelimiter": "-",
"fileName": "db2.txt",
"fileType": "text",
"path": "/datax-out",
"writeMode": "append"
}
}
}
],
"setting": {
"speed": {
"channel": "1"
}
}
}
}
执行
[whybigdata@node01 datax]$ bin/datax.py job/db2-2-hdfs.json
最终结果:
5.3 读取 DB2 的数据导入 MySQL
编写配置文件:进入DataX按照目录
[whybigdata@node01 datax]$ vim job/db2-2-mysql.json
- 文件内容如下
{
"job": {
"content": [
{
"reader": {
"name": "rdbmsreader",
"parameter": {
"column": [
"ID",
"NAME"
],
"connection": [
{
"jdbcUrl": [
"jdbc:db2://node02:50000/SAMPLE"
],
"table": [
"STUDENT"
]
}
],
"username": "db2inst1",
"password": "123456"
}
},
"writer": {
"name": "mysqlwriter",
"parameter": {
"column": ["*"],
"connection": [
{
"jdbcUrl": "jdbc:mysql://node01:3306/datax",
"table": ["student"]
}
],
"password": "123456",
"username": "root",
"writeMode": "insert"
}
}
}
],
"setting": {
"speed": {
"channel": "1"
}
}
}
}
执行
[whybigdata@node01 datax]$ bin/datax.py job/db2-2-mysql.json
最终结果:
- 导入MySQL前:
- 导入MySQL后:
结束!
- 点赞
- 收藏
- 关注作者
评论(0)