Last active
May 18, 2017 15:44
-
-
Save mattyb149/61ea035e5e917e65fd05c74bec0d090b to your computer and use it in GitHub Desktop.
A Groovy script to dump MySQL binlog events to the console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab('com.github.shyiko:mysql-binlog-connector-java:0.8.1') | |
import com.github.shyiko.mysql.binlog.event.* | |
import com.github.shyiko.mysql.binlog.* | |
args = ['192.168.99.100', '32768', 'root', 'K3sdchkm'] as String[] | |
if(!args || args.length < 3) { | |
println 'Usage: groovy binlog_connector.groovy <host> <port> <username> [<password>]' | |
return 1 | |
} | |
def client = new BinaryLogClient(args[0], Integer.parseInt(args[1]), args[2], args.length == 4 ? args[3] : '') | |
def recordCount = 0 | |
client.registerEventListener({ event -> | |
def type = event.header.eventType | |
println ("$type @ ${client.binlogFilename} : ${event.header.position} -> ${event.header.nextPosition}") | |
switch (type) { | |
case EventType.ROTATE: | |
println "\t${event.data.binlogPosition}" | |
break | |
case EventType.QUERY: | |
println "\t${event.data.database}" | |
println "\t${event.data.sql}" | |
recordCount++ | |
break | |
case EventType.TABLE_MAP: | |
println "\t${event.data.database}.${event.data.table} ${event.data.columnTypes}" | |
break | |
case EventType.EXT_WRITE_ROWS: | |
case EventType.WRITE_ROWS: | |
case EventType.EXT_DELETE_ROWS: | |
case EventType.DELETE_ROWS: | |
event.data.includedColumns.each {println "\tColumns modified: $it"} | |
event.data.rows.each { | |
println '\t' + it.collect { cell -> | |
return (cell instanceof byte[]) ? new String(cell) : cell | |
}.join(',') | |
} | |
recordCount++ | |
break | |
case EventType.EXT_UPDATE_ROWS: | |
case EventType.UPDATE_ROWS: | |
println "\tColumns updated: ${event.data.includedColumnsBeforeUpdate} => ${event.data.includedColumns}" | |
event.data.rows.each {entry -> println "\t${entry.key} now ${entry.value}"} | |
recordCount++ | |
break | |
case EventType.XID: | |
recordCount++ | |
break | |
} | |
} as BinaryLogClient.EventListener) | |
client.binlogFilename = '' | |
client.binlogPosition = -1 | |
client.connect(5000) | |
Thread.sleep(1000) | |
client.disconnect() | |
println recordCount |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment