Skip to content

Commit 45c95d3

Browse files
committed
Add CLI client
1 parent c22cdc1 commit 45c95d3

File tree

13 files changed

+965
-28
lines changed

13 files changed

+965
-28
lines changed

clickhouse-benchmark/src/main/java/com/clickhouse/benchmark/misc/StreamBenchmark.java

-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ public void wrapped(StreamState state, Blackhole consumer) throws IOException {
140140
if ((count = in.pipe(out)) != state.samples) {
141141
throw new IllegalStateException(String.format("Expect %d bytes but got %d", size, count));
142142
}
143-
out.flush();
144143
}
145144
if (!Arrays.equals(state.bytes, bao.toByteArray())) {
146145
throw new IllegalStateException("Incorrect result");
@@ -157,7 +156,6 @@ public void async(StreamState state, Blackhole consumer) throws IOException {
157156
if ((count = in.pipe(out)) != state.samples) {
158157
throw new IllegalStateException(String.format("Expect %d bytes but got %d", size, count));
159158
}
160-
out.flush();
161159
}
162160
if (!Arrays.equals(state.bytes, bao.toByteArray())) {
163161
throw new IllegalStateException("Incorrect result");

clickhouse-cli-client/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# ClickHouse Command-line Client
2+
3+
This is a wrapper of ClickHouse native command-line client. In order to use it, please make sure 1) either the native command-line client or docker is installed; and 2) `clickhouse_cli_path` or `docker_cli_path` is configured properly.
4+
5+
Unlike `clickhouse-http-client`, this module is not designed for dealing with many queries in short period of time, because it uses sub-process(NOT thread) and file-based streaming. Having said that, it provides an alternative, usually faster,way to dump and load large data sets. Besides, due to its simplicity, it can be used as an example to demonstrate how to implement SPI defined in `clickhouse-client`.
6+
7+
## Limitations and Known Issues
8+
9+
- Only `max_result_rows` and `result_overflow_mode` two settings are currently supported
10+
- ClickHouseResponseSummary is always empty - see ClickHouse/ClickHouse#37241
11+
- Session is not supported and query cannot be cancelled - see ClickHouse/ClickHouse#37308
12+
13+
## Maven Dependency
14+
15+
```xml
16+
<dependency>
17+
<!-- will stop using ru.yandex.clickhouse starting from 0.4.0 -->
18+
<groupId>com.clickhouse</groupId>
19+
<artifactId>clickhouse-cli-client</artifactId>
20+
<version>0.3.2-patch9</version>
21+
</dependency>
22+
```
23+
24+
## Examples
25+
26+
```java
27+
// make sure 'clickhouse-client' or 'docker' is in PATH before you start the program
28+
// alternatively, configure CLI path in either Java system property or environment variable, for examples:
29+
// CHC_CLICKHOUSE_CLI_PATH=/path/to/clickhouse-client CHC_DOCKER_CLI_PATH=/path/to/docker java MyProgram
30+
// java -Dclickhouse_cli_path=/path/to/clickhouse-client -Ddocker_cli_path=/path/to/docker MyProgram
31+
32+
// clickhouse-cli-client uses TCP protocol
33+
ClickHouseProtocol preferredProtocol = ClickHouseProtocol.TCP;
34+
// connect to my-server, use default port(9000) of TCP/native protocol
35+
ClickHouseNode server = ClickHouseNode.builder().host("my-server").port(preferredProtocol).build();
36+
37+
// declares a file
38+
ClickHouseFile file = ClickHouseFile.of("data.csv");
39+
40+
// dump query results into the file - format is TSV, according to file extension
41+
ClickHouseClient.dump(server, "select * from some_table", file).get();
42+
43+
// now load it into my_table, using TSV format
44+
ClickHouseClient.load(server, "my_table", file).get();
45+
46+
// it can be used in the same as any other client
47+
try (ClickHouseClient client = ClickHouseClient.newInstance(preferredProtocol);
48+
ClickHouseResponse response = client.connect(server)
49+
.query("select * from numbers(:limit)")
50+
.params(1000).executeAndWait()) {
51+
for (ClickHouseRecord r : response.records()) {
52+
int num = r.getValue(0).asInteger();
53+
String str = r.getValue(0).asString();
54+
}
55+
}
56+
```

