zoukankan      html  css  js  c++  java
  • ARTS打卡计划第二周-Tips-mysql-binlog-connector-java的使用

    最近发现一个挺不错的框架mysql-binlog-connector-java,可以实时监控binlog的变化。

    首先检查mysql的binlog是否开启,在开启的情况下:

    引入依赖

      <dependency>
        <groupId>com.github.shyiko</groupId>
        <artifactId>mysql-binlog-connector-java</artifactId>
        <version>0.18.1</version>
    </dependency>
    

     然后使用如下代码可以测试:        

    public class App 
    {
        public static void main( String[] args ) throws IOException
        {
        	BinaryLogClient client = new BinaryLogClient("xxx", 3306, "xxx", "xxx");
        	EventDeserializer eventDeserializer = new EventDeserializer();
        	eventDeserializer.setCompatibilityMode(
        	    EventDeserializer.CompatibilityMode.DATE_AND_TIME_AS_LONG,
        	    EventDeserializer.CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY
        	);
        	client.setEventDeserializer(eventDeserializer);
        	client.registerEventListener(new EventListener() {
    
        	    @Override
        	    public void onEvent(Event event) {
        	    		System.out.println(event);
        	    		EventData data = event.getData();
        	    	 
        	            if (data instanceof UpdateRowsEventData) {
        	                System.out.println("Update--------------");
        	                System.out.println(data.toString());
        	            } else if (data instanceof WriteRowsEventData) {
        	                System.out.println("Write---------------");
        	                System.out.println(data.toString());
        	            } else if (data instanceof DeleteRowsEventData) {
        	                System.out.println("Delete--------------");
        	                System.out.println(data.toString());
        	            }
        	    }
        	});
        	client.connect();
        }
    }                                                                                                                                                

    实际在使用的时候,这个框架提供列名称表名称不太好用,这个时候需要https://github.com/ngocdaothanh/mydit ,这个是一个将mysql同步到mongdb的,其中一些样例代码可以很方便的获取mysql的元数据。
  • 相关阅读:
    python 闭包
    并行【parallel】和并发【concurrency】线程是并发还是并行,进程是并发还是并行
    生成器,送代器的区别
    深拷贝和浅拷贝的区别
    对缺省参数的理解
    对不定长参数的理解
    mysql三范式
    mysql基础查询语法
    mysql常见查询练习题
    Ubantu-Nginx部署
  • 原文地址:https://www.cnblogs.com/dongqiSilent/p/10786797.html
Copyright © 2011-2022 走看看