ClickHouse/dbms/include/DB/DataStreams/RemoteBlockOutputStream.h

113 lines
3.4 KiB
C
Raw Normal View History

2012-05-21 20:38:34 +00:00
#pragma once
#include <DB/DataStreams/IBlockOutputStream.h>
#include <DB/Client/Connection.h>
2014-07-04 11:21:53 +00:00
#include <Yandex/logger_useful.h>
2012-05-21 20:38:34 +00:00
#include <statdaemons/NetException.h>
2012-05-21 20:38:34 +00:00
namespace DB
{
/** Позволяет выполнить запрос INSERT на удалённом сервере и отправить данные.
*/
class RemoteBlockOutputStream : public IBlockOutputStream
{
public:
RemoteBlockOutputStream(Connection & connection_, const String & query_, const Settings * settings_ = nullptr)
: connection(connection_), query(query_), settings(settings_)
2012-05-21 20:38:34 +00:00
{
}
/// Можно вызывать после writePrefix, чтобы получить структуру таблицы.
Block getSampleBlock() const
2012-05-21 20:38:34 +00:00
{
return sample_block;
}
void writePrefix() override
{
/** Отправляет запрос и получает блок-пример, описывающий структуру таблицы.
* Он нужен, чтобы знать, какие блоки передавать в метод write.
*/
connection.sendQuery(query, "", QueryProcessingStage::Complete, settings);
2012-05-21 20:38:34 +00:00
Connection::Packet packet = connection.receivePacket();
if (Protocol::Server::Data == packet.type)
{
sample_block = packet.block;
if (!sample_block)
throw Exception("Logical error: empty block received as table structure", ErrorCodes::LOGICAL_ERROR);
}
2012-06-19 22:46:02 +00:00
else if (Protocol::Server::Exception == packet.type)
{
packet.exception->rethrow();
return;
2012-06-19 22:46:02 +00:00
}
2012-05-21 20:38:34 +00:00
else
throw NetException("Unexpected packet from server (expected Data or Exception, got "
+ String(Protocol::Server::toString(packet.type)) + ")", ErrorCodes::UNEXPECTED_PACKET_FROM_SERVER);
2012-05-21 20:38:34 +00:00
}
2012-05-21 20:38:34 +00:00
void write(const Block & block) override
2012-05-21 20:38:34 +00:00
{
if (!sample_block)
throw Exception("You must call IBlockOutputStream::writePrefix before IBlockOutputStream::write", ErrorCodes::LOGICAL_ERROR);
2014-06-05 12:02:18 +00:00
if (!blocksHaveEqualStructure(block, sample_block))
{
std::stringstream message;
2014-06-06 14:50:27 +00:00
message << "Block structure is different from table structure.\n"
2014-06-05 12:02:18 +00:00
<< "\nTable structure:\n(" << sample_block.dumpStructure() << ")\nBlock structure:\n(" << block.dumpStructure() << ")\n";
2014-07-04 11:21:53 +00:00
LOG_ERROR(&Logger::get("RemoteBlockOutputStream"), message.str());
2014-06-05 12:02:18 +00:00
throw DB::Exception(message.str());
}
2012-05-21 20:38:34 +00:00
connection.sendData(block);
}
/// Отправить блок данных, который уже был заранее сериализован (и, если надо, сжат), который следует прочитать из input-а.
void writePrepared(ReadBuffer & input, size_t size = 0)
{
/// Не можем использовать sample_block.
connection.sendPreparedData(input, size);
}
void writeSuffix() override
2012-05-21 20:38:34 +00:00
{
/// Пустой блок означает конец данных.
connection.sendData(Block());
/// Получаем пакет EndOfStream.
Connection::Packet packet = connection.receivePacket();
2012-06-19 22:46:02 +00:00
if (Protocol::Server::EndOfStream == packet.type)
{
/// Ничего.
}
else if (Protocol::Server::Exception == packet.type)
packet.exception->rethrow();
else
throw NetException("Unexpected packet from server (expected EndOfStream or Exception, got "
+ String(Protocol::Server::toString(packet.type)) + ")", ErrorCodes::UNEXPECTED_PACKET_FROM_SERVER);
2012-05-21 20:38:34 +00:00
}
private:
Connection & connection;
2012-06-07 20:02:41 +00:00
String query;
const Settings * settings;
2014-06-05 12:02:18 +00:00
Block sample_block;
2012-05-21 20:38:34 +00:00
};
}