Protobuf
Input | Output | Alias |
---|---|---|
✔ | ✔ |
Description
The Protobuf
format is the Protocol Buffers format.
This format requires an external format schema, which is cached between queries.
ClickHouse supports:
- both
proto2
andproto3
syntaxes. Repeated
/optional
/required
fields.
Example usage
Basic examples
Usage examples:
Where the file schemafile.proto
looks like this:
To find the correspondence between table columns and fields of the Protocol Buffers' message type, ClickHouse compares their names.
This comparison is case-insensitive and the characters _
(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:
ClickHouse tries to find a column named 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:
If a message contains oneof and input_format_protobuf_oneof_presence
is set, ClickHouse fills column that indicates which field of oneof was found.
Name of the column that indicates presence must be the same as the name of oneof. Nested messages are supported (see basic-examples). Allowed types are Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Enum, Enum8 or Enum16. Enum (as well as Enum8 or Enum16) must contain all oneof' possible tags plus 0 to indicate absence, string representations does not matter.
The setting 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).
See also: how to read/write length-delimited protobuf messages in popular languages.
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 example:
In this case, ClickHouse will autogenerate the Protobuf schema according to the table structure using function 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:
The setting 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:
In this case autogenerated Protobuf schema will be saved in file path/to/schema/schema.capnp
.
Drop protobuf cache
To reload the Protobuf schema loaded from format_schema_path
use the SYSTEM DROP ... FORMAT CACHE
statement.