0


业务数据采集模块

1.采集通道

业务数据通道

2. 采集工具介绍

2.1 Maxwell 概述

Maxwell 是由美国Zendesk公司开源,用Java编写的MySQL变更数据抓取软件。它会实时监控Mysql数据库的数据变更操作(包括insert、update、delete),并将变更数据以 JSON 格式发送给 Kafka、Kinesi等流数据处理平台。官网地址:Maxwell's Daemon

2.2 Maxwell输出数据格式

请注意: Maxwell输出的json字段说明:

2.3 Maxwell原理

Maxwell的工作原理是实时读取MySQL数据库的二进制日志(Binlog),从中获取变更数据,再将变更数据以JSON格式发送至Kafka等流处理平台。

2.4 MySQL二进制日志

二进制日志(Binlog)是MySQL服务端非常重要的一种日志,它会保存MySQL数据库的所有数据变更记录。Binlog的主要作用包括主从复制和数据恢复。Maxwell的工作原理和主从复制密切相关。

2.5 MySQL主从复制

MySQL的主从复制,就是用来建立一个和主数据库完全一样的数据库环境,这个数据库称为从数据库

1****)主从复制的应用场景如下:

(1)做数据库的热备:主数据库服务器故障后,可切换到从数据库继续工作。

(2)读写分离:主数据库只负责业务数据的写入操作,而多个从数据库只负责业务数据的查询工作,在读多写少场景下,可以提高数据库工作效率。

2****)主从复制的工作原理如下:

(1)Master主库将数据变更记录,写到二进制日志(binary log)中

(2)Slave从库向mysql master发送dump协议,将master主库的binary log events拷贝到它的中继日志(relay log)

(3)Slave从库读取并回放中继日志中的事件,将改变的数据同步到自己的数据库。

2.6 Maxwell工作概述原理

将自己伪装成slave,并遵循MySQL主从复制的协议,从master同步数据。

2.7 Maxwell部署

2.7.1 下载安装包

(1) 地址: https://github.com/zendesk/maxwell/releases/download/v1.29.2/maxwell-1.29.2.tar.gz

(2)将安装包上传到hadoop102节点的/opt/software目录

[maxwell@hadoop102 opt]$ cd software/
[maxwell@hadoop102 software]$ ls -ltr
total 929868
-rwxrwxr-x. 1 root root 195013152 Oct 7 17:30 jdk-8u212-linux-x64.tar.gz
-rwxrwxr-x. 1 root root 338075860 Oct 7 17:30 hadoop-3.1.3.tar.gz
-rw-rw-r--. 1 maxwell maxwell 9311744 Mar 23 14:28 apache-zookeeper-3.5.7-bin.tar.gz
-rw-rw-r--. 1 maxwell maxwell 86486610 Mar 23 15:08 kafka_2.12-3.0.0.tgz
-rw-rw-r--. 1 maxwell maxwell 67938106 Mar 23 15:34 apache-flume-1.9.0-bin.tar.gz
-rw-rw-r--. 1 maxwell maxwell 277604 Mar 25 15:50 01_mysql-community-common-5.7.16-1.el7.x86_64.rpm
-rw-rw-r--. 1 maxwell maxwell 2112700 Mar 25 15:50 03_mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm
-rw-rw-r--. 1 maxwell maxwell 2237116 Mar 25 15:50 02_mysql-community-libs-5.7.16-1.el7.x86_64.rpm
-rw-rw-r--. 1 maxwell maxwell 25034716 Mar 25 15:50 04_mysql-community-client-5.7.16-1.el7.x86_64.rpm
-rw-rw-r--. 1 maxwell maxwell 872303 Mar 25 15:50 mysql-connector-java-5.1.27-bin.jar
-rw-rw-r--. 1 maxwell maxwell 159295840 Mar 25 15:50 05_mysql-community-server-5.7.16-1.el7.x86_64.rpm
-rw-rw-r--. 1 maxwell maxwell 65510398 Mar 27 09:46 maxwell-1.29.2.tar.gz
[maxwell@hadoop102 software]$

