ClickHouse/dbms/src/Dictionaries/HTTPDictionarySource.cpp

166 lines
6.1 KiB
C++
Raw Normal View History

#include <Dictionaries/HTTPDictionarySource.h>
2016-11-15 19:51:06 +00:00
2016-11-25 00:16:20 +00:00
#include <Poco/Net/HTTPRequest.h>
#include <Interpreters/Context.h>
2017-05-25 19:26:17 +00:00
#include <DataStreams/OwningBlockInputStream.h>
#include <IO/ReadWriteBufferFromHTTP.h>
#include <DataStreams/IBlockOutputStream.h>
#include <IO/WriteBufferFromOStream.h>
2017-05-25 19:21:57 +00:00
#include <Dictionaries/DictionarySourceHelpers.h>
#include <common/logger_useful.h>
#include <IO/ConnectionTimeouts.h>
2016-11-15 19:51:06 +00:00
namespace DB
{
2016-12-08 02:49:04 +00:00
static const size_t max_block_size = 8192;
2016-11-22 15:03:54 +00:00
HTTPDictionarySource::HTTPDictionarySource(const DictionaryStructure & dict_struct_,
const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix,
Block & sample_block, const Context & context)
: log(&Logger::get("HTTPDictionarySource")),
update_time{std::chrono::system_clock::from_time_t(0)},
dict_struct{dict_struct_},
url{config.getString(config_prefix + ".url", "")},
update_field{config.getString(config_prefix + ".update_field", "")},
format{config.getString(config_prefix + ".format")},
sample_block{sample_block},
context(context),
timeouts(ConnectionTimeouts::getHTTPTimeouts(context.getSettingsRef()))
2016-11-15 19:51:06 +00:00
{
if (update_field.empty())
return;
std::string tmp_update_field;
Poco::URI::encode(update_field, "&", tmp_update_field);
update_field = tmp_update_field;
std::string::size_type option = url.substr(0, url.find("#", 0)).find("?");
if (option == std::string::npos)
update_field = '?' + update_field;
else
update_field = '&' + update_field;
2016-11-15 19:51:06 +00:00
}
HTTPDictionarySource::HTTPDictionarySource(const HTTPDictionarySource & other)
: log(&Logger::get("HTTPDictionarySource")),
update_time{other.update_time},
dict_struct{other.dict_struct},
url{other.url},
update_field{other.update_field},
format{other.format},
sample_block{other.sample_block},
context(other.context),
timeouts(ConnectionTimeouts::getHTTPTimeouts(context.getSettingsRef()))
2016-11-15 19:51:06 +00:00
{
}
std::string HTTPDictionarySource::getUpdateFieldAndDate()
{
if (update_time != std::chrono::system_clock::from_time_t(0))
{
auto tmp_time = update_time;
update_time = std::chrono::system_clock::now();
time_t hr_time = std::chrono::system_clock::to_time_t(tmp_time) - 1;
char buffer [80];
struct tm * timeinfo;
timeinfo = localtime (&hr_time);
strftime(buffer, 80, "=%Y-%m-%d%%20%H:%M:%S", timeinfo);
std::string str_time(buffer);
return url.substr(0, url.find("#", 0)) + update_field + str_time;
}
else
{
update_time = std::chrono::system_clock::now();
return url.substr(0, url.find("#", 0)) + update_field + "=0000-00-00%2000:00:00"; ///for initial load
}
}
2016-11-15 19:51:06 +00:00
BlockInputStreamPtr HTTPDictionarySource::loadAll()
{
LOG_TRACE(log, "loadAll " + toString());
Poco::URI uri(url);
auto in_ptr = std::make_unique<ReadWriteBufferFromHTTP>(uri, Poco::Net::HTTPRequest::HTTP_GET,
ReadWriteBufferFromHTTP::OutStreamCallback(), timeouts);
2017-05-25 19:21:57 +00:00
auto input_stream = context.getInputFormat(format, *in_ptr, sample_block, max_block_size);
return std::make_shared<OwningBlockInputStream<ReadWriteBufferFromHTTP>>(input_stream, std::move(in_ptr));
}
BlockInputStreamPtr HTTPDictionarySource::loadUpdatedAll()
{
std::string url_update = getUpdateFieldAndDate();
LOG_TRACE(log, "loadUpdatedAll " + url_update);
Poco::URI uri(url_update);
auto in_ptr = std::make_unique<ReadWriteBufferFromHTTP>(uri, Poco::Net::HTTPRequest::HTTP_GET,
ReadWriteBufferFromHTTP::OutStreamCallback(), timeouts);
auto input_stream = context.getInputFormat(format, *in_ptr, sample_block, max_block_size);
2017-05-25 19:21:57 +00:00
return std::make_shared<OwningBlockInputStream<ReadWriteBufferFromHTTP>>(input_stream, std::move(in_ptr));
2016-11-15 19:51:06 +00:00
}
BlockInputStreamPtr HTTPDictionarySource::loadIds(const std::vector<UInt64> & ids)
{
LOG_TRACE(log, "loadIds " << toString() << " size = " << ids.size());
2016-11-19 00:07:58 +00:00
2017-05-25 19:21:57 +00:00
ReadWriteBufferFromHTTP::OutStreamCallback out_stream_callback = [&](std::ostream & ostr)
{
WriteBufferFromOStream out_buffer(ostr);
auto output_stream = context.getOutputFormat(format, out_buffer, sample_block);
formatIDs(output_stream, ids);
};
2016-11-24 19:57:24 +00:00
Poco::URI uri(url);
auto in_ptr = std::make_unique<ReadWriteBufferFromHTTP>(uri, Poco::Net::HTTPRequest::HTTP_POST,
out_stream_callback, timeouts);
2017-05-25 19:21:57 +00:00
auto input_stream = context.getInputFormat(format, *in_ptr, sample_block, max_block_size);
return std::make_shared<OwningBlockInputStream<ReadWriteBufferFromHTTP>>(input_stream, std::move(in_ptr));
2016-11-15 19:51:06 +00:00
}
BlockInputStreamPtr HTTPDictionarySource::loadKeys(
ColumnConst unification (#1011) * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * Fixed error in ColumnArray::replicateGeneric [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150].
2017-07-21 06:35:58 +00:00
const Columns & key_columns, const std::vector<size_t> & requested_rows)
2016-11-15 19:51:06 +00:00
{
LOG_TRACE(log, "loadKeys " << toString() << " size = " << requested_rows.size());
2016-11-22 15:03:54 +00:00
2017-05-25 19:21:57 +00:00
ReadWriteBufferFromHTTP::OutStreamCallback out_stream_callback = [&](std::ostream & ostr)
{
WriteBufferFromOStream out_buffer(ostr);
auto output_stream = context.getOutputFormat(format, out_buffer, sample_block);
formatKeys(dict_struct, output_stream, key_columns, requested_rows);
};
2016-11-22 15:03:54 +00:00
Poco::URI uri(url);
auto in_ptr = std::make_unique<ReadWriteBufferFromHTTP>(uri, Poco::Net::HTTPRequest::HTTP_POST,
out_stream_callback, timeouts);
2017-05-25 19:21:57 +00:00
auto input_stream = context.getInputFormat(format, *in_ptr, sample_block, max_block_size);
return std::make_shared<OwningBlockInputStream<ReadWriteBufferFromHTTP>>(input_stream, std::move(in_ptr));
2016-11-15 19:51:06 +00:00
}
bool HTTPDictionarySource::isModified() const
{
return true;
2016-11-15 19:51:06 +00:00
}
bool HTTPDictionarySource::supportsSelectiveLoad() const
{
return true;
2016-11-15 19:51:06 +00:00
}
bool HTTPDictionarySource::hasUpdateField() const
{
return !update_field.empty();
}
2016-11-15 19:51:06 +00:00
DictionarySourcePtr HTTPDictionarySource::clone() const
{
return std::make_unique<HTTPDictionarySource>(*this);
2016-11-15 19:51:06 +00:00
}
std::string HTTPDictionarySource::toString() const
{
Poco::URI uri(url);
return uri.toString();
2016-11-15 19:51:06 +00:00
}
}