Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added connection string to clickhouse-client #50689

Merged
merged 16 commits into from
Jun 14, 2023
Merged
110 changes: 110 additions & 0 deletions docs/en/interfaces/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,116 @@ $ clickhouse-client --param_tuple_in_tuple="(10, ('dt', 10))" -q "SELECT * FROM
$ clickhouse-client --param_tbl="numbers" --param_db="system" --param_col="number" --query "SELECT {col:Identifier} FROM {db:Identifier}.{tbl:Identifier} LIMIT 10"
```

## Connection string {#connection_string}
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

The connection string for clickhouse-client is presented in URI format:
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

```text
clickhouse://[user_info@][hosts_and_ports][/dbname][?query_parameters]
Demilivor marked this conversation as resolved.
Show resolved Hide resolved
```

where user_info is: `user[:password]`
Demilivor marked this conversation as resolved.
Show resolved Hide resolved
and hosts_and_ports is a list of values: `[host][:port],[host][:port]` Port is not mandatory.
Demilivor marked this conversation as resolved.
Show resolved Hide resolved
and query_parameters is a list of parameter[=value]: `param_name[=value]&param_name[=value]...` value may not be required for some of the parameters. Parameter names are case sensitive.

Allowed query_parameters keys:
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

- `secure` or shorthanded `s` - no value. If specified, client will connect to the server over a secure connection (TLS). See `secure` in [command-line-options](#command-line-options)
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

These examples illustrate valid connection strings for clickhouse-client:
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

```text
clickhouse:
clickhouse://localhost
clickhouse://localhost:9000
clickhouse://localhost/default
clickhouse://default@localhost
clickhouse://user:password@localhost
clickhouse://user_name@localhost/some_database?secure
clickhouse://host1:9000,host2:5000/some_database
```

The host component can either be an IP address or a host name. Put an IPv6 address in square brackets to specify it:
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

```text
clickhouse://[2001:db8::1234]
```

If user or/and password are not specified, default values will be used.
Demilivor marked this conversation as resolved.
Show resolved Hide resolved
If host is not specified, the default host will be used (localhost).
Demilivor marked this conversation as resolved.
Show resolved Hide resolved
If port is not specified, the default port will be used (9000).
Demilivor marked this conversation as resolved.
Show resolved Hide resolved
If database is not specified, the default database will be used.

User, password, and database can be specified in the connection string either in `--user`, `--password`, `--database` command line options.
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

The connection string must be specified in the first argument of clickhouse-client. The connection string can be combined with other [command-line-options](#command-line-options) except `--host(h)` and `--port`.
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

### Multiple hosts {#connection_string_multiple_hosts}

URI allows multiple hosts to be connected to, and the client will try to connect to those hosts using the order from URI and command line options. The hosts and ports in the URI accept comma-separated lists of values.
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

If more than one host is supplied, or if a single host name is translated to more than one address, each host and address will be attempted one at a time until one is successful. The remaining hosts after successful connection in the list are not tried.
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

### Percent encoding {#connection_string_uri_percent_encoding}

Hosts, user name, password, database, and query parameters should be [Percent-Encoded](https://en.wikipedia.org/wiki/URL_encoding) if values contain invalid URI characters.
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

### Examples {#connection_string_examples}

Connect to localhost using port 9000 and execute the query "SELECT 1".
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

``` bash
clickhouse-client "clickhouse://localhost:9000" --query "SELECT 1"
Demilivor marked this conversation as resolved.
Show resolved Hide resolved
```

Connect to localhost using port 9000 in interactive, multiline mode.
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

``` bash
clickhouse-client "clickhouse://localhost:9000" -m
Demilivor marked this conversation as resolved.
Show resolved Hide resolved
```

Connect to localhost using port 9000 in interactive mode with the user specified in `--user` option.
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

``` bash
clickhouse-client "clickhouse://localhost:9000" --user default
```

Connect to localhost using port 9000 in interactive mode to `my_database` database specified in the command line option.
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

``` bash
clickhouse-client "clickhouse://localhost:9000" --database my_database
```

Connect to localhost using port 9000 in interactive mode to `my_database` database specified in the connection string.

``` bash
clickhouse-client "clickhouse://localhost:9000/my_database"
```

Connect to localhost using port 9000 in interactive mode to `my_database` database specified in the connection string and a secure connection using shorthanded 's' URI parameter.

```bash
clickhouse-client "clickhouse://localhost/my_database?s"
```

Connect to default host using default port, default user, and default database.

``` bash
clickhouse-client "clickhouse:"
Demilivor marked this conversation as resolved.
Show resolved Hide resolved
```

Connect to the default host using the default port, using user user_name and no password.

``` bash
clickhouse-client "clickhouse://user_name@"
```

Connect to localhost using email as the user name. `@` symbol is percent encoded to `%40`.

``` bash
clickhouse-client "clickhouse://some_user%40some_mail.com@localhost:9000"
```

Demilivor marked this conversation as resolved.
Show resolved Hide resolved
## Configuring {#interfaces_cli_configuration}

You can pass parameters to `clickhouse-client` (all parameters have a default value) using:
Expand Down
111 changes: 111 additions & 0 deletions docs/ru/interfaces/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,117 @@ $ clickhouse-client --param_tuple_in_tuple="(10, ('dt', 10))" -q "SELECT * FROM
$ clickhouse-client --param_tbl="numbers" --param_db="system" --param_col="number" --query "SELECT {col:Identifier} FROM {db:Identifier}.{tbl:Identifier} LIMIT 10"
```