(3) 将安装包解压到/opt/module

[maxwell@hadoop102 software]$ 
[maxwell@hadoop102 software]$ tar -zxvf maxwell-1.29.2.tar.gz -C /opt/module/
maxwell-1.29.2/log4j2.xml
maxwell-1.29.2/config.md
maxwell-1.29.2/quickstart.md

(4) 修改名称

[maxwell@hadoop102 software]$ cd /opt/module/
[maxwell@hadoop102 module]$ ls -ltr
total 0
drwxr-xr-x.  7 maxwell maxwell 245 Apr  2  2019 jdk1.8.0_212
drwxrwxr-x.  3 maxwell maxwell 117 Mar 22 17:49 applog
drwxrwxr-x.  3 maxwell maxwell  18 Mar 22 23:20 hadoop-3.1.3
drwxr-xr-x. 13 maxwell maxwell 204 Mar 23 11:02 hadoop
drwxrwxr-x.  8 maxwell maxwell 160 Mar 23 14:50 zookeeper
drwxr-xr-x.  9 maxwell maxwell 130 Mar 23 15:23 kafka
drwxrwxr-x.  9 maxwell maxwell 239 Mar 25 13:30 flume
drwxrwxr-x.  2 maxwell maxwell  76 Mar 25 21:58 db_log
drwxrwxr-x.  4 maxwell maxwell 200 Mar 27 09:49 maxwell-1.29.2
[maxwell@hadoop102 module]$ mv maxwell-1.29.2/ maxwell
[maxwell@hadoop102 module]$ ls -ltr
total 0
drwxr-xr-x.  7 maxwell maxwell 245 Apr  2  2019 jdk1.8.0_212
drwxrwxr-x.  3 maxwell maxwell 117 Mar 22 17:49 applog
drwxrwxr-x.  3 maxwell maxwell  18 Mar 22 23:20 hadoop-3.1.3
drwxr-xr-x. 13 maxwell maxwell 204 Mar 23 11:02 hadoop
drwxrwxr-x.  8 maxwell maxwell 160 Mar 23 14:50 zookeeper
drwxr-xr-x.  9 maxwell maxwell 130 Mar 23 15:23 kafka
drwxrwxr-x.  9 maxwell maxwell 239 Mar 25 13:30 flume
drwxrwxr-x.  2 maxwell maxwell  76 Mar 25 21:58 db_log
drwxrwxr-x.  4 maxwell maxwell 200 Mar 27 09:49 maxwell
[maxwell@hadoop102 module]$ 

2.8 配置MySQL

2.8.1 启用MySQL Binlog

MySQL服务器的Binlog默认是未开启的,如需进行同步,需要先进行开启。

1)修改MySQL配置文件/etc/my.cnf

[maxwell@hadoop102 ~]$ sudo vim /etc/my.cnf

2****)增加如下配置

[mysqld]

#数据库id

server-id = 1

#启动binlog,该参数的值会作为binlog的文件名

log-bin=mysql-bin

#binlog类型,maxwell要求为row类型

binlog_format=row

#启用binlog的数据库,需根据实际情况作出修改

binlog-do-db=gmall

注:MySQL Binlog****模式

Statement-based:基于语句,Binlog会记录所有写操作的SQL语句,包括insert、update、

delete等。

优点: 节省空间

缺点: 有可能造成数据不一致,例如insert语句中包含now()函数。

Row-based****:基于行,Binlog会记录每次写操作后被操作行记录的变化。

优点:保持数据的绝对一致性。

缺点:占用较大空间。

mixed****:混合模式,默认是Statement-based,如果SQL语句可能导致数据不一致,就自动切换到Row-based。

Maxwell要求Binlog采用Row-based模式。

3)重启MySQL服务

