|
| 1 | +import os |
| 2 | +import json |
| 3 | +import time |
| 4 | +import urlparse |
| 5 | +from datetime import datetime, date |
| 6 | +from decimal import Decimal |
| 7 | + |
| 8 | +from pymysqlreplication import BinLogStreamReader |
| 9 | +from pymysqlreplication.row_event import BINLOG |
| 10 | +from kafka import SimpleProducer, KafkaClient |
| 11 | +from kafka.common import LeaderNotAvailableError |
| 12 | + |
| 13 | + |
| 14 | +def json_serial(obj): |
| 15 | + """JSON serializer for objects not serializable by default json code""" |
| 16 | + |
| 17 | + if isinstance(obj, (datetime, date)): |
| 18 | + serial = obj.isoformat() |
| 19 | + return serial |
| 20 | + if isinstance(obj, Decimal): |
| 21 | + return float(obj) |
| 22 | + else: |
| 23 | + print "Type '{}' for '{}' not serializable".format(obj.__class__, obj) |
| 24 | + return None |
| 25 | + |
| 26 | +def build_message(binlog_evt): |
| 27 | + schema = {'table': getattr(binlog_evt, 'schema', '') + "." + getattr(binlog_evt, 'table', '')}; |
| 28 | + |
| 29 | + if binlog_evt.event_type == BINLOG.WRITE_ROWS_EVENT_V2: |
| 30 | + # Insert |
| 31 | + return {'event':'INSERT', 'headers':schema, 'data':binlog_evt.rows[0]['values']} |
| 32 | + |
| 33 | + elif binlog_evt.event_type == BINLOG.UPDATE_ROWS_EVENT_V2: |
| 34 | + # Update |
| 35 | + return {'event':'UPDATE', 'headers':schema, 'data':binlog_evt.rows[0]['after_values']} |
| 36 | + elif binlog_evt.event_type == BINLOG.DELETE_ROWS_EVENT_V2: |
| 37 | + # Delete |
| 38 | + return {'event':'DELETE', 'headers':schema, 'data':binlog_evt.rows[0]['values']} |
| 39 | + |
| 40 | + else: |
| 41 | + return None |
| 42 | + |
| 43 | + |
| 44 | +kafka = KafkaClient("localhost:9092") |
| 45 | + |
| 46 | +producer = SimpleProducer(kafka) |
| 47 | +producer.send_messages("test", "test msg") |
| 48 | + |
| 49 | +# To wait for acknowledgements |
| 50 | +# ACK_AFTER_LOCAL_WRITE : server will wait till the data is written to |
| 51 | +# a local log before sending response |
| 52 | +# ACK_AFTER_CLUSTER_COMMIT : server will block until the message is committed |
| 53 | +# by all in sync replicas before sending a response |
| 54 | +producer = SimpleProducer(kafka, async=False, |
| 55 | + req_acks=SimpleProducer.ACK_AFTER_LOCAL_WRITE, |
| 56 | + ack_timeout=2000) |
| 57 | + |
| 58 | + |
| 59 | +conf = urlparse.urlparse(os.environ['RDS_URL']) |
| 60 | +mysql_settings = {'host': conf.hostname, |
| 61 | + 'port': conf.port, |
| 62 | + 'user': conf.username, |
| 63 | + 'passwd': conf.password} |
| 64 | + |
| 65 | +# Connect to Mysql replication stream |
| 66 | +print "Connecting to Mysql at {}...".format(mysql_settings['host']) |
| 67 | +stream = BinLogStreamReader(connection_settings = mysql_settings, server_id=100, resume_stream=False, |
| 68 | + blocking=True) |
| 69 | +print "connected. Listening for changes..." |
| 70 | + |
| 71 | +for evt in stream: |
| 72 | + evt.dump() |
| 73 | + msg = build_message(evt) |
| 74 | + if msg: |
| 75 | + try: |
| 76 | + response = producer.send_messages(msg['headers']['table'], json.dumps(msg, default=json_serial)) |
| 77 | + except LeaderNotAvailableError: |
| 78 | + time.sleep(1) |
| 79 | + response = producer.send_messages(msg['headers']['table'], json.dumps(msg, default=json_serial)) |
| 80 | + # TODO: Test response.error |
| 81 | + # TODO: Store replication stream pos |
| 82 | + |
| 83 | +stream.close() |
0 commit comments