HTTP Interface

The HTTP interface lets you use ClickHouse on any platform from any programming language. We use it for working from Java and Perl, as well as shell scripts. In other departments, the HTTP interface is used from Perl, Python, and Go. The HTTP interface is more limited than the native interface, but it has better compatibility.

By default, clickhouse-server listens for HTTP on port 8123 (this can be changed in the config).

If you make a GET / request without parameters, it returns 200 response code and the string which defined in http_server_default_response default value “Ok.” (with a line feed at the end)

  1. $ curl 'http://localhost:8123/'
  2. Ok.

Use GET /ping request in health-check scripts. This handler always return “Ok.” (with a line feed at the end). Available from version 18.12.13.

  1. $ curl 'http://localhost:8123/ping'
  2. Ok.

Send the request as a URL ‘query’ parameter, or as a POST. Or send the beginning of the query in the ‘query’ parameter, and the rest in the POST (we’ll explain later why this is necessary). The size of the URL is limited to 16 KB, so keep this in mind when sending large queries.

If successful, you receive the 200 response code and the result in the response body.
If an error occurs, you receive the 500 response code and an error description text in the response body.

When using the GET method, ‘readonly’ is set. In other words, for queries that modify data, you can only use the POST method. You can send the query itself either in the POST body, or in the URL parameter.

Examples:

  1. $ curl 'http://localhost:8123/?query=SELECT%201'
  2. 1
  3. $ wget -O- -q 'http://localhost:8123/?query=SELECT 1'
  4. 1
  5. $ echo -ne 'GET /?query=SELECT%201 HTTP/1.0\r\n\r\n' | nc localhost 8123
  6. HTTP/1.0 200 OK
  7. Date: Wed, 27 Nov 2019 10:30:18 GMT
  8. Connection: Close
  9. Content-Type: text/tab-separated-values; charset=UTF-8
  10. X-ClickHouse-Server-Display-Name: clickhouse.ru-central1.internal
  11. X-ClickHouse-Query-Id: 5abe861c-239c-467f-b955-8a201abb8b7f
  12. X-ClickHouse-Summary: {"read_rows":"0","read_bytes":"0","written_rows":"0","written_bytes":"0","total_rows_to_read":"0"}
  13. 1

As you can see, curl is somewhat inconvenient in that spaces must be URL escaped.
Although wget escapes everything itself, we don’t recommend using it because it doesn’t work well over HTTP 1.1 when using keep-alive and Transfer-Encoding: chunked.

  1. $ echo 'SELECT 1' | curl 'http://localhost:8123/' --data-binary @-
  2. 1
  3. $ echo 'SELECT 1' | curl 'http://localhost:8123/?query=' --data-binary @-
  4. 1
  5. $ echo '1' | curl 'http://localhost:8123/?query=SELECT' --data-binary @-
  6. 1

If part of the query is sent in the parameter, and part in the POST, a line feed is inserted between these two data parts.
Example (this won’t work):

  1. $ echo 'ECT 1' | curl 'http://localhost:8123/?query=SEL' --data-binary @-
  2. Code: 59, e.displayText() = DB::Exception: Syntax error: failed at position 0: SEL
  3. ECT 1
  4. , expected One of: SHOW TABLES, SHOW DATABASES, SELECT, INSERT, CREATE, ATTACH, RENAME, DROP, DETACH, USE, SET, OPTIMIZE., e.what() = DB::Exception

By default, data is returned in TabSeparated format (for more information, see the “Formats” section).
You use the FORMAT clause of the query to request any other format.

  1. $ echo 'SELECT 1 FORMAT Pretty' | curl 'http://localhost:8123/?' --data-binary @-
  2. ┏━━━┓
  3. 1
  4. ┡━━━┩
  5. 1
  6. └───┘

The POST method of transmitting data is necessary for INSERT queries. In this case, you can write the beginning of the query in the URL parameter, and use POST to pass the data to insert. The data to insert could be, for example, a tab-separated dump from MySQL. In this way, the INSERT query replaces LOAD DATA LOCAL INFILE from MySQL.

