Skip to content
Draft
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
115 changes: 115 additions & 0 deletions tests/clickhouse-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# clickhouse-client-cli

A simple CLI tool that mimics `clickhouse-client` for executing SQL queries against a ClickHouse server.
Uses the Java client-v2 API over HTTP, requests `TabSeparated` format, and streams raw output to stdout.

## Build

```bash
cd tests/clickhouse-client
mvn package -DskipTests
```

This produces an executable fat JAR at `target/clickhouse-client-cli-1.0.0.jar`.

## Wrapper executable

A wrapper script named `clickhouse-client` is provided in this directory.

```bash
cd tests/clickhouse-client
./clickhouse-client --help
```

To call it as `clickhouse-client` from anywhere:

```bash
export PATH="$PATH:/home/schernov/workspace01/clickhouse-java/tests/clickhouse-client"
clickhouse-client --help
```

## Usage

Both `--option value` and `--option=value` formats are supported.

### Query via `--query` / `-q`

```bash
./clickhouse-client -q "SELECT uniqExact(number) FROM numbers(1000)"
```

### Query via stdin

Pipe a query:

```bash
echo "SELECT uniqExact(number) FROM numbers(1000)" | ./clickhouse-client
```

Here-string:

```bash
./clickhouse-client <<< "SELECT 1"
```

From a file:

```bash
./clickhouse-client < query.sql
```

If no `--query` is given and nothing is piped, the process blocks waiting for input.
Type your SQL and press `Ctrl+D` (EOF) to execute.

## Options

| Option | Default | Description |
|--------------------|-------------|--------------------------|
| `--host`, `-h` | `localhost` | Server host |
| `--port` | `8123` | HTTP port |
| `--user`, `-u` | `default` | Username |
| `--password` | *(empty)* | Password |
| `--database`, `-d` | `default` | Database |
| `--query`, `-q` | | SQL query to execute |
| `--log_comment` | | Comment for query log |
| `--send_logs_level`| | Send server logs level |
| `--max_insert_threads` | | Server setting passthrough |
| `--multiquery` | | Execute `;`-separated SQL statements |
| `--secure`, `-s` | off | Use HTTPS |
| `--multiline`, `-n`| | Ignored (compatibility) |
| `--help` | | Print usage |

Unknown long options in the form `--name value` / `--name=value` are also accepted and forwarded as ClickHouse server settings.
Compatibility-only options used by tests are accepted but ignored.

## Examples

```bash
# simple select
./clickhouse-client -q "SELECT 1"

# connect to a remote server with credentials
./clickhouse-client \
--host ch.example.com --port 8443 --secure \
--user admin --password secret \
--log_comment "sync-job-42" \
--send_logs_level warning \
-q "SELECT count() FROM system.tables"

# multi-line query from stdin
./clickhouse-client <<'EOF'
SELECT
database,
count() AS table_count
FROM system.tables
GROUP BY database
ORDER BY table_count DESC
EOF

# multiquery from stdin (queries separated by ;)
./clickhouse-client --multiquery <<'EOF'
CREATE TEMPORARY TABLE t (x UInt8);
INSERT INTO t VALUES (1), (2), (3);
SELECT sum(x) FROM t;
EOF
```
39 changes: 39 additions & 0 deletions tests/clickhouse-client/clickhouse
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
JAR_PATH="${SCRIPT_DIR}/target/clickhouse-client-cli-1.0.0.jar"

if [[ "${1:-}" == "extract-from-config" ]]; then
shift
key=""
while [[ $# -gt 0 ]]; do
case "$1" in
--key)
key="${2:-}"
shift 2
;;
--key=*)
key="${1#--key=}"
shift
;;
*)
shift
;;
esac
done

# Minimal compatibility for clickhouse-test usage.
if [[ "${key}" == "listen_host" ]]; then
echo "127.0.0.1"
fi
exit 0
fi

if [[ ! -f "${JAR_PATH}" ]]; then
echo "Jar not found: ${JAR_PATH}" >&2
echo "Build it first: (cd ${SCRIPT_DIR} && mvn package -DskipTests)" >&2
exit 1
fi

exec java -jar "${JAR_PATH}" "$@"
13 changes: 13 additions & 0 deletions tests/clickhouse-client/clickhouse-client
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
JAR_PATH="${SCRIPT_DIR}/target/clickhouse-client-cli-1.0.0.jar"

if [[ ! -f "${JAR_PATH}" ]]; then
echo "Jar not found: ${JAR_PATH}" >&2
echo "Build it first: (cd ${SCRIPT_DIR} && mvn package -DskipTests)" >&2
exit 1
fi

exec java -jar "${JAR_PATH}" "$@"
92 changes: 92 additions & 0 deletions tests/clickhouse-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-client-cli</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>clickhouse-client-cli</name>
<description>Simple CLI tool that mimics clickhouse-client for executing SQL queries</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<clickhouse-java.version>0.9.6-SNAPSHOT</clickhouse-java.version>
<main.class>com.clickhouse.client.cli.Main</main.class>
</properties>

<dependencies>
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>client-v2</artifactId>
<version>${clickhouse-java.version}</version>
<classifier>all</classifier>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.13</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.13</version>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>${main.class}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${main.class}</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading