Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ public void wrapped(StreamState state, Blackhole consumer) throws IOException {
if ((count = in.pipe(out)) != state.samples) {
throw new IllegalStateException(String.format("Expect %d bytes but got %d", size, count));
}
out.flush();
}
if (!Arrays.equals(state.bytes, bao.toByteArray())) {
throw new IllegalStateException("Incorrect result");
Expand All @@ -157,7 +156,6 @@ public void async(StreamState state, Blackhole consumer) throws IOException {
if ((count = in.pipe(out)) != state.samples) {
throw new IllegalStateException(String.format("Expect %d bytes but got %d", size, count));
}
out.flush();
}
if (!Arrays.equals(state.bytes, bao.toByteArray())) {
throw new IllegalStateException("Incorrect result");
Expand Down
71 changes: 71 additions & 0 deletions clickhouse-cli-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# ClickHouse Command-line Client

This is a thin wrapper of ClickHouse native command-line client. It provides an alternative way to communicate with ClickHouse, which might be of use when you prefer:

- TCP/native protocol over HTTP or gRPC
- native CLI client instead of pure Java implementation
- an example of implementing SPI defined in `clickhouse-client` module

Either [clickhouse-client](https://clickhouse.com/docs/en/interfaces/cli/) or [docker](https://docs.docker.com/get-docker/) must be installed prior to use. And it's important to understand that this module uses sub-process(in addition to threads) and file-based streaming, meaning 1) it's not as fast as native CLI client or pure Java implementation, although it's close in the case of dumping and loading data; and 2) it's not suitable for scenarios like dealing with many queries in short period of time.

## Limitations and Known Issues

- Only `max_result_rows` and `result_overflow_mode` two settings are currently supported
- ClickHouseResponseSummary is always empty - see ClickHouse/ClickHouse#37241
- Session is not supported - see ClickHouse/ClickHouse#37308

## Maven Dependency

```xml
<dependency>
<!-- will stop using ru.yandex.clickhouse starting from 0.4.0 -->
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-cli-client</artifactId>
<version>0.3.2-patch9</version>
</dependency>
```

## Examples

```java
// make sure 'clickhouse-client' or 'docker' is in PATH before you start the program
// alternatively, configure CLI path in either Java system property or environment variable, for examples:
// CHC_CLICKHOUSE_CLI_PATH=/path/to/clickhouse-client CHC_DOCKER_CLI_PATH=/path/to/docker java MyProgram
// java -Dchc_clickhouse_cli_path=/path/to/clickhouse-client -Dchc_docker_cli_path=/path/to/docker MyProgram

// clickhouse-cli-client uses TCP protocol
ClickHouseProtocol preferredProtocol = ClickHouseProtocol.TCP;
// connect to my-server, use default port(9000) of TCP/native protocol
ClickHouseNode server = ClickHouseNode.builder().host("my-server").port(preferredProtocol).build();

// declares a file
ClickHouseFile file = ClickHouseFile.of("data.csv");

// dump query results into the file - format is CSV, according to file extension
ClickHouseClient.dump(server, "select * from some_table", file).get();

// now load it into my_table, using CSV format
ClickHouseClient.load(server, "my_table", file).get();

// it can be used in the same way as any other client
try (ClickHouseClient client = ClickHouseClient.newInstance(preferredProtocol);
ClickHouseResponse response = client.connect(server)
.query("select * from numbers(:limit)")
.params(1000).executeAndWait()) {
for (ClickHouseRecord r : response.records()) {
int num = r.getValue(0).asInteger();
String str = r.getValue(0).asString();
}
}

// and of course it's part of JDBC driver
try (Connection conn = DriverManager.getConnect("jdbc:ch:tcp://my-server", "default", "");
PreparedStatement stmt = conn.preparedStatement("select * from numbers(?)")) {
stmt.setInt(1, 1000);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
int num = rs.getInt(1);
String str = rs.getString(1);
}
}
```
121 changes: 121 additions & 0 deletions clickhouse-cli-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-java</artifactId>
<version>${revision}</version>
</parent>

<artifactId>clickhouse-cli-client</artifactId>
<version>${revision}</version>
<packaging>jar</packaging>

<name>${project.artifactId}</name>
<description>Wrapper of ClickHouse native command-line client</description>
<url>https://github.com/ClickHouse/clickhouse-jdbc/tree/master/clickhouse-cli-client</url>

<properties>
<shade.base>${project.parent.groupId}.client.internal</shade.base>
</properties>

<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>clickhouse-client</artifactId>
<version>${revision}</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- necessary for Java 9+ -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>clickhouse-client</artifactId>
<version>${revision}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>shade</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<createDependencyReducedPom>true</createDependencyReducedPom>
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
<shadedClassifierName>shaded</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/darwin/**</exclude>
<exclude>**/linux/**</exclude>
<exclude>**/win32/**</exclude>
<exclude>**/module-info.class</exclude>
<exclude>META-INF/MANIFEST.MF</exclude>
<exclude>META-INF/maven/**</exclude>
<exclude>META-INF/native-image/**</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<environmentVariables>
<CHC_TEST_CONTAINER_ID>clickhouse-cli-client</CHC_TEST_CONTAINER_ID>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
Loading