## Строка подключения {#connection_string}

Строка подключения для clickhouse-client представлена в формате URI:

```text
clickhouse://[user_info@][hosts_and_ports][/dbname][?query_parameters]
```

где user_info - это: `user[:password]`
hosts_and_ports - это список значений: `[host][:port],[host][:port]`. Port может быть не задан.
query_parameters - это список пар ключ[=значение]: `param_name[=value]&param_name[=value]...`. Значение может быть пустым.
Имена параметров чувствительны к регистру.

Допустимые ключи query_parameters:

- `secure` или сокращенно `s` - без значение. Если параметр указан, то соединение с сервером будет осуществляться по защищенному каналу (TLS). См. `secure` в [command-line-options](#command-line-options).

Эти примеры иллюстрируют допустимые строки подключения для clickhouse-client:

```text
clickhouse:
clickhouse://localhost
clickhouse://localhost:9000
clickhouse://localhost/default
clickhouse://default@localhost
clickhouse://user:password@localhost
clickhouse://имя_пользователя@localhost/some_database?secure
clickhouse://host1:9000,host2:5000/some_database
```

Параметр host может быть либо IP-адресом, либо именем хоста. Для указания IPv6-адреса поместите его в квадратные скобки:

```text
clickhouse://[2001:db8::1234]
```

Если пользователь или/и пароль не указаны, будут использоваться значения по умолчанию.
Если host не указан, будет использован хост по умолчанию (localhost).
Если port не указан, будет использоваться порт по умолчанию (9000).
Если база данных не указана, будет использоваться база данных по умолчанию (default).

Пользователь, пароль и база данных могут быть указаны в строке подключения либо в опциях командной строки `--user`, `--password`, `--database`.

Строка подключения должна быть указана в первом аргументе clickhouse-client. Строка подключения может комбинироваться с другими [параметрами командной строки] (#command-line-options) кроме `--host (h)` и `--port`.

### Несколько хостов {#connection_string_multiple_hosts}

URI позволяет подключаться к нескольким хостам, и клиент будет пытаться подключиться к этим хостам, используя порядок из URI и опций командной строки. Хосты и порты в URI принимают списки значений, разделенные запятыми.

Если указано более одного хоста или если одно имя хоста транслируется в несколько адресов, Клиент будет будет пытаться подключится к каждому хосту и адресу в порядке в котором они встречаются в URI И опциях клиента, пока не будет установлено соединение. Соединение разрывается, если соединение установлено и аутентификация прошла успешно, остальные хосты в списке игнорируются.

### Кодирование URI {#connection_string_uri_percent_encoding}

Хосты, имя пользователя, пароль, имя базы данных, и параметры запроса должны быть [закодированы](https://ru.wikipedia.org/wiki/URL#%D0%9A%D0%BE%D0%B4%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5_URL), если значения содержат невалидные символы URI.

### Примеры {#connection_string_examples}

Подключиться к localhost через порт 9000 и выполнить запрос "SELECT 1"

``` bash
clickhouse-client "clickhouse://localhost:9000" --query "SELECT 1"
```

Подключиться к localhost через порт 9000 в интерактивном, многострочном режиме.

``` bash
clickhouse-client "clickhouse://localhost:9000" -m
```

Подключиться к localhost через порт 9000 в интерактивном режиме с пользователем default, указанным в опции --user.

``` bash
clickhouse-client "clickhouse://localhost:9000" --user default
```

Подключиться к localhost, используя порт 9000 в интерактивном режиме с базой данных `my_database`, указанной в опции командной строки.

``` bash
clickhouse-client "clickhouse://localhost:9000" --database my_database
```

Подключиться к localhost через порт 9000 в интерактивном режиме с базой данных `my_database`, указанной в строке подключения.

``` bash
clickhouse-client "clickhouse://localhost:9000/my_database"
```

Подключиться к localhost через порт 9000 в интерактивном режиме с базой данных `my_database`, указанной в строке подключения, и безопасным соединением, используя короткий вариант команды URI 's'.

``` bash
clickhouse-client "clickhouse://localhost/my_database?s"
```

Подключиться к хосту по умолчанию с использованием порта по умолчанию, пользователя по умолчанию, и базы данных по умолчанию.

``` bash
clickhouse-client "clickhouse:"
```

Подключиться к хосту по умолчанию через порт по умолчанию, используя имя пользователя user_name без пароля.

``` bash
clickhouse-client "clickhouse://user_name@"
```

Подключиться к localhost, используя электронную почту, как имя пользователя. Символ `@` закодирован как `%40`.

``` bash
clickhouse-client "clickhouse://some_user%40some_mail.com@localhost:9000"
```

## Конфигурирование {#interfaces_cli_configuration}

В `clickhouse-client` можно передавать различные параметры (все параметры имеют значения по умолчанию) с помощью:
Expand Down
13 changes: 11 additions & 2 deletions programs/client/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include <iostream>
#include <iomanip>
#include <optional>
#include <string_view>
#include <Common/scope_guard_safe.h>
#include <boost/program_options.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <filesystem>
#include <string>
#include "Client.h"
#include "Client/ConnectionString.h"
#include "Core/Protocol.h"
#include "Parsers/formatAST.h"

Expand Down Expand Up @@ -1248,6 +1248,9 @@ void Client::readArguments(
std::vector<Arguments> & external_tables_arguments,
std::vector<Arguments> & hosts_and_ports_arguments)
{
bool has_connection_string = argc >= 2 && tryParseConnectionString(std::string_view(argv[1]), common_arguments, hosts_and_ports_arguments);
int start_argument_index = has_connection_string ? 2 : 1;

/** We allow different groups of arguments:
* - common arguments;
* - arguments for any number of external tables each in form "--external args...",
Expand All @@ -1260,7 +1263,7 @@ void Client::readArguments(
std::string prev_host_arg;
std::string prev_port_arg;

for (int arg_num = 1; arg_num < argc; ++arg_num)
for (int arg_num = start_argument_index; arg_num < argc; ++arg_num)
{
std::string_view arg = argv[arg_num];

Expand Down Expand Up @@ -1322,6 +1325,9 @@ void Client::readArguments(
}
else if (arg.starts_with("--host") || arg.starts_with("-h"))
{
if (has_connection_string)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Mixing connection string and --host/--port client arguments is prohibited");
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

std::string host_arg;
/// --host host
if (arg == "--host" || arg == "-h")
Expand Down Expand Up @@ -1353,6 +1359,9 @@ void Client::readArguments(
}
else if (arg.starts_with("--port"))
{
if (has_connection_string)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Mixing connection string and --host/--port client arguments is prohibited");
Demilivor marked this conversation as resolved.
Show resolved Hide resolved

auto port_arg = String{arg};
/// --port port
if (arg == "--port")
Expand Down
Loading
Loading