Examples: Creating a table:

  1. $ echo 'CREATE TABLE t (a UInt8) ENGINE = Memory' | curl 'http://localhost:8123/' --data-binary @-

Using the familiar INSERT query for data insertion:

  1. $ echo 'INSERT INTO t VALUES (1),(2),(3)' | curl 'http://localhost:8123/' --data-binary @-

Data can be sent separately from the query:

  1. $ echo '(4),(5),(6)' | curl 'http://localhost:8123/?query=INSERT%20INTO%20t%20VALUES' --data-binary @-

You can specify any data format. The ‘Values’ format is the same as what is used when writing INSERT INTO t VALUES:

  1. $ echo '(7),(8),(9)' | curl 'http://localhost:8123/?query=INSERT%20INTO%20t%20FORMAT%20Values' --data-binary @-

To insert data from a tab-separated dump, specify the corresponding format:

  1. $ echo -ne '10\n11\n12\n' | curl 'http://localhost:8123/?query=INSERT%20INTO%20t%20FORMAT%20TabSeparated' --data-binary @-

Reading the table contents. Data is output in random order due to parallel query processing:

  1. $ curl 'http://localhost:8123/?query=SELECT%20a%20FROM%20t'
  2. 7
  3. 8
  4. 9
  5. 10
  6. 11
  7. 12
  8. 1
  9. 2
  10. 3
  11. 4
  12. 5
  13. 6

Deleting the table.

  1. $ echo 'DROP TABLE t' | curl 'http://localhost:8123/' --data-binary @-

For successful requests that don’t return a data table, an empty response body is returned.

You can use the internal ClickHouse compression format when transmitting data. The compressed data has a non-standard format, and you will need to use the special clickhouse-compressor program to work with it (it is installed with the clickhouse-client package). To increase the efficiency of data insertion, you can disable server-side checksum verification by using the http_native_compression_disable_checksumming_on_decompress setting.

If you specified compress=1 in the URL, the server compresses the data it sends you.
If you specified decompress=1 in the URL, the server decompresses the same data that you pass in the POST method.

You can also choose to use HTTP compression. To send a compressed POST request, append the request header Content-Encoding: compression_method. In order for ClickHouse to compress the response, you must append Accept-Encoding: compression_method. ClickHouse supports gzip, br, and deflate compression methods. To enable HTTP compression, you must use the ClickHouse enable_http_compression setting. You can configure the data compression level in the http_zlib_compression_level setting for all the compression methods.

You can use this to reduce network traffic when transmitting a large amount of data, or for creating dumps that are immediately compressed.

Examples of sending data with compression:

  1. #Sending data to the server:
  2. $ curl -vsS "http://localhost:8123/?enable_http_compression=1" -d 'SELECT number FROM system.numbers LIMIT 10' -H 'Accept-Encoding: gzip'
  3. #Sending data to the client:
  4. $ echo "SELECT 1" | gzip -c | curl -sS --data-binary @- -H 'Content-Encoding: gzip' 'http://localhost:8123/'

Note

Some HTTP clients might decompress data from the server by default (with gzip and deflate) and you might get decompressed data even if you use the compression settings correctly.

You can use the ‘database’ URL parameter to specify the default database.

  1. $ echo 'SELECT number FROM numbers LIMIT 10' | curl 'http://localhost:8123/?database=system' --data-binary @-
  2. 0
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
  8. 6
  9. 7
  10. 8
  11. 9

By default, the database that is registered in the server settings is used as the default database. By default, this is the database called ‘default’. Alternatively, you can always specify the database using a dot before the table name.

