-
Notifications
You must be signed in to change notification settings - Fork 683
/
Copy pathread_event.py
86 lines (63 loc) · 2.26 KB
/
read_event.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import pymysql
from pymysqlreplication import BinLogStreamReader, gtid
from pymysqlreplication.event import GtidEvent, RotateEvent, MariadbGtidEvent, QueryEvent,MariadbAnnotateRowsEvent, MariadbBinLogCheckPointEvent
from pymysqlreplication.row_event import WriteRowsEvent, UpdateRowsEvent, DeleteRowsEvent
MARIADB_SETTINGS = {
"host": "127.0.0.1",
"port": 3306,
"user": "replication_user",
"passwd": "secret123passwd",
}
class MariaDbGTID:
def __init__(self, conn_config):
self.connection = pymysql.connect(**conn_config)
def query_single_value(self, sql: str):
res = None
with self.connection.cursor() as cursor:
cursor.execute(sql)
row = cursor.fetchone()
res = str(row[0])
return res
def extract_gtid(self, gtid: str, server_id: str):
if gtid is None or server_id is None:
return None
gtid_parts = gtid.split("-")
if len(gtid_parts) != 3:
return None
if gtid_parts[1] == server_id:
return gtid
return None
def query_gtid_current_pos(self, server_id: str):
return self.extract_gtid(self.query_single_value("SELECT @@gtid_current_pos"), server_id)
def query_server_id(self):
return int(self.query_single_value("SELECT @@server_id"))
if __name__ == "__main__":
db = MariaDbGTID(MARIADB_SETTINGS)
server_id = db.query_server_id()
print('Server ID: ', server_id)
# gtid = db.query_gtid_current_pos(server_id)
gtid = '0-1-1' # initial pos
stream = BinLogStreamReader(
connection_settings=MARIADB_SETTINGS,
server_id=server_id,
blocking=False,
only_events=[
MariadbGtidEvent,
MariadbBinLogCheckPointEvent,
RotateEvent,
WriteRowsEvent,
UpdateRowsEvent,
DeleteRowsEvent,
MariadbAnnotateRowsEvent
],
auto_position=gtid,
is_mariadb=True,
annotate_rows_event=True
)
print('Starting reading events from GTID ', gtid)
for binlogevent in stream:
binlogevent.dump()
if isinstance(binlogevent, MariadbGtidEvent):
gtid = binlogevent.gtid
print('Last encountered GTID: ', gtid)
stream.close()