MaxWell 概述

MaxWell概述

將mysql的binlog以json的形式輸出到kafka,它的常見用例包括ETL,緩存建立/過期,指標收集,搜索索引和服務間通信

json示例:

 

mysql> update test.maxwell set daemon = 'firebus!  firebus!' where id = 1;
  maxwell: {
    "database": "test",
    "table": "maxwell",
    "type": "update",
    "ts": 1449786341,
    "xid": 940786,
    "commit": true,
    "data": {"id":1, "daemon": "Firebus!  Firebus!"},
    "old":  {"daemon": "Stanislaw Lem"}
  }

下載Maxwell

brew install maxwell

配置Mysql

$ vi my.cnf
[mysqld]
server_id=1
log-bin=master
binlog_format=row

權限

權限: Maxwell需要權限來在schema_database選項指定的數據庫中存儲狀態(默認:maxwell)

mysql> CREATE USER 'maxwell'@'localhost' IDENTIFIED BY 'XXXXXX';
mysql> GRANT ALL ON maxwell.* TO 'maxwell'@'localhost';
mysql> GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'maxwell'@'localhost';

運行maxwell

To stdout

bin/maxwell --user='maxwell' --password='xxx' --host='127.0.0.1' --producer=stdout

To Kafka

bin/maxwell --user='maxwell' --password='xxx' --host='127.0.0.1' --producer=kafka --kafka.bootstrap.servers=localhost:9092 --kafka_topic=maxwell(--kafka_topic=namespace_%{database}_%{table})

 啓動GTID模式的配置

$ vi my.cnf

[mysqld]
server_id=1
log-bin=master
binlog_format=row
gtid-mode=ON
log-slave-updates=ON
enforce-gtid-consistency=true

當處於GTID模式時,Maxwell將在進行母版更改後透明地選擇一個新的複製位置。請注意,您仍然必須將maxwell重新指向新的母版。目前,Maxwell對GTID的支持被認爲是beta質量的。值得注意的是,Maxwell無法透明地從傳統複製方案升級到GTID複製方案。當前,當您啓用gtid模式時,Maxwell將從“主機所在的位置”重新捕獲架構和GTID位置

運行多個maxwell實例來達到不同表對應不同topic的功能,Maxwell根據mysql行的JSON格式的主鍵爲其Kafka消息生成鍵

kafka分區,根據hashcode進行分區存儲,默認的是對數據庫進行hashcode來進行分區,還可以對數據庫,表,primary_key,transaction_id,列等進行分區。

MaxWell過濾

basic Filter

eg1:可以將Maxwell配置爲從特定表中過濾掉更新,這由 --filter 命令行標誌控制

--filter = 'exclude: foodb.*, include: foodb.tbl, include: foodb./table_\d+/'

This example tells Maxwell to suppress all updates that happen on foodb except for updates to tbl and any table in foodb matching the regexp /table \d+/

eg2:Filter options are evaluated in the order specified, so in this example we suppress everything except updates in the db1 database.

--filter = 'exclude: *.*, include: db1.*'

Column Filter

Maxwell can also include/exclude based on column values:

--filter = 'exclude: db.tbl.col = reject'

過濾掉db.tbl.col的值爲‘reject‘的更新

(will reject any row in db.tbl that contains col and where the stringified value of "col" is "reject". Column filters are ignored if the specified column is not present, so --filter = 'exclude: *.*.col_a = *' will exclude updates to any table that contains col_a, but include every other table.)

BlackListing

--filter = 'blacklist: bad_db.*'

JavaSript Filter

--javascript FILE
function process_row(row) {
    if ( row.database == "test" && row.table == "bar" ) {
        var username = row.data.get("username");
        if ( username == "osheroff" )
            row.suppress();

        row.data.put("username", username.toUpperCase());
    }
}

 

加密

--encrypt=[none|data|all]

none : 不加密; data: 對值加密;all: 對所有數據加密

DBZ和MaxWell對比

  • dbz可以讀取數據庫的快照,而maxwell通過bootstrap來同步全部數據
  • dbz可以使用avro格式存儲,對於schema的變動方便更改,而Maxwell只支持json
  • dbz通過RestFul API來啓動多個connector並可以指定執行的task
  • dbz有白名單和黑名單的過濾,並且有豐富的SMT的數據轉換過濾
  • dbz每個表對應一個topic,按着key(id)進行分區,保證數據的有序性;
  • maxwell相對來說更輕量級,學習難度低
  • maxwell的數據傳輸量比較小
  • maxwell支持多種producer,而dbz是對應kafka\

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章