[maxwell@hadoop102 ~]$ 
[maxwell@hadoop102 ~]$ sudo systemctl restart mysqld
[maxwell@hadoop102 ~]$ 
[maxwell@hadoop102 ~]$ 
[maxwell@hadoop102 ~]$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status
    -> ;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 | gmall        |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> 

2.9 创建Maxwell所需数据库和用户

Maxwell需要在MySQL中存储其运行过程中的所需的一些数据,包括binlog同步的断点位置(Maxwell支持断点续传)等等,故需要在MySQL为Maxwell创建数据库及用户。

1)创建数据库

[maxwell@hadoop102 ~]$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      154 | gmall        |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> 
mysql> CREATE DATABASE maxwell;
Query OK, 1 row affected (0.00 sec)

mysql> 

2)调整MySQL数据库密码级别

mysql> 
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=4;
Query OK, 0 rows affected (0.00 sec)

mysql> 

3)创建Maxwell用户并赋予其必要权限

mysql> 
mysql> CREATE USER 'maxwell'@'%' IDENTIFIED BY 'maxwell';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL ON maxwell.* TO 'maxwell'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'maxwell'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> 

2.10 配置Maxwell

1)修改Maxwell配置文件名称

[maxwell@hadoop102 ~]$ cd /opt/module/maxwell
[maxwell@hadoop102 maxwell]$ ls -ltr
total 80
-rw-rw-r--. 1 maxwell maxwell   470 Sep 17  2021 log4j2.xml
-rw-rw-r--. 1 maxwell maxwell   548 Sep 17  2021 LICENSE
-rw-rw-r--. 1 maxwell maxwell 10259 Sep 17  2021 kinesis-producer-library.properties.example
-rw-rw-r--. 1 maxwell maxwell 11970 Sep 17  2021 config.properties.example
-rw-rw-r--. 1 maxwell maxwell  1429 Sep 17  2021 README.md
-rw-rw-r--. 1 maxwell maxwell  3328 Sep 17  2021 quickstart.md
-rw-rw-r--. 1 maxwell maxwell 25133 Sep 17  2021 config.md
drwxrwxr-x. 3 maxwell maxwell  8192 Sep 17  2021 lib
drwxrwxr-x. 2 maxwell maxwell    93 Mar 27 09:49 bin
[maxwell@hadoop102 maxwell]$ cp config.properties.example config.properties
[maxwell@hadoop102 maxwell]$ 

2)修改Maxwell配置文件

[maxwell@hadoop102 maxwell]$ cd /opt/module/maxwell/
[maxwell@hadoop102 maxwell]$ cp config.properties.example config.properties
[maxwell@hadoop102 maxwell]$
[maxwell@hadoop102 maxwell]$ vim config.properties

[maxwell@hadoop102 maxwell]$ cat config.properties

tl;dr config

log_level=info

#Maxwell数据发送目的地,可选配置有stdout|file|kafka|kinesis|pubsub|sqs|rabbitmq|redis
producer=kafka

#目标Kafka集群地址
kafka.bootstrap.servers=hadoop102:9092,hadoop103:9092

#目标Kafka topic,可静态配置,例如:maxwell,也可动态配置,例如:%{database}_%{table}
kafka_topic=maxwell

mysql login info

host=hadoop102
user=maxwell
password=maxwell
jdbc_options=useSSL=false&serverTimezone=Asia/Shanghai

*** general ***

choose where to produce data to. stdout|file|kafka|kinesis|pubsub|sqs|rabbitmq|redis

#producer=kafka

set the log level. note that you can configure things further in log4j2.xml

#log_level=DEBUG # [DEBUG, INFO, WARN, ERROR]

if set, maxwell will look up the scoped environment variables, strip off the prefix and inject the configs

#env_config_prefix=MAXWELL_

2.11 Maxwell使用

2.11.1 Maxwell 启停

1****)启动Maxwell

