-
Notifications
You must be signed in to change notification settings - Fork 12
/
ClickhousedbIT.java
218 lines (183 loc) · 8.49 KB
/
ClickhousedbIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package alpakka.clickhousedb;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.clickhouse.ClickHouseContainer;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.JdbcDatabaseContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import scala.jdk.javaapi.FutureConverters;
import javax.sql.DataSource;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
/**
* ClickhouseDB is a read optimized DB
* <p>
* This IT test uses the JDBC client as well as the Scala client
* We keep the test suite running after the last test,
* so we can inspect the DB via the Tabix Browser client
* Doc:
* https://clickhouse.com
* Good overview when to use/NOT to use:
* https://aiven.io/blog/what-is-clickhouse
*/
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@Testcontainers
public class ClickhousedbIT {
private static final Logger LOGGER = LoggerFactory.getLogger(ClickhousedbIT.class);
private static final Integer CLICKHOUSEDB_HTTP_PORT = 8123;
public String searchAfterPattern;
// Doc: https://java.testcontainers.org/modules/databases/clickhouse/
@org.testcontainers.junit.jupiter.Container
public static ClickHouseContainer clickhouseDBContainer = new ClickHouseContainer("clickhouse/clickhouse-server")
.withUsername("test")
.withPassword("test")
.withDatabaseName("test")
.withUrlParam("max_result_rows", "5");
@BeforeAll
public static void setupBeforeClass() throws IOException, InterruptedException {
LOGGER.info("ClickhouseDB container listening on HTTP port: {}. Running: {} ", clickhouseDBContainer.getMappedPort(CLICKHOUSEDB_HTTP_PORT), clickhouseDBContainer.isRunning());
Thread.sleep(1000);
GenericContainer tabixWebClientContainer = new GenericContainer<>(DockerImageName.parse("spoonest/clickhouse-tabix-web-client"))
.withExposedPorts(80)
.withEnv("CH_LOGIN", "test")
.withEnv("CH_PASSWORD", "test")
.withEnv("CH_HOST", "localhost:" + clickhouseDBContainer.getMappedPort(CLICKHOUSEDB_HTTP_PORT));
tabixWebClientContainer.start();
LOGGER.info("Clickhouse Tabix UI listening on HTTP port: {}. Running: {} ", tabixWebClientContainer.getMappedPort(80), tabixWebClientContainer.isRunning());
browserClient(tabixWebClientContainer.getMappedPort(80));
}
@AfterAll
public static void shutdown() throws InterruptedException {
LOGGER.info("Sleep to keep ClickhouseDB instance running...");
Thread.sleep(10000000);
}
@BeforeEach
public void setupBeforeTest(TestInfo testInfo) {
searchAfterPattern = String.format("Starting test: %s", testInfo.getTestMethod().toString());
LOGGER.info(searchAfterPattern);
}
@Test
@Order(1)
void testHealthHTTP() throws IOException, URISyntaxException {
// Doc: https://clickhouse.com/docs/en/interfaces/http
Integer httpPort = clickhouseDBContainer.getMappedPort(CLICKHOUSEDB_HTTP_PORT);
URL url = new URI("http://localhost:" + httpPort + "/ping").toURL();
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
assertThat(con.getResponseCode()).isEqualTo(200);
}
@Test
@Order(2)
void testReadJDBC() throws SQLException {
ResultSet resultSet = performQuery(clickhouseDBContainer, clickhouseDBContainer.getTestQueryString());
int resultSetInt = resultSet.getInt(1);
assertThat(resultSetInt).isEqualTo(1);
}
@Test
@Order(3)
void testWriteJDBC() throws SQLException {
createTable();
String newLine = System.lineSeparator();
String insertDataTextBlock =
"INSERT INTO test.my_table (myfloat_nullable, mystr, myint_id)"
+ newLine
+ "VALUES (1.0, '1', 1), (2.0, '2', 2), (3.0, '3', 3)";
Integer resultInsert = performStatement(clickhouseDBContainer, insertDataTextBlock);
LOGGER.info("Successfully inserted: {} records via JDBC", resultInsert);
dropTable();
assertThat(resultInsert).isEqualTo(3);
}
@Test
@Order(4)
void testReadScala() {
ClickhouseDB instance = ClickhouseDB.apply(clickhouseDBContainer.getMappedPort(CLICKHOUSEDB_HTTP_PORT));
assertThat(instance.testRead()).isEqualTo("1");
}
@Test
@Order(5)
void testWriteReadScalaStreamed() throws SQLException {
createTable();
int noOfRecords = 100;
ClickhouseDB instance = ClickhouseDB.apply(clickhouseDBContainer.getMappedPort(CLICKHOUSEDB_HTTP_PORT));
FutureConverters.asJava(instance.writeAll(noOfRecords)).thenAccept(each ->
{
assertThat(instance.countRows()).isEqualTo(noOfRecords);
assertThat(instance.readAllSource()).isEqualTo(noOfRecords);
assertThat(instance.readAllSourceByteString()).isEqualTo(noOfRecords);
}
);
// since this is the last test we don't drop the table
}
protected ResultSet performQuery(JdbcDatabaseContainer<?> container, String sql) throws SQLException {
DataSource ds = getDataSource(container);
Statement statement = ds.getConnection().createStatement();
statement.execute(sql);
ResultSet resultSet = statement.getResultSet();
resultSet.next();
return resultSet;
}
protected int performStatement(JdbcDatabaseContainer<?> container, String sqlStatement) throws SQLException {
DataSource ds = getDataSource(container);
try (Statement statement = ds.getConnection().createStatement()) {
return statement.executeUpdate(sqlStatement);
}
}
protected DataSource getDataSource(JdbcDatabaseContainer<?> container) {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(container.getJdbcUrl());
hikariConfig.setUsername(container.getUsername());
hikariConfig.setPassword(container.getPassword());
hikariConfig.setDriverClassName(container.getDriverClassName());
return new HikariDataSource(hikariConfig);
}
protected void createTable() throws SQLException {
// Doc: https://clickhouse.com/docs/en/engines/table-engines
String createStatementTextBlock = """
CREATE TABLE test.my_table
(
`myfloat_nullable` Nullable(Float32),
`mystr` String,
`myint_id` Int32
) ENGINE = Log
""";
LOGGER.info(createStatementTextBlock);
LOGGER.info("About to execute CREATE TABLE statement: {}", createStatementTextBlock);
Integer resultCreate = performStatement(clickhouseDBContainer, createStatementTextBlock);
LOGGER.info("Successfully executed CREATE TABLE statement: {}", resultCreate);
}
protected void dropTable() throws SQLException {
String dropStatementTextBlock =
"DROP TABLE test.my_table";
LOGGER.info(dropStatementTextBlock);
LOGGER.info("About to execute DROP TABLE statement: {}", dropStatementTextBlock);
Integer resultCreate = performStatement(clickhouseDBContainer, dropStatementTextBlock);
LOGGER.info("Successfully executed DROP TABLE statement: {}", resultCreate);
}
private static void browserClient(Integer port) throws IOException, InterruptedException {
// We need to wait until the GUI is ready
// If not ready: Hit "Reload structure" in Browser UI to see the table
Thread.sleep(5000);
String os = System.getProperty("os.name").toLowerCase();
String tabuxURL = String.format("http://localhost:%s", port);
if (os.equals("mac os x")) {
String[] cmd = {"open", tabuxURL};
Runtime.getRuntime().exec(cmd);
} else if (os.equals("windows 10")) {
String[] cmd = {"cmd /c start", tabuxURL};
Runtime.getRuntime().exec(cmd);
} else {
LOGGER.info("Please open a browser at: {}", tabuxURL);
}
}
}