Skip to content

Commit 966331b

Browse files
codebase/introduction-to-clickhouse-database [BAEL-8899] (#18063)
* add module skeleton * add db migration and entity class * add live test * organize imports * exteralize testcontainers configuration * enable testcontainers at development support * downgrade java version to 21 * add logback-spring.xml
1 parent 7b4f48b commit 966331b

File tree

9 files changed

+248
-0
lines changed

9 files changed

+248
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>clickhouse</artifactId>
7+
<version>0.0.1</version>
8+
<packaging>jar</packaging>
9+
<name>clickhouse</name>
10+
<description>codebase demonstrating the integration of ClickHouse database in Spring Boot to perform CRUD operations.</description>
11+
12+
<parent>
13+
<groupId>com.baeldung</groupId>
14+
<artifactId>parent-boot-3</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
<relativePath>../../parent-boot-3</relativePath>
17+
</parent>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-jdbc</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.clickhouse</groupId>
31+
<artifactId>clickhouse-jdbc</artifactId>
32+
<version>${clickhouse.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.lz4</groupId>
36+
<artifactId>lz4-java</artifactId>
37+
<version>${lz4.version}</version>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.flywaydb</groupId>
42+
<artifactId>flyway-core</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.flywaydb</groupId>
46+
<artifactId>flyway-database-clickhouse</artifactId>
47+
<version>${flyway-clickhouse.version}</version>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-test</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework.boot</groupId>
57+
<artifactId>spring-boot-testcontainers</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.testcontainers</groupId>
62+
<artifactId>clickhouse</artifactId>
63+
<scope>test</scope>
64+
</dependency>
65+
</dependencies>
66+
67+
<build>
68+
<plugins>
69+
<plugin>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-maven-plugin</artifactId>
72+
</plugin>
73+
<plugin>
74+
<groupId>org.apache.maven.plugins</groupId>
75+
<artifactId>maven-compiler-plugin</artifactId>
76+
<configuration>
77+
<source>21</source>
78+
<target>21</target>
79+
</configuration>
80+
</plugin>
81+
</plugins>
82+
</build>
83+
84+
<properties>
85+
<lz4.version>1.8.0</lz4.version>
86+
<clickhouse.version>0.7.1</clickhouse.version>
87+
<spring-boot.version>3.4.0</spring-boot.version>
88+
<flyway-clickhouse.version>10.16.3</flyway-clickhouse.version>
89+
</properties>
90+
91+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.clickhouse;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.clickhouse;
2+
3+
import java.time.LocalDateTime;
4+
import java.util.UUID;
5+
6+
public record Author(
7+
UUID id,
8+
String name,
9+
String email,
10+
LocalDateTime createdAt) {
11+
12+
public static Author create(String name, String email) {
13+
return new Author(
14+
UUID.randomUUID(),
15+
name,
16+
email,
17+
LocalDateTime.now()
18+
);
19+
}
20+
21+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE authors (
2+
id UUID,
3+
name String,
4+
email String,
5+
created_at DateTime
6+
)
7+
ENGINE = MergeTree()
8+
PRIMARY KEY id;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<configuration>
2+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c{1}] - %m%n</pattern>
5+
</encoder>
6+
</appender>
7+
8+
<root level="INFO">
9+
<appender-ref ref="CONSOLE" />
10+
</root>
11+
12+
<logger name="org.springframework" level="INFO" additivity="false">
13+
<appender-ref ref="CONSOLE" />
14+
</logger>
15+
</configuration>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.clickhouse;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.context.annotation.Import;
7+
import org.springframework.jdbc.core.JdbcTemplate;
8+
9+
import java.sql.ResultSet;
10+
import java.time.LocalDateTime;
11+
import java.util.List;
12+
import java.util.UUID;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
16+
@SpringBootTest
17+
@Import(TestcontainersConfiguration.class)
18+
class ClickHouseCrudLiveTest {
19+
20+
@Autowired
21+
private JdbcTemplate jdbcTemplate;
22+
23+
@Test
24+
void whenAuthorSaved_thenDatabaseContainsRecord() {
25+
Author author = Author.create("John Doe", "[email protected]");
26+
27+
jdbcTemplate.update(
28+
"""
29+
INSERT INTO authors (id, name, email, created_at)
30+
VALUES (?, ?, ?, ?);
31+
""",
32+
author.id(),
33+
author.name(),
34+
author.email(),
35+
author.createdAt()
36+
);
37+
38+
List<Author> retrievedAuthors = jdbcTemplate.query(
39+
"SELECT * FROM authors WHERE id = ?",
40+
(ResultSet resultSet, int rowNum) -> new Author(
41+
UUID.fromString(resultSet.getString("id")),
42+
resultSet.getString("name"),
43+
resultSet.getString("email"),
44+
resultSet.getObject("created_at", LocalDateTime.class)
45+
),
46+
author.id()
47+
);
48+
49+
assertThat(retrievedAuthors)
50+
.hasSize(1)
51+
.first()
52+
.satisfies(retrievedAuthor -> {
53+
assertThat(retrievedAuthor.id()).isEqualTo(author.id());
54+
assertThat(retrievedAuthor.name()).isEqualTo(author.name());
55+
assertThat(retrievedAuthor.email()).isEqualTo(author.email());
56+
assertThat(retrievedAuthor.createdAt()).isNotNull();
57+
});
58+
}
59+
60+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.clickhouse;
2+
3+
import org.springframework.boot.SpringApplication;
4+
5+
class TestApplication {
6+
7+
public static void main(String[] args) {
8+
SpringApplication.from(Application::main)
9+
.with(TestcontainersConfiguration.class)
10+
.run(args);
11+
}
12+
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.clickhouse;
2+
3+
import org.springframework.boot.test.context.TestConfiguration;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.test.context.DynamicPropertyRegistrar;
6+
import org.testcontainers.clickhouse.ClickHouseContainer;
7+
8+
@TestConfiguration(proxyBeanMethods = false)
9+
class TestcontainersConfiguration {
10+
11+
@Bean
12+
public ClickHouseContainer clickhouseContainer() {
13+
return new ClickHouseContainer("clickhouse/clickhouse-server:24.11");
14+
}
15+
16+
@Bean
17+
public DynamicPropertyRegistrar dynamicPropertyRegistrar(ClickHouseContainer clickhouseContainer) {
18+
return registry -> {
19+
registry.add("spring.datasource.url", clickhouseContainer::getJdbcUrl);
20+
registry.add("spring.datasource.username", clickhouseContainer::getUsername);
21+
registry.add("spring.datasource.password", clickhouseContainer::getPassword);
22+
registry.add("spring.datasource.driver-class-name", clickhouseContainer::getDriverClassName);
23+
};
24+
}
25+
26+
}

persistence-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<module>apache-derby</module>
2121
<module>atomikos</module>
2222
<module>blaze-persistence</module>
23+
<module>clickhouse</module>
2324
<module>core-java-persistence</module>
2425
<module>core-java-persistence-2</module>
2526
<module>core-java-persistence-3</module>

0 commit comments

Comments
 (0)