聊聊EventDeserializer的CompatibilityMode
来源:SegmentFault
时间:2023-02-24 19:51:34 413浏览 收藏
本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《聊聊EventDeserializer的CompatibilityMode》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~
序
本文主要研究一下EventDeserializer的CompatibilityMode
CompatibilityMode
mysql-binlog-connector-java-0.20.1/src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/EventDeserializer.java
public enum CompatibilityMode { /** * Return DATETIME/DATETIME_V2/TIMESTAMP/TIMESTAMP_V2/DATE/TIME/TIME_V2 values as long|s * (number of milliseconds since the epoch (00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, * not counting leap seconds)) (instead of java.util.Date/java.sql.Timestamp/java.sql.Date/new java.sql.Time). * *This option is going to be enabled by default starting from mysql-binlog-connector-java@1.0.0. */ DATE_AND_TIME_AS_LONG, /** * Same as {@link CompatibilityMode#DATE_AND_TIME_AS_LONG} but values are returned in microseconds. */ DATE_AND_TIME_AS_LONG_MICRO, /** * Return 0 instead of null if year/month/day is 0. * Affects DATETIME/DATETIME_V2/DATE/TIME/TIME_V2. */ INVALID_DATE_AND_TIME_AS_ZERO, /** * Return -1 instead of null if year/month/day is 0. * Affects DATETIME/DATETIME_V2/DATE/TIME/TIME_V2. * * @deprecated */ INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE, /** * Return Long.MIN_VALUE instead of null if year/month/day is 0. * Affects DATETIME/DATETIME_V2/DATE/TIME/TIME_V2. */ INVALID_DATE_AND_TIME_AS_MIN_VALUE, /** * Return CHAR/VARCHAR/BINARY/VARBINARY values as byte[]|s (instead of String|s). * *
This option is going to be enabled by default starting from mysql-binlog-connector-java@1.0.0. */ CHAR_AND_BINARY_AS_BYTE_ARRAY }
- EventDeserializer的CompatibilityMode定义了DATE_AND_TIME_AS_LONG、DATE_AND_TIME_AS_LONG_MICRO、INVALID_DATE_AND_TIME_AS_ZERO、INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE、INVALID_DATE_AND_TIME_AS_MIN_VALUE、CHAR_AND_BINARY_AS_BYTE_ARRAY这几个枚举值
ensureCompatibility
mysql-binlog-connector-java-0.20.1/src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/EventDeserializer.java
public class EventDeserializer { //...... private void ensureCompatibility(EventDataDeserializer eventDataDeserializer) { if (eventDataDeserializer instanceof AbstractRowsEventDataDeserializer) { AbstractRowsEventDataDeserializer deserializer = (AbstractRowsEventDataDeserializer) eventDataDeserializer; boolean deserializeDateAndTimeAsLong = compatibilitySet.contains(CompatibilityMode.DATE_AND_TIME_AS_LONG) || compatibilitySet.contains(CompatibilityMode.DATE_AND_TIME_AS_LONG_MICRO); deserializer.setDeserializeDateAndTimeAsLong(deserializeDateAndTimeAsLong); deserializer.setMicrosecondsPrecision( compatibilitySet.contains(CompatibilityMode.DATE_AND_TIME_AS_LONG_MICRO) ); if (compatibilitySet.contains(CompatibilityMode.INVALID_DATE_AND_TIME_AS_ZERO)) { deserializer.setInvalidDateAndTimeRepresentation(0L); } if (compatibilitySet.contains(CompatibilityMode.INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE)) { if (!deserializeDateAndTimeAsLong) { throw new IllegalArgumentException("INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE requires " + "DATE_AND_TIME_AS_LONG or DATE_AND_TIME_AS_LONG_MICRO"); } deserializer.setInvalidDateAndTimeRepresentation(-1L); } if (compatibilitySet.contains(CompatibilityMode.INVALID_DATE_AND_TIME_AS_MIN_VALUE)) { if (!deserializeDateAndTimeAsLong) { throw new IllegalArgumentException("INVALID_DATE_AND_TIME_AS_MIN_VALUE requires " + "DATE_AND_TIME_AS_LONG or DATE_AND_TIME_AS_LONG_MICRO"); } deserializer.setInvalidDateAndTimeRepresentation(Long.MIN_VALUE); } deserializer.setDeserializeCharAndBinaryAsByteArray( compatibilitySet.contains(CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY) ); } } //...... }
- EventDeserializer的ensureCompatibility方法在compatibilitySet包含DATE_AND_TIME_AS_LONG或者DATE_AND_TIME_AS_LONG_MICRO时,设置deserializeDateAndTimeAsLong为true;在compatibilitySet包含DATE_AND_TIME_AS_LONG_MICRO时设置microsecondsPrecision为true;在compatibilitySet包含INVALID_DATE_AND_TIME_AS_ZERO时设置invalidDateAndTimeRepresentation为0L;在compatibilitySet包含INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE时设置invalidDateAndTimeRepresentation为-1;在compatibilitySet包含INVALID_DATE_AND_TIME_AS_MIN_VALUE时设置invalidDateAndTimeRepresentation为Long.MIN_VALUE;在compatibilitySet包含CHAR_AND_BINARY_AS_BYTE_ARRAY时设置deserializeCharAndBinaryAsByteArray为true
AbstractRowsEventDataDeserializer
mysql-binlog-connector-java-0.20.1/src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/AbstractRowsEventDataDeserializer.java
public abstract class AbstractRowsEventDataDeserializerimplements EventDataDeserializer { //...... private Long castTimestamp(Long timestamp, int fsp) { if (microsecondsPrecision && timestamp != null && !timestamp.equals(invalidDateAndTimeRepresentation)) { return timestamp * 1000 + fsp % 1000; } return timestamp; } protected Long asUnixTime(int year, int month, int day, int hour, int minute, int second, int millis) { // https://dev.mysql.com/doc/refman/5.0/en/datetime.html if (year == 0 || month == 0 || day == 0) { return invalidDateAndTimeRepresentation; } return UnixTime.from(year, month, day, hour, minute, second, millis); } //...... }
- AbstractRowsEventDataDeserializer的castTimestamp方法根据microsecondsPrecision来决定是否设置精度;asUnixTime方法则在year或者month或者day为0时返回invalidDateAndTimeRepresentation,其余的通过UnixTime构造返回
小结
EventDeserializer的CompatibilityMode定义了DATE_AND_TIME_AS_LONG、DATE_AND_TIME_AS_LONG_MICRO、INVALID_DATE_AND_TIME_AS_ZERO、INVALID_DATE_AND_TIME_AS_NEGATIVE_ONE、INVALID_DATE_AND_TIME_AS_MIN_VALUE、CHAR_AND_BINARY_AS_BYTE_ARRAY这几个枚举值
doc
本篇关于《聊聊EventDeserializer的CompatibilityMode》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于数据库的相关知识,请关注golang学习网公众号!
-
499 收藏
-
244 收藏
-
235 收藏
-
157 收藏
-
101 收藏
-
493 收藏
-
118 收藏
-
133 收藏
-
290 收藏
-
301 收藏
-
461 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习