2020-11-06 14:07:56 +00:00
|
|
|
#include <Storages/Kafka/KafkaBlockOutputStream.h>
|
2019-08-20 11:17:57 +00:00
|
|
|
|
|
|
|
#include <Formats/FormatFactory.h>
|
2021-10-11 16:11:50 +00:00
|
|
|
#include <Processors/Formats/IOutputFormat.h>
|
2019-10-22 10:31:28 +00:00
|
|
|
#include <Storages/Kafka/WriteBufferToKafkaProducer.h>
|
2019-08-20 11:17:57 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-07-23 14:25:35 +00:00
|
|
|
KafkaSink::KafkaSink(
|
2020-06-16 12:48:10 +00:00
|
|
|
StorageKafka & storage_,
|
|
|
|
const StorageMetadataPtr & metadata_snapshot_,
|
2021-04-10 23:33:54 +00:00
|
|
|
const ContextPtr & context_)
|
2021-07-23 14:25:35 +00:00
|
|
|
: SinkToStorage(metadata_snapshot_->getSampleBlockNonMaterialized())
|
|
|
|
, storage(storage_)
|
2020-06-16 12:48:10 +00:00
|
|
|
, metadata_snapshot(metadata_snapshot_)
|
|
|
|
, context(context_)
|
2019-08-20 11:17:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-23 14:25:35 +00:00
|
|
|
void KafkaSink::onStart()
|
2019-08-20 11:17:57 +00:00
|
|
|
{
|
2021-09-03 17:29:36 +00:00
|
|
|
buffer = storage.createWriteBuffer(getHeader());
|
2019-08-20 11:17:57 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
auto format_settings = getFormatSettings(context);
|
2021-01-11 01:50:30 +00:00
|
|
|
format_settings.protobuf.allow_multiple_rows_without_delimiter = true;
|
2020-11-02 07:50:38 +00:00
|
|
|
|
2021-10-11 16:11:50 +00:00
|
|
|
format = FormatFactory::instance().getOutputFormat(storage.getFormatName(), *buffer,
|
2021-09-03 17:29:36 +00:00
|
|
|
getHeader(), context,
|
2020-11-02 07:50:38 +00:00
|
|
|
[this](const Columns & columns, size_t row)
|
|
|
|
{
|
|
|
|
buffer->countRow(columns, row);
|
|
|
|
},
|
|
|
|
format_settings);
|
2019-08-20 11:17:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 14:25:35 +00:00
|
|
|
void KafkaSink::consume(Chunk chunk)
|
2019-08-20 11:17:57 +00:00
|
|
|
{
|
2021-10-11 16:11:50 +00:00
|
|
|
format->write(getHeader().cloneWithColumns(chunk.detachColumns()));
|
2019-08-20 11:17:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 14:25:35 +00:00
|
|
|
void KafkaSink::onFinish()
|
2019-08-20 11:17:57 +00:00
|
|
|
{
|
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();
|
2019-08-20 11:17:57 +00:00
|
|
|
|
|
|
|
if (buffer)
|
|
|
|
buffer->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|