Class MySqlValueConverters
- java.lang.Object
-
- io.debezium.jdbc.JdbcValueConverters
-
- io.debezium.connector.mysql.MySqlValueConverters
-
- All Implemented Interfaces:
ValueConverterProvider
@Immutable public class MySqlValueConverters extends JdbcValueConverters
MySQL-specific customization of the conversions from JDBC values obtained from the MySQL binlog client library.This class always uses UTC for the default time zone when converting values without timezone information to values that require timezones. This is because MySQL
TIMESTAMPvalues are always stored in UTC (unlikeDATETIMEvalues) and are replicated in this form. Meanwhile, the MySQL Binlog Client library willdeserializethese asTimestampvalues that have no timezone and, therefore, are presumed to be in UTC. When the column is properly marked with aTypes.TIMESTAMP_WITH_TIMEZONEtype, the converters will need to convert thatTimestampvalue into anOffsetDateTimeusing the default time zone, which always is UTC.- Author:
- Randall Hauch
- See Also:
AbstractRowsEventDataDeserializer
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceMySqlValueConverters.ParsingErrorHandler-
Nested classes/interfaces inherited from class io.debezium.jdbc.JdbcValueConverters
JdbcValueConverters.BigIntUnsignedMode, JdbcValueConverters.DecimalMode
-
-
Field Summary
Fields Modifier and Type Field Description private static PatternDATE_FIELD_PATTERNUsed to parse values of DATE columns.private static org.slf4j.LoggerLOGGERprivate MySqlValueConverters.ParsingErrorHandlerparsingErrorHandlerprivate static PatternTIME_FIELD_PATTERNUsed to parse values of TIME columns.private static PatternTIMESTAMP_FIELD_PATTERNUsed to parse values of TIMESTAMP columns.-
Fields inherited from class io.debezium.jdbc.JdbcValueConverters
adaptiveTimeMicrosecondsPrecisionMode, adaptiveTimePrecisionMode, bigIntUnsignedMode, binaryMode, decimalMode, logger
-
-
Constructor Summary
Constructors Constructor Description MySqlValueConverters(JdbcValueConverters.DecimalMode decimalMode, TemporalPrecisionMode temporalPrecisionMode, JdbcValueConverters.BigIntUnsignedMode bigIntUnsignedMode, CommonConnectorConfig.BinaryHandlingMode binaryMode)Create a new instance that always uses UTC for the default time zone when_needed converting values without timezone information to values that require timezones.MySqlValueConverters(JdbcValueConverters.DecimalMode decimalMode, TemporalPrecisionMode temporalPrecisionMode, JdbcValueConverters.BigIntUnsignedMode bigIntUnsignedMode, CommonConnectorConfig.BinaryHandlingMode binaryMode, TemporalAdjuster adjuster, MySqlValueConverters.ParsingErrorHandler parsingErrorHandler)Create a new instance that always uses UTC for the default time zone when converting values without timezone information to values that require timezones.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static TemporaladjustTemporal(Temporal temporal)A utility method that adjusts ambiguous 2-digit year values of DATETIME, DATE, and TIMESTAMP types using these MySQL-specific rules: Year values in the range 00-69 are converted to 2000-2069. Year values in the range 70-99 are converted to 1970-1999.protected ByteOrderbyteOrderOfBitType()protected CharsetcharsetFor(Column column)Return theCharsetinstance with the MySQL-specific character set name used by the given column.static booleancontainsZeroValuesInDatePart(String timestampString, Column column, Table table)protected ObjectconvertDurationToMicroseconds(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)protected ObjectconvertEnumToString(List<String> options, Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)Converts a value object for a MySQLENUM, which is represented in the binlog events as an integer value containing the index of the enum option.ValueConverterconverter(Column column, org.apache.kafka.connect.data.Field fieldDefn)protected ObjectconvertGeometry(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)Convert the a value representing a GEOMETRYbyte[]value to a Geometry value used in aSourceRecord.protected ObjectconvertJson(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)protected ObjectconvertPoint(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)Convert the a value representing a POINTbyte[]value to a Point value used in aSourceRecord.protected ObjectconvertSetToString(List<String> options, Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)Converts a value object for a MySQLSET, which is represented in the binlog events contain a long number in which every bit corresponds to a different option.protected StringconvertSetValue(Column column, long indexes, List<String> options)protected ObjectconvertString(Column column, org.apache.kafka.connect.data.Field fieldDefn, Charset columnCharset, Object data)protected ObjectconvertTimestampToLocalDateTime(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)protected ObjectconvertUnsignedBigint(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)Convert the a value representing a Unsigned BIGINT value to the correct Unsigned INT representation.protected ObjectconvertUnsignedInt(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)Convert the a value representing a Unsigned INT value to the correct Unsigned INT representation.protected ObjectconvertUnsignedMediumint(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)Convert the a value representing a Unsigned MEDIUMINT value to the correct Unsigned SMALLINT representation.protected ObjectconvertUnsignedSmallint(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)Convert the a value representing a Unsigned SMALLINT value to the correct Unsigned SMALLINT representation.protected ObjectconvertUnsignedTinyint(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)Convert the a value representing a Unsigned TINYINT value to the correct Unsigned TINYINT representation.protected ObjectconvertYearToInt(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)Converts a value object for a MySQLYEAR, which appear in the binlog as an integer though returns from the MySQL JDBC driver as either a short or aDate.static voiddefaultParsingErrorHandler(String message, Exception exception)protected List<String>extractEnumAndSetOptions(Column column)protected StringextractEnumAndSetOptionsAsString(Column column)protected booleanisGeometryCollection(String upperCaseTypeName)Determine if the uppercase form of a column's type is geometry collection independent of JDBC driver or server version.protected booleanmatches(String upperCaseTypeName, String upperCaseMatch)Determine if the uppercase form of a column's type exactly matches or begins with the specified prefix.org.apache.kafka.connect.data.SchemaBuilderschemaBuilder(Column column)static DurationstringToDuration(String timeString)static LocalDatestringToLocalDate(String dateString, Column column, Table table)protected ByteBuffertoByteBuffer(Column column, byte[] data)-
Methods inherited from class io.debezium.jdbc.JdbcValueConverters
convertBigInt, convertBinary, convertBinaryToBase64, convertBinaryToBytes, convertBinaryToHex, convertBit, convertBits, convertBits, convertBoolean, convertDateToEpochDays, convertDateToEpochDaysAsDate, convertDecimal, convertDouble, convertFloat, convertInteger, convertNumeric, convertReal, convertRowId, convertSmallInt, convertString, convertTime, convertTimestampToEpochMicros, convertTimestampToEpochMillis, convertTimestampToEpochMillisAsDate, convertTimestampToEpochNanos, convertTimestampWithZone, convertTimeToMicrosPastMidnight, convertTimeToMillisPastMidnight, convertTimeToMillisPastMidnightAsDate, convertTimeToNanosPastMidnight, convertTimeWithZone, convertTinyInt, convertValue, getTimePrecision, handleUnknownData, padLittleEndian, toBigDecimal, unexpectedBinary, withScaleAdjustedIfNeeded
-
-
-
-
Field Detail
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
TIME_FIELD_PATTERN
private static final Pattern TIME_FIELD_PATTERN
Used to parse values of TIME columns. Format: 000:00:00.000000.
-
DATE_FIELD_PATTERN
private static final Pattern DATE_FIELD_PATTERN
Used to parse values of DATE columns. Format: 000-00-00.
-
TIMESTAMP_FIELD_PATTERN
private static final Pattern TIMESTAMP_FIELD_PATTERN
Used to parse values of TIMESTAMP columns. Format: 000-00-00 00:00:00.000.
-
parsingErrorHandler
private final MySqlValueConverters.ParsingErrorHandler parsingErrorHandler
-
-
Constructor Detail
-
MySqlValueConverters
public MySqlValueConverters(JdbcValueConverters.DecimalMode decimalMode, TemporalPrecisionMode temporalPrecisionMode, JdbcValueConverters.BigIntUnsignedMode bigIntUnsignedMode, CommonConnectorConfig.BinaryHandlingMode binaryMode)
Create a new instance that always uses UTC for the default time zone when_needed converting values without timezone information to values that require timezones.- Parameters:
decimalMode- howDECIMALandNUMERICvalues should be treated; may be null ifJdbcValueConverters.DecimalMode.PRECISEis to be usedtemporalPrecisionMode- temporal precision mode based onTemporalPrecisionModebigIntUnsignedMode- howBIGINT UNSIGNEDvalues should be treated; may be null ifJdbcValueConverters.BigIntUnsignedMode.PRECISEis to be usedbinaryMode- how binary columns should be represented
-
MySqlValueConverters
public MySqlValueConverters(JdbcValueConverters.DecimalMode decimalMode, TemporalPrecisionMode temporalPrecisionMode, JdbcValueConverters.BigIntUnsignedMode bigIntUnsignedMode, CommonConnectorConfig.BinaryHandlingMode binaryMode, TemporalAdjuster adjuster, MySqlValueConverters.ParsingErrorHandler parsingErrorHandler)
Create a new instance that always uses UTC for the default time zone when converting values without timezone information to values that require timezones.- Parameters:
decimalMode- howDECIMALandNUMERICvalues should be treated; may be null ifJdbcValueConverters.DecimalMode.PRECISEis to be usedtemporalPrecisionMode- temporal precision mode based onTemporalPrecisionModebigIntUnsignedMode- howBIGINT UNSIGNEDvalues should be treated; may be null ifJdbcValueConverters.BigIntUnsignedMode.PRECISEis to be usedbinaryMode- how binary columns should be representedadjuster- a temporal adjuster to make a database specific time modification before conversionhandler- for errors during postponed binlog parsing
-
-
Method Detail
-
adjustTemporal
public static Temporal adjustTemporal(Temporal temporal)
A utility method that adjusts ambiguous 2-digit year values of DATETIME, DATE, and TIMESTAMP types using these MySQL-specific rules:- Year values in the range 00-69 are converted to 2000-2069.
- Year values in the range 70-99 are converted to 1970-1999.
- Parameters:
temporal- the temporal instance to adjust; may not be null- Returns:
- the possibly adjusted temporal instance; never null
-
byteOrderOfBitType
protected ByteOrder byteOrderOfBitType()
- Overrides:
byteOrderOfBitTypein classJdbcValueConverters
-
schemaBuilder
public org.apache.kafka.connect.data.SchemaBuilder schemaBuilder(Column column)
- Specified by:
schemaBuilderin interfaceValueConverterProvider- Overrides:
schemaBuilderin classJdbcValueConverters
-
converter
public ValueConverter converter(Column column, org.apache.kafka.connect.data.Field fieldDefn)
- Specified by:
converterin interfaceValueConverterProvider- Overrides:
converterin classJdbcValueConverters
-
charsetFor
protected Charset charsetFor(Column column)
Return theCharsetinstance with the MySQL-specific character set name used by the given column.- Parameters:
column- the column in which the character set is used; never null- Returns:
- the Java
Charset, or null if there is no mapping
-
convertJson
protected Object convertJson(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
- Parameters:
column- the column in which the value appearsfieldDefn- the field definition for theSourceRecord'sSchema; never nulldata- the data; may be null- Returns:
- the converted value, or null if the conversion could not be made and the column allows nulls
- Throws:
IllegalArgumentException- if the value could not be converted but the column does not allow nulls
-
convertString
protected Object convertString(Column column, org.apache.kafka.connect.data.Field fieldDefn, Charset columnCharset, Object data)
- Parameters:
column- the column in which the value appearsfieldDefn- the field definition for theSourceRecord'sSchema; never nullcolumnCharset- the Java character set in which column byte[] values are encoded; may not be nulldata- the data; may be null- Returns:
- the converted value, or null if the conversion could not be made and the column allows nulls
- Throws:
IllegalArgumentException- if the value could not be converted but the column does not allow nulls
-
convertYearToInt
protected Object convertYearToInt(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Converts a value object for a MySQLYEAR, which appear in the binlog as an integer though returns from the MySQL JDBC driver as either a short or aDate.- Parameters:
column- the column definition describing thedatavalue; never nullfieldDefn- the field definition; never nulldata- the data object to be converted into a year literal integer value; never null- Returns:
- the converted value, or null if the conversion could not be made and the column allows nulls
- Throws:
IllegalArgumentException- if the value could not be converted but the column does not allow nulls
-
convertEnumToString
protected Object convertEnumToString(List<String> options, Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Converts a value object for a MySQLENUM, which is represented in the binlog events as an integer value containing the index of the enum option. The MySQL JDBC driver returns a string containing the option, so this method calculates the same.- Parameters:
options- the characters that appear in the same order as defined in the column; may not be nullcolumn- the column definition describing thedatavalue; never nullfieldDefn- the field definition; never nulldata- the data object to be converted into anENUMliteral String value- Returns:
- the converted value, or null if the conversion could not be made and the column allows nulls
- Throws:
IllegalArgumentException- if the value could not be converted but the column does not allow nulls
-
convertSetToString
protected Object convertSetToString(List<String> options, Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Converts a value object for a MySQLSET, which is represented in the binlog events contain a long number in which every bit corresponds to a different option. The MySQL JDBC driver returns a string containing the comma-separated options, so this method calculates the same.- Parameters:
options- the characters that appear in the same order as defined in the column; may not be nullcolumn- the column definition describing thedatavalue; never nullfieldDefn- the field definition; never nulldata- the data object to be converted into anSETliteral String value; never null- Returns:
- the converted value, or null if the conversion could not be made and the column allows nulls
- Throws:
IllegalArgumentException- if the value could not be converted but the column does not allow nulls
-
matches
protected boolean matches(String upperCaseTypeName, String upperCaseMatch)
Determine if the uppercase form of a column's type exactly matches or begins with the specified prefix. Note that this logic works when the column'stypecontains the type name followed by parentheses.- Parameters:
upperCaseTypeName- the upper case form of the column'stype nameupperCaseMatch- the upper case form of the expected type or prefix of the type; may not be null- Returns:
trueif the type matches the specified type, orfalseotherwise
-
isGeometryCollection
protected boolean isGeometryCollection(String upperCaseTypeName)
Determine if the uppercase form of a column's type is geometry collection independent of JDBC driver or server version.- Parameters:
upperCaseTypeName- the upper case form of the column'stype name- Returns:
trueif the type is geometry collection
-
convertSetValue
protected String convertSetValue(Column column, long indexes, List<String> options)
-
convertPoint
protected Object convertPoint(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Convert the a value representing a POINTbyte[]value to a Point value used in aSourceRecord.- Parameters:
column- the column in which the value appearsfieldDefn- the field definition for theSourceRecord'sSchema; never nulldata- the data; may be null- Returns:
- the converted value, or null if the conversion could not be made and the column allows nulls
- Throws:
IllegalArgumentException- if the value could not be converted but the column does not allow nulls
-
convertGeometry
protected Object convertGeometry(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Convert the a value representing a GEOMETRYbyte[]value to a Geometry value used in aSourceRecord.- Parameters:
column- the column in which the value appearsfieldDefn- the field definition for theSourceRecord'sSchema; never nulldata- the data; may be null- Returns:
- the converted value, or null if the conversion could not be made and the column allows nulls
- Throws:
IllegalArgumentException- if the value could not be converted but the column does not allow nulls
-
toByteBuffer
protected ByteBuffer toByteBuffer(Column column, byte[] data)
- Overrides:
toByteBufferin classJdbcValueConverters
-
convertUnsignedTinyint
protected Object convertUnsignedTinyint(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Convert the a value representing a Unsigned TINYINT value to the correct Unsigned TINYINT representation.- Parameters:
column- the column in which the value appearsfieldDefn- the field definition for theSourceRecord'sSchema; never nulldata- the data; may be null- Returns:
- the converted value, or null if the conversion could not be made and the column allows nulls
- Throws:
IllegalArgumentException- if the value could not be converted but the column does not allow nulls
-
convertUnsignedSmallint
protected Object convertUnsignedSmallint(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Convert the a value representing a Unsigned SMALLINT value to the correct Unsigned SMALLINT representation.- Parameters:
column- the column in which the value appearsfieldDefn- the field definition for theSourceRecord'sSchema; never nulldata- the data; may be null- Returns:
- the converted value, or null if the conversion could not be made and the column allows nulls
- Throws:
IllegalArgumentException- if the value could not be converted but the column does not allow nulls
-
convertUnsignedMediumint
protected Object convertUnsignedMediumint(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Convert the a value representing a Unsigned MEDIUMINT value to the correct Unsigned SMALLINT representation.- Parameters:
column- the column in which the value appearsfieldDefn- the field definition for theSourceRecord'sSchema; never nulldata- the data; may be null- Returns:
- the converted value, or null if the conversion could not be made and the column allows nulls
- Throws:
IllegalArgumentException- if the value could not be converted but the column does not allow nulls
-
convertUnsignedInt
protected Object convertUnsignedInt(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Convert the a value representing a Unsigned INT value to the correct Unsigned INT representation.- Parameters:
column- the column in which the value appearsfieldDefn- the field definition for theSourceRecord'sSchema; never nulldata- the data; may be null- Returns:
- the converted value, or null if the conversion could not be made and the column allows nulls
- Throws:
IllegalArgumentException- if the value could not be converted but the column does not allow nulls
-
convertUnsignedBigint
protected Object convertUnsignedBigint(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Convert the a value representing a Unsigned BIGINT value to the correct Unsigned INT representation.- Parameters:
column- the column in which the value appearsfieldDefn- the field definition for theSourceRecord'sSchema; never nulldata- the data; may be null- Returns:
- the converted value, or null if the conversion could not be made and the column allows nulls
- Throws:
IllegalArgumentException- if the value could not be converted but the column does not allow nulls
-
convertDurationToMicroseconds
protected Object convertDurationToMicroseconds(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Converts a value object for an expected type ofDurationtoLongvalues that represents the time in microseconds.Per the JDBC specification, databases should return
Timeinstances, but that's not working because it can only handle Daytime 00:00:00-23:59:59. We useDurationinstead that can handle the range of -838:59:59.000000 to 838:59:59.000000 of a MySQL TIME type and transfer data as signed INT64 which reflects the DB value converted to microseconds.- Parameters:
column- the column definition describing thedatavalue; never nullfieldDefn- the field definition; never nulldata- the data object to be converted into aDurationtype; never null- Returns:
- the converted value, or null if the conversion could not be made and the column allows nulls
- Throws:
IllegalArgumentException- if the value could not be converted but the column does not allow nulls
-
convertTimestampToLocalDateTime
protected Object convertTimestampToLocalDateTime(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
-
stringToLocalDate
public static LocalDate stringToLocalDate(String dateString, Column column, Table table)
-
containsZeroValuesInDatePart
public static boolean containsZeroValuesInDatePart(String timestampString, Column column, Table table)
-
-