Search in sources :

Example 1 with RdsLocalBinlogEventParser

use of com.alibaba.otter.canal.parse.inbound.mysql.rds.RdsLocalBinlogEventParser in project canal by alibaba.

the class RdsLocalBinlogDumpTest method testSimple.

@Test
public void testSimple() {
    String directory = "/tmp/rds";
    final RdsLocalBinlogEventParser controller = new RdsLocalBinlogEventParser();
    controller.setMasterInfo(new AuthenticationInfo(new InetSocketAddress("127.0.0.1", 3306), "root", "hello"));
    controller.setConnectionCharsetStd(Charset.forName("UTF-8"));
    controller.setDirectory(directory);
    controller.setAccesskey("");
    controller.setSecretkey("");
    controller.setInstanceId("");
    controller.setStartTime(1507860498350L);
    controller.setEventSink(new AbstractCanalEventSinkTest<List<Entry>>() {

        public boolean sink(List<Entry> entrys, InetSocketAddress remoteAddress, String destination) throws CanalSinkException, InterruptedException {
            for (Entry entry : entrys) {
                if (entry.getEntryType() == EntryType.TRANSACTIONBEGIN || entry.getEntryType() == EntryType.TRANSACTIONEND) {
                    continue;
                }
                if (entry.getEntryType() == EntryType.ROWDATA) {
                    RowChange rowChange = null;
                    try {
                        rowChange = RowChange.parseFrom(entry.getStoreValue());
                    } catch (Exception e) {
                        throw new RuntimeException("ERROR ## parser of eromanga-event has an error , data:" + entry.toString(), e);
                    }
                    EventType eventType = rowChange.getEventType();
                    System.out.println(String.format("================> binlog[%s:%s] , name[%s,%s] , eventType : %s", entry.getHeader().getLogfileName(), entry.getHeader().getLogfileOffset(), entry.getHeader().getSchemaName(), entry.getHeader().getTableName(), eventType));
                    for (RowData rowData : rowChange.getRowDatasList()) {
                        if (eventType == EventType.DELETE) {
                            print(rowData.getBeforeColumnsList());
                        } else if (eventType == EventType.INSERT) {
                            print(rowData.getAfterColumnsList());
                        } else {
                            System.out.println("-------> before");
                            print(rowData.getBeforeColumnsList());
                            System.out.println("-------> after");
                            print(rowData.getAfterColumnsList());
                        }
                    }
                }
            }
            return true;
        }
    });
    controller.setLogPositionManager(new AbstractLogPositionManager() {

        @Override
        public LogPosition getLatestIndexBy(String destination) {
            return null;
        }

        @Override
        public void persistLogPosition(String destination, LogPosition logPosition) throws CanalParseException {
            System.out.println(logPosition);
        }
    });
    controller.start();
    try {
        Thread.sleep(100 * 1000 * 1000L);
    } catch (InterruptedException e) {
        Assert.fail(e.getMessage());
    }
    controller.stop();
}
Also used : RdsLocalBinlogEventParser(com.alibaba.otter.canal.parse.inbound.mysql.rds.RdsLocalBinlogEventParser) RowChange(com.alibaba.otter.canal.protocol.CanalEntry.RowChange) EventType(com.alibaba.otter.canal.protocol.CanalEntry.EventType) InetSocketAddress(java.net.InetSocketAddress) AbstractLogPositionManager(com.alibaba.otter.canal.parse.index.AbstractLogPositionManager) AuthenticationInfo(com.alibaba.otter.canal.parse.support.AuthenticationInfo) CanalSinkException(com.alibaba.otter.canal.sink.exception.CanalSinkException) CanalParseException(com.alibaba.otter.canal.parse.exception.CanalParseException) CanalParseException(com.alibaba.otter.canal.parse.exception.CanalParseException) Entry(com.alibaba.otter.canal.protocol.CanalEntry.Entry) RowData(com.alibaba.otter.canal.protocol.CanalEntry.RowData) List(java.util.List) CanalSinkException(com.alibaba.otter.canal.sink.exception.CanalSinkException) LogPosition(com.alibaba.otter.canal.protocol.position.LogPosition) Test(org.junit.Test) AbstractCanalEventSinkTest(com.alibaba.otter.canal.parse.stub.AbstractCanalEventSinkTest)

Aggregations

CanalParseException (com.alibaba.otter.canal.parse.exception.CanalParseException)1 RdsLocalBinlogEventParser (com.alibaba.otter.canal.parse.inbound.mysql.rds.RdsLocalBinlogEventParser)1 AbstractLogPositionManager (com.alibaba.otter.canal.parse.index.AbstractLogPositionManager)1 AbstractCanalEventSinkTest (com.alibaba.otter.canal.parse.stub.AbstractCanalEventSinkTest)1 AuthenticationInfo (com.alibaba.otter.canal.parse.support.AuthenticationInfo)1 Entry (com.alibaba.otter.canal.protocol.CanalEntry.Entry)1 EventType (com.alibaba.otter.canal.protocol.CanalEntry.EventType)1 RowChange (com.alibaba.otter.canal.protocol.CanalEntry.RowChange)1 RowData (com.alibaba.otter.canal.protocol.CanalEntry.RowData)1 LogPosition (com.alibaba.otter.canal.protocol.position.LogPosition)1 CanalSinkException (com.alibaba.otter.canal.sink.exception.CanalSinkException)1 InetSocketAddress (java.net.InetSocketAddress)1 List (java.util.List)1 Test (org.junit.Test)1