[maxwell@hadoop102 ~]$ cd bin
[maxwell@hadoop102 bin]$ ls -ltr
total 36
-rwxrwxr-x. 1 maxwell maxwell  565 Oct  7 18:33 xsync
-rwxrwxr-x. 1 maxwell maxwell 1023 Oct  8 10:26 myhadoop.sh
-rwxrwxr-x. 1 maxwell maxwell  122 Oct  8 10:27 jpsall
-rwxrwxrwx. 1 maxwell maxwell  195 Mar 22 17:48 lg.sh
-rwxrwxrwx. 1 maxwell maxwell  130 Mar 22 17:53 xcall
-rwxrwxrwx. 1 maxwell maxwell 1092 Mar 23 11:11 hdp.sh
-rwxrwxrwx. 1 maxwell maxwell  565 Mar 23 14:55 zk.sh
-rwxrwxrwx. 1 maxwell maxwell  442 Mar 23 15:26 kf.sh
-rwxrwxrwx. 1 maxwell maxwell  574 Mar 25 13:29 f1.sh
[maxwell@hadoop102 bin]$ zk.sh start
---------- zookeeper hadoop102 启动 ------------
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
---------- zookeeper hadoop103 启动 ------------
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
---------- zookeeper hadoop104 启动 ------------
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[maxwell@hadoop102 bin]$ kf.sh start
 --------启动 hadoop102 Kafka-------
 --------启动 hadoop103 Kafka-------
 --------启动 hadoop104 Kafka-------
[maxwell@hadoop102 bin]$ xcall jps
----------------- hadoop102 -----------------------
9490 Kafka
9562 Jps
9102 QuorumPeerMain
----------------- hadoop103 -----------------------
3746 Jps
3652 Kafka
3262 QuorumPeerMain
----------------- hadoop104 -----------------------
3920 Jps
3819 Kafka
3436 QuorumPeerMain
[maxwell@hadoop102 bin]$ /opt/module/maxwell/bin/maxwell --config /opt/module/maxwell/config.properties --daemon
Redirecting STDOUT to /opt/module/maxwell/bin/../logs/MaxwellDaemon.out
Using kafka version: 1.0.0
[maxwell@hadoop102 bin]$ jps
9490 Kafka
9636 Maxwell
9102 QuorumPeerMain
9679 Jps
[maxwell@hadoop102 bin]$ 

2)停止Maxwell

[maxwell@hadoop102 ~]$ ps -ef | grep maxwell | grep -v grep | grep maxwell | awk '{print $2}' | xargs kill -9

3)Maxwell启停脚本

[maxwell@hadoop102 bin]$ mxw.sh start
启动Maxwell
Redirecting STDOUT to /opt/module/maxwell/bin/../logs/MaxwellDaemon.out
Using kafka version: 1.0.0
[maxwell@hadoop102 bin]$ jps
10336 Jps
9490 Kafka
10293 Maxwell
9102 QuorumPeerMain
[maxwell@hadoop102 bin]$ cat mxw.sh 
#!/bin/bash

MAXWELL_HOME=/opt/module/maxwell

status_maxwell(){
    result=`ps -ef | grep com.zendesk.maxwell.Maxwell | grep -v grep | wc -l`
    return $result
}

start_maxwell(){
    status_maxwell
    if [[ $? -lt 1 ]]; then
        echo "启动Maxwell"
        $MAXWELL_HOME/bin/maxwell --config $MAXWELL_HOME/config.properties --daemon
    else
        echo "Maxwell正在运行"
    fi
}

stop_maxwell(){
    status_maxwell
    if [[ $? -gt 0 ]]; then
        echo "停止Maxwell"
        ps -ef | grep com.zendesk.maxwell.Maxwell | grep -v grep | awk '{print $2}' | xargs kill -9
    else
        echo "Maxwell未在运行"
    fi
}

case $1 in
    start )
        start_maxwell
    ;;
    stop )
        stop_maxwell
    ;;
    restart )
       stop_maxwell
       start_maxwell
    ;;
esac

[maxwell@hadoop102 bin]$ 

2.12 增量数据同步

1)启动Kafka消费者

[maxwell@hadoop102 kafka]$ bin/kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --topic maxwell

2****)模拟生成数据

[maxwell@hadoop102 db_log]$ java -jar gmall2020-mock-db-2021-01-22.jar

3)观察Kafka消费者

2.13 历史数据全量同步

2.12,我们已经实现了使用Maxwell实时增量同步MySQL变更数据的功能。但有时只有增量数据是不够的,我们可能需要使用到MySQL数据库中从历史至今的一个完整的数据集。这就需要我们在进行增量同步之前,先进行一次历史数据的全量同步。这样就能保证得到一个完整的数据集。

2.13.1 Maxwell-bootstrap

Maxwell提供了bootstrap功能来进行历史数据的全量同步,命令如下:

[maxwell@hadoop102 maxwell]$ 
[maxwell@hadoop102 maxwell]$ /opt/module/maxwell/bin/maxwell-bootstrap --database gmall --table user_info --config /opt/module/maxwell/config.properties
connecting to jdbc:mysql://hadoop102:3306/maxwell?allowPublicKeyRetrieval=true&connectTimeout=5000&serverTimezone=Asia%2FShanghai&zeroDateTimeBehavior=convertToNull&useSSL=false
[maxwell@hadoop102 maxwell]$ 

2.13.2 bootstrap 数据格式

采用bootstrap方式同步的输出数据格式如下:

{

"database": "fooDB",

"table": "barTable",

"type": "bootstrap-start",

"ts": 1450557744,

"data": {}

}

{

"database": "fooDB",

"table": "barTable",

"type": "bootstrap-insert",

"ts": 1450557744,

"data": {

    "txt": "hello"

}

}

{

"database": "fooDB",

"table": "barTable",

"type": "bootstrap-insert",

"ts": 1450557744,

"data": {

    "txt": "bootstrap!"

}

}

{

"database": "fooDB",

"table": "barTable",

"type": "bootstrap-complete",

"ts": 1450557744,

"data": {}

}

注意事项:

1)第一条type为bootstrap-start和最后一条type为bootstrap-complete的数据,是bootstrap开始和结束的标志,不包含数据,中间的type为bootstrap-insert的数据才包含数据。

2)一次bootstrap输出的所有记录的ts都相同,为bootstrap开始的时间。

3. 采集通道Maxwell配置

3.1 修改Maxwell配置文件 config.properties

[maxwell@hadoop102 maxwell]$ vim /opt/module/maxwell/config.properties

3.2 配置参数如下:

log_level=info

producer=kafka

kafka.bootstrap.servers=hadoop102:9092,hadoop103:9092

#kafka topic配置

kafka_topic=topic_db

mysql login info

host=hadoop102

user=maxwell

password=maxwell

jdbc_options=useSSL=false&serverTimezone=Asia/Shanghai

3.3 重新启动Maxwell

[maxwell@hadoop102 bin]$ mxw.sh restart
停止Maxwell
启动Maxwell
Redirecting STDOUT to /opt/module/maxwell/bin/../logs/MaxwellDaemon.out
Using kafka version: 1.0.0
[maxwell@hadoop102 bin]$

3.4 通道测试

3.4.1 启动Zookeeper以及Kafka集群

3.4.2 启动一个Kafka Console Consumer,消费topic_db数据

[maxwell@hadoop103 kafka]$ bin/kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --topic topic_db

3.4.3 生成模拟数据

[maxwell@hadoop102 bin]$ cd /opt/module/db_log/
[maxwell@hadoop102 db_log]$ java -jar gmall2020-mock-db-2021-11-14.jar 

3.4.4 观察Kafka消费者是否能消费到数据


本文转载自: https://blog.csdn.net/u011868279/article/details/129788694
版权归原作者 DB架构 所有, 如有侵权,请联系我们删除。

“业务数据采集模块”的评论:

还没有评论