|
| 1 | +package io.questdb.kafka; |
| 2 | + |
| 3 | +import io.confluent.kafka.serializers.KafkaAvroSerializer; |
| 4 | +import io.confluent.kafka.serializers.KafkaAvroSerializerConfig; |
| 5 | +import io.debezium.testing.testcontainers.ConnectorConfiguration; |
| 6 | +import io.debezium.testing.testcontainers.DebeziumContainer; |
| 7 | +import io.questdb.client.Sender; |
| 8 | +import io.questdb.kafka.domain.Student; |
| 9 | +import org.apache.avro.Schema; |
| 10 | +import org.apache.avro.generic.GenericData; |
| 11 | +import org.apache.avro.generic.GenericRecord; |
| 12 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 13 | +import org.apache.kafka.clients.producer.Producer; |
| 14 | +import org.apache.kafka.clients.producer.ProducerConfig; |
| 15 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 16 | +import org.apache.kafka.common.serialization.StringSerializer; |
| 17 | +import org.jetbrains.annotations.NotNull; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | +import org.testcontainers.containers.GenericContainer; |
| 22 | +import org.testcontainers.containers.KafkaContainer; |
| 23 | +import org.testcontainers.containers.Network; |
| 24 | +import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 25 | +import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; |
| 26 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 27 | +import org.testcontainers.junit.jupiter.Container; |
| 28 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 29 | +import org.testcontainers.utility.DockerImageName; |
| 30 | +import org.testcontainers.utility.MountableFile; |
| 31 | + |
| 32 | +import java.time.Instant; |
| 33 | +import java.util.Properties; |
| 34 | + |
| 35 | +import static java.time.Duration.ofMinutes; |
| 36 | + |
| 37 | +@Testcontainers |
| 38 | +public class AvroSchemaRegistryIT { |
| 39 | + // we need to locate JARs with QuestDB client and Kafka Connect Connector, |
| 40 | + // this is later used to copy to the Kafka Connect container |
| 41 | + @RegisterExtension |
| 42 | + public static JarResolverExtension connectorJarResolver = JarResolverExtension.forClass(QuestDBSinkTask.class); |
| 43 | + @RegisterExtension |
| 44 | + public static JarResolverExtension questdbJarResolver = JarResolverExtension.forClass(Sender.class); |
| 45 | + |
| 46 | + private final static Network network = Network.newNetwork(); |
| 47 | + |
| 48 | + @Container |
| 49 | + private final KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.2.0")) |
| 50 | + .withNetwork(network); |
| 51 | + |
| 52 | + @Container |
| 53 | + private final GenericContainer<?> questDBContainer = new GenericContainer<>("questdb/questdb:6.5.3") |
| 54 | + .withNetwork(network) |
| 55 | + .withExposedPorts(QuestDBUtils.QUESTDB_HTTP_PORT) |
| 56 | + .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("questdb"))) |
| 57 | + .withEnv("QDB_CAIRO_COMMIT_LAG", "100") |
| 58 | + .withEnv("JAVA_OPTS", "-Djava.locale.providers=JRE,SPI"); |
| 59 | + |
| 60 | + @Container |
| 61 | + private final DebeziumContainer connectContainer = new DebeziumContainer("confluentinc/cp-kafka-connect:7.2.1") |
| 62 | + .withEnv("CONNECT_BOOTSTRAP_SERVERS", kafkaContainer.getNetworkAliases().get(0) + ":9092") |
| 63 | + .withEnv("CONNECT_GROUP_ID", "test") |
| 64 | + .withEnv("CONNECT_OFFSET_STORAGE_TOPIC", "connect-storage-topic") |
| 65 | + .withEnv("CONNECT_CONFIG_STORAGE_TOPIC", "connect-config-topic") |
| 66 | + .withEnv("CONNECT_STATUS_STORAGE_TOPIC", "connect-status-topic") |
| 67 | + .withEnv("CONNECT_KEY_CONVERTER", "org.apache.kafka.connect.storage.StringConverter") |
| 68 | + .withEnv("CONNECT_VALUE_CONVERTER", "org.apache.kafka.connect.json.JsonConverter") |
| 69 | + .withEnv("CONNECT_VALUE_CONVERTER_SCHEMAS_ENABLE", "false") |
| 70 | + .withEnv("CONNECT_REST_ADVERTISED_HOST_NAME", "connect") |
| 71 | + .withEnv("CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR", "1") |
| 72 | + .withEnv("CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR", "1") |
| 73 | + .withEnv("CONNECT_STATUS_STORAGE_REPLICATION_FACTOR", "1") |
| 74 | + .withNetwork(network) |
| 75 | + .withExposedPorts(8083) |
| 76 | + .withCopyFileToContainer(MountableFile.forHostPath(connectorJarResolver.getJarPath()), "/usr/share/java/kafka/questdb-connector.jar") |
| 77 | + .withCopyFileToContainer(MountableFile.forHostPath(questdbJarResolver.getJarPath()), "/usr/share/java/kafka/questdb.jar") |
| 78 | + .withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("connect"))) |
| 79 | + .dependsOn(kafkaContainer, questDBContainer) |
| 80 | + .waitingFor(new HttpWaitStrategy() |
| 81 | + .forPath("/connectors") |
| 82 | + .forStatusCode(200) |
| 83 | + .forPort(8083) |
| 84 | + .withStartupTimeout(ofMinutes(5))); |
| 85 | + |
| 86 | + @Container |
| 87 | + private GenericContainer<?> schemaRegistry = new GenericContainer<>(DockerImageName.parse("confluentinc/cp-schema-registry:7.2.2")) |
| 88 | + .withNetwork(network) |
| 89 | + .withNetworkAliases("schema-registry") |
| 90 | + .withEnv("SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS", kafkaContainer.getNetworkAliases().get(0) + ":9092") |
| 91 | + .withEnv("SCHEMA_REGISTRY_HOST_NAME", "localhost") |
| 92 | + .withExposedPorts(8081) |
| 93 | + .dependsOn(kafkaContainer) |
| 94 | + .waitingFor(Wait.forHttp("/subjects")); |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testSmoke() throws Exception { |
| 98 | + String topicName = "mytopic"; |
| 99 | + try (Producer<String, Student> producer = new KafkaProducer<>(producerProps())) { |
| 100 | + Student student = Student.newBuilder() |
| 101 | + .setFirstname("John") |
| 102 | + .setLastname("Doe") |
| 103 | + .setBirthday(Instant.parse("2000-01-01T00:00:00Z")) |
| 104 | + .build(); |
| 105 | + producer.send(new ProducerRecord<>(topicName, "foo", student)).get(); |
| 106 | + } |
| 107 | + |
| 108 | + startConnector(topicName); |
| 109 | + QuestDBUtils.assertSqlEventually(questDBContainer, "\"firstname\",\"lastname\",\"timestamp\"\r\n" |
| 110 | + + "\"John\",\"Doe\",\"2000-01-01T00:00:00.000000Z\"\r\n", |
| 111 | + "select * from " + topicName); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testSchemaEvolution() throws Exception { |
| 116 | + String topicName = "mytopic"; |
| 117 | + try (Producer<String, Student> producer = new KafkaProducer<>(producerProps())) { |
| 118 | + Student student = Student.newBuilder() |
| 119 | + .setFirstname("John") |
| 120 | + .setLastname("Doe") |
| 121 | + .setBirthday(Instant.parse("2000-01-01T00:00:00Z")) |
| 122 | + .build(); |
| 123 | + producer.send(new ProducerRecord<>(topicName, "foo", student)).get(); |
| 124 | + } |
| 125 | + startConnector(topicName); |
| 126 | + |
| 127 | + QuestDBUtils.assertSqlEventually(questDBContainer, "\"firstname\",\"lastname\",\"timestamp\"\r\n" |
| 128 | + + "\"John\",\"Doe\",\"2000-01-01T00:00:00.000000Z\"\r\n", |
| 129 | + "select * from " + topicName); |
| 130 | + |
| 131 | + try (Producer<String, GenericRecord> producer = new KafkaProducer<>(producerProps())) { |
| 132 | + Schema schema = new org.apache.avro.Schema.Parser().parse(getClass().getResourceAsStream("/avro-runtime/StudentWithExtraColumn.avsc")); |
| 133 | + GenericRecord student = new GenericData.Record(schema); |
| 134 | + student.put("firstname", "Mary"); |
| 135 | + student.put("lastname", "Doe"); |
| 136 | + student.put("birthday", Instant.parse("2005-01-01T00:00:00Z").toEpochMilli()); |
| 137 | + student.put("active", true); |
| 138 | + producer.send(new ProducerRecord<>(topicName, "foo", student)).get(); |
| 139 | + } |
| 140 | + QuestDBUtils.assertSqlEventually(questDBContainer, "\"firstname\",\"lastname\",\"timestamp\",\"active\"\r\n" |
| 141 | + + "\"John\",\"Doe\",\"2000-01-01T00:00:00.000000Z\",false\r\n" |
| 142 | + + "\"Mary\",\"Doe\",\"2005-01-01T00:00:00.000000Z\",true\r\n", |
| 143 | + "select * from " + topicName); |
| 144 | + } |
| 145 | + |
| 146 | + private void startConnector(String topicName) { |
| 147 | + ConnectorConfiguration connector = ConnectorConfiguration.create() |
| 148 | + .with("connector.class", QuestDBSinkConnector.class.getName()) |
| 149 | + .with("tasks.max", "1") |
| 150 | + .with("key.converter", "org.apache.kafka.connect.storage.StringConverter") |
| 151 | + .with("value.converter", "io.confluent.connect.avro.AvroConverter") |
| 152 | + .with("value.converter.schema.registry.url", "http://" + schemaRegistry.getNetworkAliases().get(0) + ":8081") |
| 153 | + .with("topics", topicName) |
| 154 | + .with(QuestDBSinkConnectorConfig.DESIGNATED_TIMESTAMP_COLUMN_NAME_CONFIG, "birthday") |
| 155 | + .with(QuestDBSinkConnectorConfig.INCLUDE_KEY_CONFIG, "false") |
| 156 | + .with("host", questDBContainer.getNetworkAliases().get(0) + ":" + QuestDBUtils.QUESTDB_ILP_PORT); |
| 157 | + connectContainer.registerConnector("my-connector", connector); |
| 158 | + } |
| 159 | + |
| 160 | + @NotNull |
| 161 | + private Properties producerProps() { |
| 162 | + Properties props = new Properties(); |
| 163 | + props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers()); |
| 164 | + props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); |
| 165 | + props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class); |
| 166 | + props.put(KafkaAvroSerializerConfig.SCHEMA_REGISTRY_URL_CONFIG, |
| 167 | + "http://" + schemaRegistry.getHost() + ":" + schemaRegistry.getFirstMappedPort()); |
| 168 | + return props; |
| 169 | + } |
| 170 | +} |
0 commit comments