聊聊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).

*

* <p>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).

*

* <p>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 AbstractRowsEventDataDeserializer<T extends EventData> implements EventDataDeserializer<T> {

//......

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

以上是 聊聊EventDeserializer的CompatibilityMode 的全部内容, 来源链接: utcz.com/z/516020.html

回到顶部