The username and password can be indicated in one of three ways:

  1. Using HTTP Basic Authentication. Example:
  1. $ echo 'SELECT 1' | curl 'http://user:passwo[email protected]:8123/' -d @-
  1. In the ‘user’ and ‘password’ URL parameters. Example:
  1. $ echo 'SELECT 1' | curl 'http://localhost:8123/?user=user&password=password' -d @-
  1. Using ‘X-ClickHouse-User’ and ‘X-ClickHouse-Key’ headers. Example:
  1. $ echo 'SELECT 1' | curl -H 'X-ClickHouse-User: user' -H 'X-ClickHouse-Key: password' 'http://localhost:8123/' -d @-

If the user name is not specified, the default name is used. If the password is not specified, the empty password is used.
You can also use the URL parameters to specify any settings for processing a single query, or entire profiles of settings. Example:http://localhost:8123/?profile=web&max_rows_to_read=1000000000&query=SELECT+1

For more information, see the Settings section.

  1. $ echo 'SELECT number FROM system.numbers LIMIT 10' | curl 'http://localhost:8123/?' --data-binary @-
  2. 0
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
  8. 6
  9. 7
  10. 8
  11. 9

For information about other parameters, see the section “SET”.

Similarly, you can use ClickHouse sessions in the HTTP protocol. To do this, you need to add the session_id GET parameter to the request. You can use any string as the session ID. By default, the session is terminated after 60 seconds of inactivity. To change this timeout, modify the default_session_timeout setting in the server configuration, or add the session_timeout GET parameter to the request. To check the session status, use the session_check=1 parameter. Only one query at a time can be executed within a single session.

You can receive information about the progress of a query in X-ClickHouse-Progress response headers. To do this, enable send_progress_in_http_headers. Example of the header sequence:

  1. X-ClickHouse-Progress: {"read_rows":"2752512","read_bytes":"240570816","total_rows_to_read":"8880128"}
  2. X-ClickHouse-Progress: {"read_rows":"5439488","read_bytes":"482285394","total_rows_to_read":"8880128"}
  3. X-ClickHouse-Progress: {"read_rows":"8783786","read_bytes":"819092887","total_rows_to_read":"8880128"}

Possible header fields:

  • read_rows — Number of rows read.
  • read_bytes — Volume of data read in bytes.
  • total_rows_to_read — Total number of rows to be read.
  • written_rows — Number of rows written.
  • written_bytes — Volume of data written in bytes.

Running requests don’t stop automatically if the HTTP connection is lost. Parsing and data formatting are performed on the server side, and using the network might be ineffective.
The optional ‘query_id’ parameter can be passed as the query ID (any string). For more information, see the section “Settings, replace_running_query”.

The optional ‘quota_key’ parameter can be passed as the quota key (any string). For more information, see the section “Quotas”.

The HTTP interface allows passing external data (external temporary tables) for querying. For more information, see the section “External data for query processing”.

Response Buffering

You can enable response buffering on the server side. The buffer_size and wait_end_of_query URL parameters are provided for this purpose.

buffer_size determines the number of bytes in the result to buffer in the server memory. If the result body is larger than this threshold, the buffer is written to the HTTP channel, and the remaining data is sent directly to the HTTP channel.

To ensure that the entire response is buffered, set wait_end_of_query=1. In this case, the data that is not stored in memory will be buffered in a temporary server file.

Example:

  1. $ curl -sS 'http://localhost:8123/?max_result_bytes=4000000&buffer_size=3000000&wait_end_of_query=1' -d 'SELECT toUInt8(number) FROM system.numbers LIMIT 9000000 FORMAT RowBinary'

Use buffering to avoid situations where a query processing error occurred after the response code and HTTP headers were sent to the client. In this situation, an error message is written at the end of the response body, and on the client side, the error can only be detected at the parsing stage.

Queries with Parameters

You can create a query with parameters and pass values for them from the corresponding HTTP request parameters. For more information, see Queries with Parameters for CLI.

Example

  1. $ curl -sS "<address>?param_id=2&param_phrase=test" -d "SELECT * FROM table WHERE int_column = {id:UInt8} and string_column = {phrase:String}"