什么網(wǎng)站可以找手工活做廣州營銷網(wǎng)站建設(shè)靠譜
1 Maxwell輸出格式
- database:變更數(shù)據(jù)所屬的數(shù)據(jù)庫
- table:變更數(shù)據(jù)所屬的表
- type:數(shù)據(jù)變更類型
- ts:數(shù)據(jù)變更發(fā)生的時間
- xid:事務(wù)id
- commit:事務(wù)提交標(biāo)志,可用于重新組裝事務(wù)
- data:對于insert類型,表示插入的數(shù)據(jù);對于update類型,表示修改之后的數(shù)據(jù);對于delete類型,表示刪除的數(shù)據(jù)
- old:對于update類習(xí)慣,表示修改之前的數(shù)據(jù),只包含變更字段
2.Maxwell部署
2.1 安裝Maxwell?
(1)解壓安裝包
tar -zxvf maxwell-1.29.2.tar.gz -C /opt/module
(2)修改解壓后的maxwell名字
mv maxwell-1.29.2/ maxwell
2.2 配置MySQL
2.2.1 判斷MySQL是否已經(jīng)開啟binlog
MySQL服務(wù)器的Binlog默認(rèn)是未開啟的,如需進行同步,需要先進行開啟
SHOW VARIABLES LIKE 'log_bin';
查看MySQL的binlog模式
show global variables like "binlog%";
開啟binlog日志
修改? MySQL? 的? my.cnf? 配置文件
一般默認(rèn)在? /etc/my.cnf? 下
#第一種方式:
#開啟binlog日志
log_bin=ON
#binlog日志的基本文件名
log_bin_basename=/var/lib/mysql/mysql-bin
#binlog文件的索引文件,管理所有binlog文件
log_bin_index=/var/lib/mysql/mysql-bin.index
#配置serverid
server-id=1
注:MySQL Binlog模式
Statement-based:基于語句,Binlog會記錄所有寫操作的SQL語句,包括insert、update、delete等。
優(yōu)點: 節(jié)省空間
缺點: 有可能造成數(shù)據(jù)不一致,例如insert語句中包含now()函數(shù)。
Row-based:基于行,Binlog會記錄每次寫操作后被操作行記錄的變化。
優(yōu)點:保持?jǐn)?shù)據(jù)的絕對一致性。
缺點:占用較大空間。
mixed:混合模式,默認(rèn)是Statement-based,如果SQL語句可能導(dǎo)致數(shù)據(jù)不一致,就自動切換到Row-based。
Maxwell要求Binlog采用Row-based模式。
修改完配置后,重啟MySQL。
重啟MySQL服務(wù)
systemctl restart mysqld
執(zhí)行? SHOW VARIABLES LIKE 'log_bin';? ?value值為? ON? 即可。
2.2.2 創(chuàng)建Maxwell所需數(shù)據(jù)庫和用戶
Maxwell需要在MySQL中存儲其運行過程中的所需的一些數(shù)據(jù),包括binlog同步的斷點位置(Maxwell支持?jǐn)帱c續(xù)傳)等等,故需要在MySQL為Maxwell創(chuàng)建數(shù)據(jù)庫及用戶。
(1)創(chuàng)建數(shù)據(jù)庫
msyql> CREATE DATABASE maxwell;
(2)更改MySQL數(shù)據(jù)庫密碼級別(可選操作)
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=4;
(3)創(chuàng)建Maxwell用戶并賦予必要權(quán)限
mysql> CREATE USER 'maxwell'@'%' IDENTIFIED BY 'abc';
mysql> GRANT ALL ON maxwell.* TO 'maxwell'@'%';
mysql> GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'maxwell'@'%';
2.3 配置Maxwell
(1)修改Maxwell配置文件
cd /opt/module/maxwell
cp config.properties.example config.properties
vim config.properties#Maxwell數(shù)據(jù)發(fā)送目的地,可選配置有stdout|file|kafka|kinesis|pubsub|sqs|rabbitmq|redis
producer=kafka
#目標(biāo)Kafka集群地址
kafka.bootstrap.servers=bigdata1:9092,bigdata2:9092,bigdata3:9092
#目標(biāo)Kafka topic,可靜態(tài)配置,例如:maxwell,也可動態(tài)配置,例如:%{database}_%{table}
kafka_topic=maxwell#MySQL相關(guān)配置
host=bigdata1
user=maxwell
password=123456
jdbc_options=useSSL=false&serverTimezone=Asia/Shanghai
3.Maxwell使用
3.1啟動Kafka集群
若Maxwell發(fā)送數(shù)據(jù)的目的地為Kafka集群,則需先確保Kafka集群為啟動狀態(tài)。
3.2 Maxwell啟停
(1)啟動
/opt/module/maxwell/bin/maxwell --config /opt/module/maxwell/config.properties --daemon
(2)停止Maxwell
ps -ef | grep maxwell | grep -v grep | grep maxwell | awk '{print $2}' | xargs kill -9
(3)Maxwell啟停腳本
創(chuàng)建啟停腳本
????????vim mxw.sh
內(nèi)容
#!/bin/bashMAXWELL_HOME=/opt/module/maxwellstatus_maxwell(){result=`ps -ef | grep com.zendesk.maxwell.Maxwell | grep -v grep | wc -l`return $result
}start_maxwell(){status_maxwellif [[ $? -lt 1 ]]; thenecho "啟動Maxwell"$MAXWELL_HOME/bin/maxwell --config $MAXWELL_HOME/config.properties --daemonelseecho "Maxwell正在運行"fi
}stop_maxwell(){status_maxwellif [[ $? -gt 0 ]]; thenecho "停止Maxwell"ps -ef | grep com.zendesk.maxwell.Maxwell | grep -v grep | awk '{print $2}' | xargs kill -9elseecho "Maxwell未在運行"fi
}case $1 instart )start_maxwell;;stop )stop_maxwell;;restart )stop_maxwellstart_maxwell;;
esac
賦予權(quán)限
chmod 777 mxw.sh
3.3 增量數(shù)據(jù)同步
(1)啟動Kafka消費者
bin/kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --topic maxwell
(2)模擬生成數(shù)據(jù)
java -jar gmall2020-mock-db-2021-01-22.jar
(3)觀察Kafka消費者
{"database":"gmall","table":"comment_info","type":"insert","ts":1634023510,"xid":1653373,"xoffset":11998,"data":{"id":1447825655672463369,"user_id":289,"nick_name":null,"head_img":null,"sku_id":11,"spu_id":3,"order_id":18440,"appraise":"1204","comment_txt":"評論內(nèi)容:12897688728191593794966121429786132276125164551411","create_time":"2020-06-16 15:25:09","operate_time":null}}
{"database":"gmall","table":"comment_info","type":"insert","ts":1634023510,"xid":1653373,"xoffset":11999,"data":{"id":1447825655672463370,"user_id":774,"nick_name":null,"head_img":null,"sku_id":25,"spu_id":8,"order_id":18441,"appraise":"1204","comment_txt":"評論內(nèi)容:67552221621263422568447438734865327666683661982185","create_time":"2020-06-16 15:25:09","operate_time":null}}
3.4 歷史數(shù)據(jù)全量同步
可能需要使用到MySQL數(shù)據(jù)庫中從歷史至今的一個完整的數(shù)據(jù)集。這就需要我們在進行增量同步之前,先進行一次歷史數(shù)據(jù)的全量同步。這樣就能保證得到一個完整的數(shù)據(jù)集。
3.4.1Maxwell-bootstrap
歷史數(shù)據(jù)的全量同步的命令
/opt/module/maxwell/bin/maxwell-bootstrap --database gmall --table user_info --config /opt/module/maxwell/config.properties
3.4.2 bootstrap數(shù)據(jù)格式
采用bootsrtap方式同步輸出數(shù)據(jù)格式
{"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": {}
}
第一條type為bootstrap-start和最后一條type為bootstrap-complete的數(shù)據(jù),是bootstrap開始和結(jié)束的標(biāo)志,不包含數(shù)據(jù),中間的type為bootstrap-insert的數(shù)據(jù)才包含數(shù)據(jù)。
一次bootstrap輸出的所有記錄的ts都相同,為bootstrap開始的時間。