|
2 | 2 |
|
3 | 3 | import org.apache.kafka.clients.consumer.ConsumerRecord;
|
4 | 4 | import org.apache.kafka.clients.consumer.ConsumerRecords;
|
| 5 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 6 | +import org.apache.kafka.clients.producer.ProducerRecord; |
5 | 7 | import org.apache.kafka.connect.data.Date;
|
6 | 8 | import org.apache.kafka.connect.data.Decimal;
|
7 | 9 | import org.apache.kafka.connect.data.Schema;
|
8 | 10 | import org.apache.kafka.connect.data.SchemaBuilder;
|
9 | 11 | import org.apache.kafka.connect.data.Struct;
|
10 | 12 | import org.apache.kafka.connect.data.Time;
|
11 | 13 | import org.apache.kafka.connect.data.Timestamp;
|
| 14 | +import org.apache.kafka.connect.errors.ConnectException; |
12 | 15 | import org.apache.kafka.connect.json.JsonConverter;
|
13 | 16 | import org.apache.kafka.connect.runtime.AbstractStatus;
|
| 17 | +import org.apache.kafka.connect.runtime.Connect; |
14 | 18 | import org.apache.kafka.connect.runtime.ConnectorConfig;
|
15 | 19 | import org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo;
|
16 | 20 | import org.apache.kafka.connect.storage.Converter;
|
|
46 | 50 | import static java.util.concurrent.TimeUnit.SECONDS;
|
47 | 51 | import static org.apache.kafka.connect.runtime.ConnectorConfig.KEY_CONVERTER_CLASS_CONFIG;
|
48 | 52 | import static org.apache.kafka.connect.runtime.ConnectorConfig.VALUE_CONVERTER_CLASS_CONFIG;
|
| 53 | +import static org.hamcrest.CoreMatchers.containsString; |
| 54 | +import static org.hamcrest.MatcherAssert.assertThat; |
49 | 55 | import static org.junit.jupiter.api.Assertions.fail;
|
50 | 56 |
|
51 | 57 | @Testcontainers
|
@@ -457,6 +463,53 @@ private void testTimestampUnitResolution0(String mode) {
|
457 | 463 | "select firstname,lastname,timestamp from " + topicName);
|
458 | 464 | }
|
459 | 465 |
|
| 466 | + @Test |
| 467 | + public void testKafkaNativeTimestampsAndExplicitDesignatedFieldTimestampMutuallyExclusive() { |
| 468 | + Map<String, String> props = baseConnectorProps(topicName); |
| 469 | + props.put("value.converter.schemas.enable", "false"); |
| 470 | + props.put(QuestDBSinkConnectorConfig.DESIGNATED_TIMESTAMP_COLUMN_NAME_CONFIG, "born"); |
| 471 | + props.put(QuestDBSinkConnectorConfig.DESIGNATED_TIMESTAMP_KAFKA_NATIVE_CONFIG, "true"); |
| 472 | + try { |
| 473 | + connect.configureConnector(CONNECTOR_NAME, props); |
| 474 | + fail("Expected ConnectException"); |
| 475 | + } catch (ConnectException e) { |
| 476 | + assertThat(e.getMessage(), containsString("timestamp.field.name with timestamp.kafka.native")); |
| 477 | + } |
| 478 | + } |
| 479 | + |
| 480 | + @Test |
| 481 | + public void testKafkaNativeTimestamp() { |
| 482 | + connect.kafka().createTopic(topicName, 1); |
| 483 | + Map<String, String> props = baseConnectorProps(topicName); |
| 484 | + props.put("value.converter.schemas.enable", "false"); |
| 485 | + props.put(QuestDBSinkConnectorConfig.DESIGNATED_TIMESTAMP_KAFKA_NATIVE_CONFIG, "true"); |
| 486 | + |
| 487 | + connect.configureConnector(CONNECTOR_NAME, props); |
| 488 | + assertConnectorTaskRunningEventually(); |
| 489 | + |
| 490 | + QuestDBUtils.assertSql(questDBContainer, |
| 491 | + "{\"ddl\":\"OK\"}\n", |
| 492 | + "create table " + topicName + " (firstname string, lastname string, born timestamp) timestamp(born)", |
| 493 | + QuestDBUtils.Endpoint.EXEC); |
| 494 | + |
| 495 | + java.util.Date birth = new Calendar.Builder() |
| 496 | + .setTimeZone(TimeZone.getTimeZone("UTC")) |
| 497 | + .setDate(2022, 9, 23) // note: month is 0-based, so it's October and not November |
| 498 | + .setTimeOfDay(13, 53, 59, 123) |
| 499 | + .build().getTime(); |
| 500 | + |
| 501 | + Map<String, Object> prodProps = new HashMap<>(); |
| 502 | + try (KafkaProducer<byte[], byte[]> producer = connect.kafka().createProducer(prodProps)) { |
| 503 | + String val = "{\"firstname\":\"John\",\"lastname\":\"Doe\"}"; |
| 504 | + ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(topicName, null, birth.getTime(), null, val.getBytes()); |
| 505 | + producer.send(record); |
| 506 | + } |
| 507 | + |
| 508 | + QuestDBUtils.assertSqlEventually(questDBContainer, "\"firstname\",\"lastname\",\"born\"\r\n" + |
| 509 | + "\"John\",\"Doe\",\"2022-10-23T13:53:59.123000Z\"\r\n", |
| 510 | + "select * from " + topicName); |
| 511 | + } |
| 512 | + |
460 | 513 | @Test
|
461 | 514 | public void testTimestampSMT_parseTimestamp_schemaLess() {
|
462 | 515 | connect.kafka().createTopic(topicName, 1);
|
|
0 commit comments