ClickHouse/docs/en/interfaces/grpc.md

101 lines
4.7 KiB
Markdown
Raw Normal View History

2021-11-09 18:57:11 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/interfaces/grpc
sidebar_position: 19
sidebar_label: gRPC Interface
2021-11-09 18:57:11 +00:00
---
2022-06-02 10:55:18 +00:00
# gRPC Interface
2021-11-09 18:57:11 +00:00
2021-11-20 09:35:23 +00:00
## Introduction {#grpc-interface-introduction}
2021-11-09 18:57:11 +00:00
2021-11-20 09:35:23 +00:00
ClickHouse supports [gRPC](https://grpc.io/) interface. It is an open source remote procedure call system that uses HTTP/2 and [Protocol Buffers](https://en.wikipedia.org/wiki/Protocol_Buffers). The implementation of gRPC in ClickHouse supports:
2021-11-14 18:11:40 +00:00
2022-06-02 10:55:18 +00:00
- SSL;
- authentication;
- sessions;
- compression;
- parallel queries through the same channel;
- cancellation of queries;
- getting progress and logs;
2021-11-14 18:11:40 +00:00
- external tables.
2021-11-20 09:35:23 +00:00
The specification of the interface is described in [clickhouse_grpc.proto](https://github.com/ClickHouse/ClickHouse/blob/master/src/Server/grpc_protos/clickhouse_grpc.proto).
2021-11-14 18:11:40 +00:00
2021-11-20 09:35:23 +00:00
## gRPC Configuration {#grpc-interface-configuration}
2021-11-14 18:11:40 +00:00
2021-11-21 18:56:08 +00:00
To use the gRPC interface set `grpc_port` in the main [server configuration](../operations/configuration-files.md). Other configuration options see in the following example:
2021-11-14 18:11:40 +00:00
```xml
<grpc_port>9100</grpc_port>
<grpc>
<enable_ssl>false</enable_ssl>
2021-11-20 09:35:23 +00:00
<!-- The following two files are used only if SSL is enabled -->
2021-11-14 18:11:40 +00:00
<ssl_cert_file>/path/to/ssl_cert_file</ssl_cert_file>
<ssl_key_file>/path/to/ssl_key_file</ssl_key_file>
2021-11-20 09:35:23 +00:00
<!-- Whether server requests client for a certificate -->
2021-11-14 18:11:40 +00:00
<ssl_require_client_auth>false</ssl_require_client_auth>
2021-11-20 09:35:23 +00:00
<!-- The following file is used only if ssl_require_client_auth=true -->
2021-11-14 18:11:40 +00:00
<ssl_ca_cert_file>/path/to/ssl_ca_cert_file</ssl_ca_cert_file>
<!-- Default compression algorithm (applied if client doesn't specify another algorithm, see result_compression in QueryInfo).
Supported algorithms: none, deflate, gzip, stream_gzip -->
<compression>deflate</compression>
<!-- Default compression level (applied if client doesn't specify another level, see result_compression in QueryInfo).
Supported levels: none, low, medium, high -->
<compression_level>medium</compression_level>
<!-- Send/receive message size limits in bytes. -1 means unlimited -->
<max_send_message_size>-1</max_send_message_size>
<max_receive_message_size>-1</max_receive_message_size>
2021-11-20 09:35:23 +00:00
<!-- Enable if you want to get detailed logs -->
2021-11-14 18:11:40 +00:00
<verbose_logs>false</verbose_logs>
</grpc>
```
## Built-in Client {#grpc-client}
2021-12-03 19:36:59 +00:00
You can write a client in any of the programming languages supported by gRPC using the provided [specification](https://github.com/ClickHouse/ClickHouse/blob/master/src/Server/grpc_protos/clickhouse_grpc.proto).
2022-06-02 10:55:18 +00:00
Or you can use a built-in Python client. It is placed in [utils/grpc-client/clickhouse-grpc-client.py](https://github.com/ClickHouse/ClickHouse/blob/master/utils/grpc-client/clickhouse-grpc-client.py) in the repository. The built-in client requires [grpcio and grpcio-tools](https://grpc.io/docs/languages/python/quickstart) Python modules.
2021-11-14 18:11:40 +00:00
The client supports the following arguments:
2021-11-14 18:11:40 +00:00
2021-11-20 09:35:23 +00:00
- `--help` Shows a help message and exits.
- `--host HOST, -h HOST` A server name. Default value: `localhost`. You can use IPv4 or IPv6 addresses also.
- `--port PORT` A port to connect to. This port should be enabled in the ClickHouse server configuration (see `grpc_port`). Default value: `9100`.
2021-11-20 09:35:23 +00:00
- `--user USER_NAME, -u USER_NAME` A user name. Default value: `default`.
- `--password PASSWORD` A password. Default value: empty string.
- `--query QUERY, -q QUERY` A query to process when using non-interactive mode.
- `--database DATABASE, -d DATABASE` A default database. If not specified, the current database set in the server settings is used (`default` by default).
- `--format OUTPUT_FORMAT, -f OUTPUT_FORMAT` A result output [format](formats.md). Default value for interactive mode: `PrettyCompact`.
2021-11-20 09:35:23 +00:00
- `--debug` Enables showing debug information.
2021-11-14 18:11:40 +00:00
To run the client in an interactive mode call it without `--query` argument.
In a batch mode query data can be passed via `stdin`.
2021-11-14 18:11:40 +00:00
**Client Usage Example**
2021-11-20 09:35:23 +00:00
In the following example a table is created and loaded with data from a CSV file. Then the content of the table is queried.
2021-11-09 18:57:11 +00:00
``` bash
2021-11-14 18:11:40 +00:00
./clickhouse-grpc-client.py -q "CREATE TABLE grpc_example_table (id UInt32, text String) ENGINE = MergeTree() ORDER BY id;"
2022-10-03 08:54:35 +00:00
echo -e "0,Input data for\n1,gRPC protocol example" > a.csv
cat a.csv | ./clickhouse-grpc-client.py -q "INSERT INTO grpc_example_table FORMAT CSV"
2021-11-20 09:35:23 +00:00
2021-11-14 18:11:40 +00:00
./clickhouse-grpc-client.py --format PrettyCompact -q "SELECT * FROM grpc_example_table;"
```
2021-11-09 18:57:11 +00:00
2021-11-14 18:11:40 +00:00
Result:
``` text
┌─id─┬─text──────────────────┐
│ 0 │ Input data for │
│ 1 │ gRPC protocol example │
└────┴───────────────────────┘
2021-11-09 18:57:11 +00:00
```