Skip to content

Commit 5d9f69d

Browse files
authored
Merge pull request #1765 from ClickHouse/clientv2_spring_example
[client-v2] Demo Spring-Boot Application for Client v2
2 parents 9c309e2 + 21588d1 commit 5d9f69d

File tree

16 files changed

+742
-2
lines changed

16 files changed

+742
-2
lines changed

.github/workflows/build.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,20 @@ jobs:
5151
uses: actions/setup-java@v4
5252
with:
5353
distribution: "temurin"
54-
java-version: 8
54+
java-version: |
55+
8
56+
21
5557
cache: "maven"
5658
- name: Build and install libraries
5759
run: mvn --batch-mode --no-transfer-progress --show-version --strict-checksums --threads 2 -Dmaven.wagon.rto=30000 -DclickhouseVersion=$PREFERRED_LTS_VERSION -Dj8 install
5860
- name: Compile examples
5961
run: |
6062
export LIB_VER=$(grep '<revision>' pom.xml | sed -e 's|[[:space:]]*<[/]*revision>[[:space:]]*||g')
6163
find `pwd`/examples -type f -name pom.xml -exec sed -i -e "s|\(<clickhouse-java.version>\).*\(<\)|\1$LIB_VER\2|g" {} \;
62-
for d in $(ls -d `pwd`/examples/*/); do cd $d && mvn --batch-mode --no-transfer-progress clean compile; done
64+
for d in $(ls -d `pwd`/examples/*/); do \
65+
if [ -e $d/pom.xml ]; then cd $d && mvn --batch-mode --no-transfer-progress clean compile; fi;
66+
if [ -e $d/gradlew ]; then cd $d && ./gradlew clean build; fi;
67+
done
6368
- name: Upload test results
6469
uses: actions/upload-artifact@v4
6570
if: failure()
@@ -265,6 +270,7 @@ jobs:
265270
java-version: |
266271
8
267272
17
273+
21
268274
cache: "maven"
269275
- name: Setup Toolchain
270276
shell: bash

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import com.clickhouse.client.api.command.CommandResponse;
88
import com.clickhouse.client.api.command.CommandSettings;
99
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
10+
import com.clickhouse.client.api.data_formats.NativeFormatReader;
11+
import com.clickhouse.client.api.data_formats.RowBinaryFormatReader;
1012
import com.clickhouse.client.api.data_formats.RowBinaryWithNamesAndTypesFormatReader;
13+
import com.clickhouse.client.api.data_formats.RowBinaryWithNamesFormatReader;
1114
import com.clickhouse.client.api.data_formats.internal.MapBackedRecord;
1215
import com.clickhouse.client.api.data_formats.internal.ProcessParser;
1316
import com.clickhouse.client.api.enums.Protocol;
@@ -1361,6 +1364,39 @@ public CompletableFuture<CommandResponse> execute(String sql) {
13611364
}, sharedOperationExecutor);
13621365
}
13631366

1367+
/**
1368+
* <p>Create an instance of {@link ClickHouseBinaryFormatReader} based on response. Table schema is option and only
1369+
* required for {@link ClickHouseFormat#RowBinaryWithNames}, {@link ClickHouseFormat#RowBinary}.
1370+
* Format {@link ClickHouseFormat#RowBinaryWithDefaults} is not supported for output (read operations).</p>
1371+
* @param response
1372+
* @param schema
1373+
* @return
1374+
*/
1375+
public static ClickHouseBinaryFormatReader newBinaryFormatReader(QueryResponse response, TableSchema schema) {
1376+
ClickHouseBinaryFormatReader reader = null;
1377+
switch (response.getFormat()) {
1378+
case Native:
1379+
reader = new NativeFormatReader(response.getInputStream(), response.getSettings());
1380+
break;
1381+
case RowBinaryWithNamesAndTypes:
1382+
reader = new RowBinaryWithNamesAndTypesFormatReader(response.getInputStream(), response.getSettings());
1383+
break;
1384+
case RowBinaryWithNames:
1385+
reader = new RowBinaryWithNamesFormatReader(response.getInputStream(), response.getSettings(), schema);
1386+
break;
1387+
case RowBinary:
1388+
reader = new RowBinaryFormatReader(response.getInputStream(), response.getSettings(), schema);
1389+
break;
1390+
default:
1391+
throw new IllegalArgumentException("Unsupported format: " + response.getFormat());
1392+
}
1393+
return reader;
1394+
}
1395+
1396+
public static ClickHouseBinaryFormatReader newBinaryFormatReader(QueryResponse response) {
1397+
return newBinaryFormatReader(response, null);
1398+
}
1399+
13641400
private String startOperation() {
13651401
String operationId = UUID.randomUUID().toString();
13661402
globalClientStats.put(operationId, new ClientStatisticsHolder());

examples/demo-service/.gitignore

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
HELP.md
2+
.gradle
3+
build/
4+
!gradle/wrapper/gradle-wrapper.jar
5+
!**/src/main/**/build/
6+
!**/src/test/**/build/
7+
8+
### STS ###
9+
.apt_generated
10+
.classpath
11+
.factorypath
12+
.project
13+
.settings
14+
.springBeans
15+
.sts4-cache
16+
bin/
17+
!**/src/main/**/bin/
18+
!**/src/test/**/bin/
19+
20+
### IntelliJ IDEA ###
21+
.idea
22+
*.iws
23+
*.iml
24+
*.ipr
25+
out/
26+
!**/src/main/**/out/
27+
!**/src/test/**/out/
28+
29+
### NetBeans ###
30+
/nbproject/private/
31+
/nbbuild/
32+
/dist/
33+
/nbdist/
34+
/.nb-gradle/
35+
36+
### VS Code ###
37+
.vscode/

examples/demo-service/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# ClickHouse-Java Demo Service
2+
3+
## About
4+
This is an example of a Spring Boot application using different ClickHouse-Java clients and features.
5+
6+
7+
## Usage
8+
9+
This example requires an instance of ClickHouse running locally on in remote server.
10+
Application uses `system.numbers` table to generate dataset of any size. It is very convenient because:
11+
- data is already there
12+
- server generates data as if it was a real table
13+
- no need to change schema or create tables
14+
15+
To run
16+
17+
```bash
18+
./gradlew bootRun
19+
```
20+
21+
To test
22+
23+
```bash
24+
curl http://localhost:8080/direct/dataset/0?limit=100000
25+
```
26+
27+
## Features
28+
29+
- [x] Client V2 New Implementation
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
plugins {
2+
java
3+
id("org.springframework.boot") version "3.3.2"
4+
id("io.spring.dependency-management") version "1.1.6"
5+
}
6+
7+
group = "com.clickhouse"
8+
version = "0.0.1-SNAPSHOT"
9+
10+
java {
11+
toolchain {
12+
languageVersion = JavaLanguageVersion.of(21)
13+
}
14+
}
15+
16+
configurations {
17+
compileOnly {
18+
extendsFrom(configurations.annotationProcessor.get())
19+
}
20+
}
21+
22+
repositories {
23+
mavenLocal() // comment to pull nightly builds instead of local cache
24+
mavenCentral()
25+
maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") // for nightly builds
26+
}
27+
28+
dependencies {
29+
30+
// -- clickhouse dependencies
31+
// Main dependency
32+
implementation("com.clickhouse:client-v2:0.6.3-SNAPSHOT") // nightly build
33+
// implementation("com.clickhouse:client-v2:0.6.3") // stable version
34+
// http client used by clickhouse client
35+
implementation("org.apache.httpcomponents.client5:httpclient5:5.3.1")
36+
// compression dependencies
37+
runtimeOnly("org.apache.commons:commons-compress:1.26.2")
38+
runtimeOnly("org.lz4:lz4-pure-java:1.8.0")
39+
// client V1 if old implementation is needed
40+
// implementation("com.clickhouse:clickhouse-http-client:0.6.3")
41+
42+
43+
44+
45+
// -- application dependencies
46+
implementation("org.springframework.boot:spring-boot-starter-actuator")
47+
implementation("org.springframework.boot:spring-boot-starter-web")
48+
compileOnly("org.projectlombok:lombok")
49+
annotationProcessor("org.projectlombok:lombok")
50+
51+
// -- test dependencies
52+
testImplementation("org.springframework.boot:spring-boot-starter-test")
53+
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
54+
}
55+
56+
tasks.withType<Test> {
57+
useJUnitPlatform()
58+
}
42.4 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)