|
8 | 8 | import io.questdb.kafka.QuestDBSinkTask; |
9 | 9 | import io.questdb.kafka.QuestDBUtils; |
10 | 10 | import io.questdb.kafka.JarResolverExtension; |
11 | | -import org.apache.kafka.clients.consumer.ConsumerConfig; |
12 | | -import org.apache.kafka.clients.consumer.ConsumerRecord; |
13 | | -import org.apache.kafka.clients.consumer.KafkaConsumer; |
14 | | -import org.apache.kafka.common.serialization.StringDeserializer; |
15 | 11 | import org.apache.kafka.connect.json.JsonConverter; |
| 12 | +import org.junit.jupiter.api.AfterEach; |
16 | 13 | import org.junit.jupiter.api.BeforeAll; |
17 | 14 | import org.junit.jupiter.api.Test; |
18 | 15 | import org.junit.jupiter.api.extension.RegisterExtension; |
19 | | -import org.rnorth.ducttape.unreliables.Unreliables; |
20 | 16 | import org.slf4j.LoggerFactory; |
21 | 17 | import org.testcontainers.containers.GenericContainer; |
22 | 18 | import org.testcontainers.containers.KafkaContainer; |
|
33 | 29 | import java.sql.DriverManager; |
34 | 30 | import java.sql.SQLException; |
35 | 31 | import java.sql.Statement; |
36 | | -import java.sql.Time; |
37 | | -import java.time.Duration; |
38 | | -import java.util.ArrayList; |
39 | | -import java.util.Arrays; |
40 | | -import java.util.List; |
41 | | -import java.util.Properties; |
42 | | -import java.util.UUID; |
43 | | -import java.util.concurrent.TimeUnit; |
44 | 32 | import java.util.stream.Stream; |
45 | 33 |
|
46 | 34 | import static org.hamcrest.MatcherAssert.assertThat; |
47 | 35 |
|
48 | 36 | @Testcontainers |
49 | 37 | public class DebeziumIT { |
| 38 | + private static final String PG_SCHEMA_NAME = "test"; |
| 39 | + private static final String PG_TABLE_NAME = "test"; |
| 40 | + private static final String PG_SERVER_NAME = "dbserver1"; |
| 41 | + private static final String DEBEZIUM_CONNECTOR_NAME = "debezium_source"; |
| 42 | + private static final String QUESTDB_CONNECTOR_NAME = "questdb_sink"; |
50 | 43 |
|
51 | 44 | // we need to locate JARs with QuestDB client and Kafka Connect Connector, |
52 | 45 | // this is later used to copy to the Kafka Connect container |
@@ -90,49 +83,85 @@ public static void startContainers() { |
90 | 83 | .join(); |
91 | 84 | } |
92 | 85 |
|
| 86 | + @AfterEach |
| 87 | + public void cleanup() throws SQLException { |
| 88 | + debeziumContainer.deleteAllConnectors(); |
| 89 | + try (Connection connection = getConnection(postgresContainer); |
| 90 | + Statement statement = connection.createStatement()) { |
| 91 | + statement.execute("drop schema " + PG_SCHEMA_NAME + " CASCADE"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static ConnectorConfiguration newQuestSinkBaseConfig(String questTableName) { |
| 96 | + ConnectorConfiguration questSink = ConnectorConfiguration.create() |
| 97 | + .with("connector.class", QuestDBSinkConnector.class.getName()) |
| 98 | + .with("host", questDBContainer.getNetworkAliases().get(0)) |
| 99 | + .with("tasks.max", "1") |
| 100 | + .with("topics", PG_SERVER_NAME + "."+ PG_SCHEMA_NAME + "." + PG_TABLE_NAME) |
| 101 | + .with(QuestDBSinkConnectorConfig.TABLE_CONFIG, questTableName) |
| 102 | + .with("key.converter", JsonConverter.class.getName()) |
| 103 | + .with("value.converter", JsonConverter.class.getName()) |
| 104 | + .with("transforms", "unwrap") |
| 105 | + .with("transforms.unwrap.type", "io.debezium.transforms.ExtractNewRecordState") |
| 106 | + .with(QuestDBSinkConnectorConfig.INCLUDE_KEY_CONFIG, "false"); |
| 107 | + return questSink; |
| 108 | + } |
| 109 | + |
93 | 110 | @Test |
94 | 111 | public void testSmoke() throws Exception { |
95 | | - String tableName = "todo"; |
| 112 | + String questTableName = "test_smoke"; |
96 | 113 | try (Connection connection = getConnection(postgresContainer); |
97 | 114 | Statement statement = connection.createStatement()) { |
98 | 115 |
|
99 | | - statement.execute("create schema todo"); |
100 | | - statement.execute("create table todo.Todo (id int8 not null, " + |
101 | | - "title varchar(255), primary key (id))"); |
102 | | - statement.execute("alter table todo.Todo replica identity full"); |
103 | | - statement.execute("insert into todo.Todo values (1, " + |
104 | | - "'Learn CDC')"); |
105 | | - statement.execute("insert into todo.Todo values (2, " + |
106 | | - "'Learn Debezium')"); |
| 116 | + statement.execute("create schema " + PG_SCHEMA_NAME); |
| 117 | + statement.execute("create table " + PG_SCHEMA_NAME + "." + PG_TABLE_NAME + " (id int8 not null, title varchar(255), primary key (id))"); |
| 118 | + statement.execute("alter table " + PG_SCHEMA_NAME + "." + PG_TABLE_NAME + " replica identity full"); |
| 119 | + statement.execute("insert into " + PG_SCHEMA_NAME + "." + PG_TABLE_NAME + " values (1, 'Learn CDC')"); |
| 120 | + statement.execute("insert into " + PG_SCHEMA_NAME + "." + PG_TABLE_NAME + " values (2, 'Learn Debezium')"); |
107 | 121 |
|
108 | | - ConnectorConfiguration connector = ConnectorConfiguration |
| 122 | + ConnectorConfiguration debeziumSourceConfig = ConnectorConfiguration |
109 | 123 | .forJdbcContainer(postgresContainer) |
110 | | - .with("database.server.name", "dbserver1"); |
| 124 | + .with("database.server.name", PG_SERVER_NAME); |
| 125 | + debeziumContainer.registerConnector(DEBEZIUM_CONNECTOR_NAME, debeziumSourceConfig); |
| 126 | + |
| 127 | + ConnectorConfiguration questSinkConfig = newQuestSinkBaseConfig(questTableName); |
| 128 | + debeziumContainer.registerConnector(QUESTDB_CONNECTOR_NAME, questSinkConfig); |
111 | 129 |
|
| 130 | + QuestDBUtils.assertSqlEventually(questDBContainer, "\"id\",\"title\"\r\n" |
| 131 | + + "1,\"Learn CDC\"\r\n" |
| 132 | + + "2,\"Learn Debezium\"\r\n", "select id, title from " + questTableName); |
| 133 | + } |
| 134 | + } |
112 | 135 |
|
113 | | - debeziumContainer.registerConnector("my-connector", connector); |
| 136 | + @Test |
| 137 | + public void testSchemaChange() throws Exception { |
| 138 | + String questTableName = "test_schema_change"; |
| 139 | + try (Connection connection = getConnection(postgresContainer); |
| 140 | + Statement statement = connection.createStatement()) { |
114 | 141 |
|
115 | | - ConnectorConfiguration questSink = ConnectorConfiguration.create() |
116 | | - .with("connector.class", QuestDBSinkConnector.class.getName()) |
117 | | - .with("host", questDBContainer.getNetworkAliases().get(0)) |
118 | | - .with("tasks.max", "1") |
119 | | - .with("topics", "dbserver1.todo.todo") |
120 | | - .with(QuestDBSinkConnectorConfig.TABLE_CONFIG, tableName) |
121 | | - .with("key.converter", JsonConverter.class.getName()) |
122 | | - .with("value.converter", JsonConverter.class.getName()) |
123 | | - .with("transforms", "unwrap") |
124 | | - .with("transforms.unwrap.type", "io.debezium.transforms.ExtractNewRecordState") |
125 | | - .with(QuestDBSinkConnectorConfig.INCLUDE_KEY_CONFIG, "false"); |
| 142 | + statement.execute("create schema " + PG_SCHEMA_NAME); |
| 143 | + statement.execute("create table " + PG_SCHEMA_NAME + "." + PG_TABLE_NAME + " (id int8 not null, title varchar(255), primary key (id))"); |
| 144 | + statement.execute("alter table " + PG_SCHEMA_NAME + "." + PG_TABLE_NAME + " replica identity full"); |
| 145 | + statement.execute("insert into " + PG_SCHEMA_NAME + "." + PG_TABLE_NAME + " values (1, 'Learn CDC')"); |
| 146 | + statement.execute("alter table " + PG_SCHEMA_NAME + "." + PG_TABLE_NAME + " add column description varchar(255)"); |
| 147 | + statement.execute("insert into " + PG_SCHEMA_NAME + "." + PG_TABLE_NAME + " values (2, 'Learn Debezium', 'Best book ever')"); |
126 | 148 |
|
127 | | - debeziumContainer.registerConnector("questdb-sink", questSink); |
| 149 | + ConnectorConfiguration connector = ConnectorConfiguration |
| 150 | + .forJdbcContainer(postgresContainer) |
| 151 | + .with("database.server.name", PG_SERVER_NAME); |
128 | 152 |
|
| 153 | + debeziumContainer.registerConnector(DEBEZIUM_CONNECTOR_NAME, connector); |
| 154 | + ConnectorConfiguration questSink = newQuestSinkBaseConfig(questTableName); |
| 155 | + debeziumContainer.registerConnector(QUESTDB_CONNECTOR_NAME, questSink); |
129 | 156 |
|
130 | | - QuestDBUtils.assertSqlEventually(questDBContainer, "\"id\",\"title\"\r\n" |
131 | | - + "1,\"Learn CDC\"\r\n" |
132 | | - + "2,\"Learn Debezium\"\r\n", "select id, title from " + tableName); |
| 157 | + QuestDBUtils.assertSqlEventually(questDBContainer, "\"id\",\"title\",\"description\"\r\n" |
| 158 | + + "1,\"Learn CDC\",\r\n" |
| 159 | + + "2,\"Learn Debezium\",\"Best book ever\"\r\n", |
| 160 | + "select id, title, description from " + questTableName); |
133 | 161 | } |
134 | 162 | } |
135 | 163 |
|
| 164 | + |
136 | 165 | private static Connection getConnection( |
137 | 166 | PostgreSQLContainer<?> postgresContainer) |
138 | 167 | throws SQLException { |
|
0 commit comments