Description
TheProtobuf format is the Protocol Buffers format.
This format requires an external format schema, which is cached between queries.
ClickHouse supports:
- both
proto2andproto3syntaxes. Repeated/optional/requiredfields.
_ (underscore) and . (dot) are considered as equal.
If the types of a column and a field of the Protocol Buffers’ message are different, then the necessary conversion is applied.
Nested messages are supported. For example, for the field z in the following message type:
x.y.z (or x_y_z or X.y_Z and so on).
Nested messages are suitable for input or output of a nested data structures.
Default values defined in a protobuf schema like the one that follows are not applied, rather the table defaults are used instead of them:
input_format_protobuf_oneof_presence is set, ClickHouse fills column that indicates which field of oneof was found.
input_format_protobuf_oneof_presence is disabled by default
ClickHouse inputs and outputs protobuf messages in the length-delimited format.
This means that before every message its length should be written as a variable width integer (varint).
Example usage
Reading and writing data
Example filesThe files used in this example are available in the examples repository
protobuf_message.bin into a ClickHouse table. We’ll then write it
back out to a file called protobuf_message_from_clickhouse.bin using the Protobuf format.
Given the file schemafile.proto:
Generating the binary file
Generating the binary file
If you already know how to serialize and deserialize data in the Now, create a new Python file named Now run the script from the command line. It is recommended to run it from a
python virtual environment, for example using You will need to install the following python libraries:Run the script to generate the binary file:
Protobuf format, you can skip this step.We’ll use Python to serialize some data into protobuf_message.bin and read it into ClickHouse.
If there is another language you want to use, see also: “How to read/write length-delimited Protobuf messages in popular languages”.Run the following command to generate a Python file named schemafile_pb2.py in
the same directory as schemafile.proto. This file contains the Python classes
that represent your UserData Protobuf message:generate_protobuf_data.py, in the same
directory as schemafile_pb2.py. Paste the following code into it:uv:Protobuf format:
protobuf_message_from_clickhouse.bin.
Reading and writing data using ClickHouse Cloud
With ClickHouse Cloud you are not able to upload a Protobuf schema file. However, you can use theformat_protobuf_schema
setting to specify the schema in the query. In this example, we show you how to read serialized data from your local
machine and insert it into a table in ClickHouse Cloud.
As in the previous example, create the table according to the schema of your Protobuf schema in ClickHouse Cloud:
format_schema_source defines the source of setting format_schema
Possible values:
- ‘file’ (default): unsupported in Cloud
- ‘string’: The
format_schemais the literal content of the schema. - ‘query’: The
format_schemais a query to retrieve the schema.
format_schema_source='string'
Insert the data into ClickHouse Cloud, specifying the schema as a string, run:
format_schema_source='query'
You can also store your Protobuf schema in a table.
Create a table on ClickHouse Cloud to insert data into:
Using autogenerated schema
If you don’t have an external Protobuf schema for your data, you can still output/input data in the Protobuf format using an autogenerated schema. For this use theformat_protobuf_use_autogenerated_schema setting.
For example:
structureToProtobufSchema. It will then use this schema to serialize data in the Protobuf format.
You can also read a Protobuf file with the autogenerated schema. In this case it is necessary for the file to be created using the same schema:
format_protobuf_use_autogenerated_schema is enabled by default and applies if format_schema is not set.
You can also save autogenerated schema in the file during input/output using setting output_format_schema. For example:
path/to/schema/schema.capnp.
Drop protobuf cache
To reload the Protobuf schema loaded fromformat_schema_path use the SYSTEM DROP ... FORMAT CACHE statement.