|
30 | 30 | import io.debezium.time.NanoTimestamp; |
31 | 31 | import io.debezium.time.Time; |
32 | 32 | import io.debezium.time.Timestamp; |
| 33 | +import io.debezium.time.ZonedTimestamp; |
33 | 34 | import org.apache.kafka.connect.data.Decimal; |
34 | 35 | import org.apache.kafka.connect.data.Field; |
35 | 36 | import org.apache.kafka.connect.data.SchemaBuilder; |
@@ -204,7 +205,14 @@ public static StructuredRecord convert(Struct struct) { |
204 | 205 | builder.setTimestamp(fieldName, getZonedDateTime((long) val, TimeUnit.MILLISECONDS)); |
205 | 206 | break; |
206 | 207 | case TIMESTAMP_MICROS: |
207 | | - builder.setTimestamp(fieldName, getZonedDateTime((long) val, TimeUnit.MICROSECONDS)); |
| 208 | + if (val instanceof Long) { |
| 209 | + builder.setTimestamp(fieldName, getZonedDateTime((long) val, TimeUnit.MICROSECONDS)); |
| 210 | + break; |
| 211 | + } |
| 212 | + if (val instanceof String) { |
| 213 | + builder.setTimestamp(fieldName, ZonedDateTime.parse((String)val)); |
| 214 | + break; |
| 215 | + } |
208 | 216 | break; |
209 | 217 | case TIME_MILLIS: |
210 | 218 | builder.setTime(fieldName, LocalTime.ofNanoOfDay(TimeUnit.MILLISECONDS.toNanos((int) val))); |
@@ -239,11 +247,16 @@ private static ZonedDateTime getZonedDateTime(long ts, TimeUnit unit) { |
239 | 247 | long mod = unit.convert(1, TimeUnit.SECONDS); |
240 | 248 | int fraction = (int) (ts % mod); |
241 | 249 | long tsInSeconds = unit.toSeconds(ts); |
| 250 | + return getZonedDateTime(tsInSeconds, fraction, unit); |
| 251 | + } |
| 252 | + |
| 253 | + private static ZonedDateTime getZonedDateTime(long epochSecond, long fraction, TimeUnit fractionUnit) { |
242 | 254 | // create an Instant with time in seconds and fraction which will be stored as nano seconds. |
243 | | - Instant instant = Instant.ofEpochSecond(tsInSeconds, unit.toNanos(fraction)); |
| 255 | + Instant instant = Instant.ofEpochSecond(epochSecond, fractionUnit.toNanos(fraction)); |
244 | 256 | return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC); |
245 | 257 | } |
246 | 258 |
|
| 259 | + |
247 | 260 | private static Object convert(org.apache.kafka.connect.data.Schema schema, Object val) { |
248 | 261 | if (val == null) { |
249 | 262 | return null; |
@@ -302,7 +315,11 @@ public static Schema convert(org.apache.kafka.connect.data.Schema schema) { |
302 | 315 | } |
303 | 316 | break; |
304 | 317 | case STRING: |
305 | | - converted = Schema.of(Schema.Type.STRING); |
| 318 | + if (ZonedTimestamp.SCHEMA_NAME.equals(schema.name())) { |
| 319 | + converted = Schema.of(Schema.LogicalType.TIMESTAMP_MICROS); |
| 320 | + } else { |
| 321 | + converted = Schema.of(Schema.Type.STRING); |
| 322 | + } |
306 | 323 | break; |
307 | 324 | case INT8: |
308 | 325 | case INT16: |
|
0 commit comments