1 | #pragma once |
---|---|
2 | |
3 | #include "config_formats.h" |
4 | #if USE_PROTOBUF |
5 | |
6 | #include <Core/Block.h> |
7 | #include <Formats/FormatSettings.h> |
8 | #include <Formats/ProtobufWriter.h> |
9 | #include <Formats/FormatSchemaInfo.h> |
10 | #include <Processors/Formats/IRowOutputFormat.h> |
11 | |
12 | |
13 | namespace google |
14 | { |
15 | namespace protobuf |
16 | { |
17 | class Message; |
18 | } |
19 | } |
20 | |
21 | |
22 | namespace DB |
23 | { |
24 | /** Stream designed to serialize data in the google protobuf format. |
25 | * Each row is written as a separated message. |
26 | * These messages are delimited according to documentation |
27 | * https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/util/delimited_message_util.h |
28 | * Serializing in the protobuf format requires the 'format_schema' setting to be set, e.g. |
29 | * SELECT * from table FORMAT Protobuf SETTINGS format_schema = 'schema:Message' |
30 | * where schema is the name of "schema.proto" file specifying protobuf schema. |
31 | */ |
32 | class ProtobufRowOutputFormat : public IRowOutputFormat |
33 | { |
34 | public: |
35 | ProtobufRowOutputFormat( |
36 | WriteBuffer & out_, |
37 | const Block & header, |
38 | FormatFactory::WriteCallback callback, |
39 | const FormatSchemaInfo & format_schema); |
40 | |
41 | String getName() const override { return "ProtobufRowOutputFormat"; } |
42 | |
43 | void write(const Columns & columns, size_t row_num) override; |
44 | void writeField(const IColumn &, const IDataType &, size_t) override {} |
45 | std::string getContentType() const override { return "application/octet-stream"; } |
46 | |
47 | private: |
48 | DataTypes data_types; |
49 | ProtobufWriter writer; |
50 | std::vector<size_t> value_indices; |
51 | }; |
52 | |
53 | } |
54 | #endif |
55 |