ClickHouse/src/Storages/Kafka/KafkaBlockSink.cpp

53 lines
1.2 KiB
C++
Raw Normal View History

2022-05-09 19:13:02 +00:00
#include <Storages/Kafka/KafkaSink.h>
#include <Formats/FormatFactory.h>
2021-10-11 16:11:50 +00:00
#include <Processors/Formats/IOutputFormat.h>
#include <Storages/Kafka/WriteBufferToKafkaProducer.h>
namespace DB
{
2021-07-23 14:25:35 +00:00
KafkaSink::KafkaSink(
StorageKafka & storage_,
const StorageMetadataPtr & metadata_snapshot_,
const ContextPtr & context_)
2021-07-23 14:25:35 +00:00
: SinkToStorage(metadata_snapshot_->getSampleBlockNonMaterialized())
, storage(storage_)
, metadata_snapshot(metadata_snapshot_)
, context(context_)
{
}
2021-07-23 14:25:35 +00:00
void KafkaSink::onStart()
{
buffer = storage.createWriteBuffer(getHeader());
auto format_settings = getFormatSettings(context);
format_settings.protobuf.allow_multiple_rows_without_delimiter = true;
2021-10-11 16:11:50 +00:00
format = FormatFactory::instance().getOutputFormat(storage.getFormatName(), *buffer,
getHeader(), context,
[this](const Columns & columns, size_t row)
{
buffer->countRow(columns, row);
},
format_settings);
}
2021-07-23 14:25:35 +00:00
void KafkaSink::consume(Chunk chunk)
{
2021-10-11 16:11:50 +00:00
format->write(getHeader().cloneWithColumns(chunk.detachColumns()));
}
2021-07-23 14:25:35 +00:00
void KafkaSink::onFinish()
{
2021-10-11 16:11:50 +00:00
if (format)
2021-11-11 18:09:21 +00:00
format->finalize();
2021-07-23 14:25:35 +00:00
//flush();
if (buffer)
buffer->flush();
}
}