【Maxwell】使用maxwell+kafka+python做binlog增量解析消費

小亮520cl發表於2018-05-14
1.maxwell基礎
參考

這裡寫下我的基礎配置
  1. [root@ip-10-1-2-175 maxwell-1.12.0]# more config.properties

  2. log_level=info
  3. kafka.bootstrap.servers=localhost:9092
  4. producer=kafka
  5. host=10.1.2.175
  6. user=dbaadmin
  7. password=
  8. schema_database=maxwell
  9. kafka.compression.type=snappy
  10. kafka.retries=0
  11. kafka.acks=1
  12. kinesis_stream=maxwell
  13. sqs_queue_uri=aws_sqs_queue_uri

  1. producer=kafka   ###生產者選擇kafka
  2. kafka.bootstrap.servers=10.1.2.175:9092
  3. kafka_topic=test    ####主題

  4. output_ddl = true   ####ddl輸出到 maxwell_ddl topic
    ddl_kafka_topic=maxwell_ddl

  5. producer_partition_by=database

2.python 消費者虛擬碼
  1. import time, json
  2. from pykafka import KafkaClient
  3. client = KafkaClient(hosts="10.1.2.175:9092")
  4. topic = client.topics['test']
  5. balanced_consumer = topic.get_balanced_consumer(consumer_group='goods_group',auto_commit_enable=True,zookeeper_connect='localhost:2181')

  6. for message in balanced_consumer:
  7.     print message.offset, message.value


啟動
[root@ip-10-1-2-175 sh]# python cus.py 



3.啟動maxwel
  1. [root@ip-10-1-2-175 maxwell-1.12.0]# ./bin/maxwell

4.模擬資料庫操作
  1. mysql> insert into testwell(name) values ('fuck');
  2. Query OK, 1 row affected (0.00 sec)

  3. mysql> insert into testwell(name) values ('fuck');
  4. Query OK, 1 row affected (0.00 sec)

  5. mysql> insert into testwell(name) values ('fuck');
  6. Query OK, 1 row affected (0.00 sec)

  7. mysql>


5.檢視消費者
  1. [root@ip-10-1-2-175 sh]# python cus.py
  2. 67 {"database":"test","table":"testwell","type":"insert","ts":1526290515,"xid":208818,"commit":true,"data":{"id":3050,"name":"fuck"}}
  3. 68 {"database":"test","table":"testwell","type":"insert","ts":1526290515,"xid":208819,"commit":true,"data":{"id":3051,"name":"fuck"}}
  4. 69 {"database":"test","table":"testwell","type":"insert","ts":1526290517,"xid":208820,"commit":true,"data":{"id":3052,"name":"fuck"}}
  5. 70 {"database":"test","table":"testwell","type":"insert","ts":1526290519,"xid":208822,"commit":true,"data":{"id":3053,"name":"fuck"}}


6.根據上面的思路可以寫出增量同步的虛擬碼
  1. [root@ip-10-1-2-175 sh]#
  2. import time, json
  3. from pykafka import KafkaClient
  4. client = KafkaClient(hosts="10.1.2.175:9092")
  5. topic = client.topics['test']
  6. balanced_consumer = topic.get_balanced_consumer(consumer_group='goods_group',auto_commit_enable=True,zookeeper_connect='localhost:2181')

  7. for message in balanced_consumer:
  8.     print message.offset, message.value
  9.    # 對資料庫進行操作
  10.     if message.value ['type'] == 'insert':
  11.       mysql_insert()
  12.     elif  message.value['type'] == 'update':
  13.       mysql_update()
  14.     elif  message.value['type'] == 'delete':
  15.       mysql_delete()
  16.     else:
  17.       order_option()


其它:
https://mp.weixin.qq.com/s/aGqwWNd6Q2Gm2jilxXS0RA

Kafka Connect 如何實現同步RDS binlog資料?

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2154470/,如需轉載,請註明出處,否則將追究法律責任。

相關文章