clickhouse-cli-client/pom.xml

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
4+
<parent>
5+
<groupId>com.clickhouse</groupId>
6+
<artifactId>clickhouse-java</artifactId>
7+
<version>${revision}</version>
8+
</parent>
9+
10+
<artifactId>clickhouse-cli-client</artifactId>
11+
<version>${revision}</version>
12+
<packaging>jar</packaging>
13+
14+
<name>${project.artifactId}</name>
15+
<description>Wrapper of ClickHouse native command-line client</description>
16+
<url>https://github.com/ClickHouse/clickhouse-jdbc/tree/master/clickhouse-cli-client</url>
17+
18+
<properties>
19+
<shade.base>${project.parent.groupId}.client.internal</shade.base>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>${project.parent.groupId}</groupId>
25+
<artifactId>clickhouse-client</artifactId>
26+
<version>${revision}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.lz4</groupId>
30+
<artifactId>lz4-java</artifactId>
31+
</dependency>
32+
33+
<!-- necessary for Java 9+ -->
34+
<dependency>
35+
<groupId>org.apache.tomcat</groupId>
36+
<artifactId>annotations-api</artifactId>
37+
<scope>provided</scope>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>${project.parent.groupId}</groupId>
42+
<artifactId>clickhouse-client</artifactId>
43+
<version>${revision}</version>
44+
<type>test-jar</type>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.slf4j</groupId>
49+
<artifactId>slf4j-simple</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.testcontainers</groupId>
54+
<artifactId>testcontainers</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.github.tomakehurst</groupId>
59+
<artifactId>wiremock-jre8</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.testng</groupId>
64+
<artifactId>testng</artifactId>
65+
<scope>test</scope>
66+
</dependency>
67+
</dependencies>
68+
69+
<build>
70+
<plugins>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-shade-plugin</artifactId>
74+
<executions>
75+
<execution>
76+
<id>shade</id>
77+
<phase>package</phase>
78+
<goals>
79+
<goal>shade</goal>
80+
</goals>
81+
<configuration>
82+
<shadedArtifactAttached>true</shadedArtifactAttached>
83+
<createDependencyReducedPom>true</createDependencyReducedPom>
84+
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
85+
<shadedClassifierName>shaded</shadedClassifierName>
86+
<relocations>
87+
<relocation>
88+
<pattern>net.jpountz</pattern>
89+
<shadedPattern>${shade.base}.jpountz</shadedPattern>
90+
</relocation>
91+
</relocations>
92+
<transformers>
93+
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
94+
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer" />
95+
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
96+
</transformers>
97+
<filters>
98+
<filter>
99+
<artifact>*:*</artifact>
100+
<excludes>
101+
<exclude>**/darwin/**</exclude>
102+
<exclude>**/linux/**</exclude>
103+
<exclude>**/win32/**</exclude>
104+
<exclude>**/module-info.class</exclude>
105+
<exclude>META-INF/MANIFEST.MF</exclude>
106+
<exclude>META-INF/maven/**</exclude>
107+
<exclude>META-INF/native-image/**</exclude>
108+
</excludes>
109+
</filter>
110+
</filters>
111+
</configuration>
112+
</execution>
113+
</executions>
114+
</plugin>
115+
<plugin>
116+
<groupId>org.apache.maven.plugins</groupId>
117+
<artifactId>maven-compiler-plugin</artifactId>
118+
</plugin>
119+
</plugins>
120+
</build>
121+
</project>

0 commit comments

Comments
 (0)