2018-11-28 11:37:12 +00:00
|
|
|
#include "HTTPDictionarySource.h"
|
2018-12-10 15:25:45 +00:00
|
|
|
#include <DataStreams/IBlockOutputStream.h>
|
2017-05-25 19:26:17 +00:00
|
|
|
#include <DataStreams/OwningBlockInputStream.h>
|
2018-12-10 15:25:45 +00:00
|
|
|
#include <IO/ConnectionTimeouts.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadWriteBufferFromHTTP.h>
|
|
|
|
#include <IO/WriteBufferFromOStream.h>
|
2019-12-05 09:18:51 +00:00
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2018-12-10 15:25:45 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
2017-01-21 04:24:28 +00:00
|
|
|
#include <common/logger_useful.h>
|
2018-11-28 11:37:12 +00:00
|
|
|
#include "DictionarySourceFactory.h"
|
2018-12-10 15:25:45 +00:00
|
|
|
#include "DictionarySourceHelpers.h"
|
2018-11-28 11:37:12 +00:00
|
|
|
#include "DictionaryStructure.h"
|
2019-12-15 06:34:43 +00:00
|
|
|
#include "registerDictionaries.h"
|
2018-11-28 11:37:12 +00:00
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
|
2016-11-15 19:51:06 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
2019-02-10 16:55:12 +00:00
|
|
|
static const UInt64 max_block_size = 8192;
|
2016-12-08 02:49:04 +00:00
|
|
|
|
|
|
|
|
2018-12-10 15:25:45 +00:00
|
|
|
HTTPDictionarySource::HTTPDictionarySource(
|
|
|
|
const DictionaryStructure & dict_struct_,
|
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const std::string & config_prefix,
|
2019-08-03 11:02:40 +00:00
|
|
|
Block & sample_block_,
|
2019-12-10 17:27:29 +00:00
|
|
|
const Context & context_,
|
|
|
|
bool check_config)
|
2018-12-10 15:25:45 +00:00
|
|
|
: 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")}
|
2019-08-03 11:02:40 +00:00
|
|
|
, sample_block{sample_block_}
|
|
|
|
, context(context_)
|
2019-03-29 18:10:03 +00:00
|
|
|
, timeouts(ConnectionTimeouts::getHTTPTimeouts(context))
|
2016-11-15 19:51:06 +00:00
|
|
|
{
|
2019-12-10 17:27:29 +00:00
|
|
|
|
|
|
|
if (check_config)
|
|
|
|
context.getRemoteHostFilter().checkURL(Poco::URI(url));
|
|
|
|
|
2019-09-25 04:33:54 +00:00
|
|
|
const auto & credentials_prefix = config_prefix + ".credentials";
|
|
|
|
|
|
|
|
if (config.has(credentials_prefix))
|
|
|
|
{
|
2019-09-26 03:34:22 +00:00
|
|
|
credentials.setUsername(config.getString(credentials_prefix + ".user", ""));
|
|
|
|
credentials.setPassword(config.getString(credentials_prefix + ".password", ""));
|
2019-09-25 04:33:54 +00:00
|
|
|
}
|
2019-09-25 08:08:46 +00:00
|
|
|
|
2019-09-26 03:34:22 +00:00
|
|
|
const auto & headers_prefix = config_prefix + ".headers";
|
2019-09-25 08:08:46 +00:00
|
|
|
|
2019-09-26 03:34:22 +00:00
|
|
|
if (config.has(headers_prefix))
|
2019-09-25 08:08:46 +00:00
|
|
|
{
|
|
|
|
Poco::Util::AbstractConfiguration::Keys config_keys;
|
2019-09-26 03:34:22 +00:00
|
|
|
config.keys(headers_prefix, config_keys);
|
2019-09-25 08:08:46 +00:00
|
|
|
|
2019-09-26 03:34:22 +00:00
|
|
|
header_entries.reserve(config_keys.size());
|
2019-09-25 08:08:46 +00:00
|
|
|
for (const auto & key : config_keys)
|
|
|
|
{
|
2019-09-26 03:34:22 +00:00
|
|
|
const auto header_key = config.getString(headers_prefix + "." + key + ".name", "");
|
|
|
|
const auto header_value = config.getString(headers_prefix + "." + key + ".value", "");
|
|
|
|
header_entries.emplace_back(std::make_tuple(header_key, header_value));
|
2019-09-25 08:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-15 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
HTTPDictionarySource::HTTPDictionarySource(const HTTPDictionarySource & other)
|
2018-12-10 15:25:45 +00:00
|
|
|
: log(&Logger::get("HTTPDictionarySource"))
|
|
|
|
, update_time{other.update_time}
|
|
|
|
, dict_struct{other.dict_struct}
|
|
|
|
, url{other.url}
|
2019-09-25 08:08:46 +00:00
|
|
|
, header_entries{other.header_entries}
|
2018-12-10 15:25:45 +00:00
|
|
|
, update_field{other.update_field}
|
|
|
|
, format{other.format}
|
|
|
|
, sample_block{other.sample_block}
|
|
|
|
, context(other.context)
|
2019-03-29 18:10:03 +00:00
|
|
|
, timeouts(ConnectionTimeouts::getHTTPTimeouts(context))
|
2016-11-15 19:51:06 +00:00
|
|
|
{
|
2019-09-26 03:34:22 +00:00
|
|
|
credentials.setUsername(other.credentials.getUsername());
|
|
|
|
credentials.setPassword(other.credentials.getPassword());
|
2016-11-15 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 13:22:27 +00:00
|
|
|
void HTTPDictionarySource::getUpdateFieldAndDate(Poco::URI & uri)
|
2018-01-15 12:44:39 +00:00
|
|
|
{
|
2018-02-15 13:08:23 +00:00
|
|
|
if (update_time != std::chrono::system_clock::from_time_t(0))
|
2018-01-15 12:44:39 +00:00
|
|
|
{
|
|
|
|
auto tmp_time = update_time;
|
|
|
|
update_time = std::chrono::system_clock::now();
|
2018-02-15 13:08:23 +00:00
|
|
|
time_t hr_time = std::chrono::system_clock::to_time_t(tmp_time) - 1;
|
2019-12-05 09:18:51 +00:00
|
|
|
WriteBufferFromOwnString out;
|
|
|
|
writeDateTimeText(hr_time, out);
|
|
|
|
uri.addQueryParameter(update_field, out.str());
|
2018-01-15 12:44:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
update_time = std::chrono::system_clock::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-15 19:51:06 +00:00
|
|
|
BlockInputStreamPtr HTTPDictionarySource::loadAll()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
LOG_TRACE(log, "loadAll " + toString());
|
2018-02-15 13:08:23 +00:00
|
|
|
Poco::URI uri(url);
|
2018-12-10 15:25:45 +00:00
|
|
|
auto in_ptr = std::make_unique<ReadWriteBufferFromHTTP>(
|
2019-09-25 08:08:46 +00:00
|
|
|
uri, Poco::Net::HTTPRequest::HTTP_GET, ReadWriteBufferFromHTTP::OutStreamCallback(), timeouts,
|
2019-09-26 03:34:22 +00:00
|
|
|
0, credentials, DBMS_DEFAULT_BUFFER_SIZE, header_entries);
|
2017-05-25 19:21:57 +00:00
|
|
|
auto input_stream = context.getInputFormat(format, *in_ptr, sample_block, max_block_size);
|
2018-02-15 13:08:23 +00:00
|
|
|
return std::make_shared<OwningBlockInputStream<ReadWriteBufferFromHTTP>>(input_stream, std::move(in_ptr));
|
|
|
|
}
|
2018-01-15 12:44:39 +00:00
|
|
|
|
2018-02-15 13:08:23 +00:00
|
|
|
BlockInputStreamPtr HTTPDictionarySource::loadUpdatedAll()
|
|
|
|
{
|
2018-05-16 13:22:27 +00:00
|
|
|
Poco::URI uri(url);
|
|
|
|
getUpdateFieldAndDate(uri);
|
|
|
|
LOG_TRACE(log, "loadUpdatedAll " + uri.toString());
|
2018-12-10 15:25:45 +00:00
|
|
|
auto in_ptr = std::make_unique<ReadWriteBufferFromHTTP>(
|
2019-09-25 08:08:46 +00:00
|
|
|
uri, Poco::Net::HTTPRequest::HTTP_GET, ReadWriteBufferFromHTTP::OutStreamCallback(), timeouts,
|
2019-09-26 03:34:22 +00:00
|
|
|
0, credentials, DBMS_DEFAULT_BUFFER_SIZE, header_entries);
|
2018-02-15 13:08:23 +00:00
|
|
|
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)
|
|
|
|
{
|
2017-05-25 19:27:23 +00:00
|
|
|
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);
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
2016-11-24 19:57:24 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::URI uri(url);
|
2019-09-25 08:08:46 +00:00
|
|
|
auto in_ptr = std::make_unique<ReadWriteBufferFromHTTP>(
|
|
|
|
uri, Poco::Net::HTTPRequest::HTTP_POST, out_stream_callback, timeouts,
|
2019-09-26 03:34:22 +00:00
|
|
|
0, credentials, DBMS_DEFAULT_BUFFER_SIZE, header_entries);
|
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
|
|
|
}
|
|
|
|
|
2018-12-10 15:25:45 +00:00
|
|
|
BlockInputStreamPtr HTTPDictionarySource::loadKeys(const Columns & key_columns, const std::vector<size_t> & requested_rows)
|
2016-11-15 19:51:06 +00:00
|
|
|
{
|
2017-05-25 19:27:23 +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);
|
2017-05-25 20:17:23 +00:00
|
|
|
formatKeys(dict_struct, output_stream, key_columns, requested_rows);
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
2016-11-22 15:03:54 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::URI uri(url);
|
2019-09-25 08:08:46 +00:00
|
|
|
auto in_ptr = std::make_unique<ReadWriteBufferFromHTTP>(
|
|
|
|
uri, Poco::Net::HTTPRequest::HTTP_POST, out_stream_callback, timeouts,
|
2019-09-26 03:34:22 +00:00
|
|
|
0, credentials, DBMS_DEFAULT_BUFFER_SIZE, header_entries);
|
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
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2016-11-15 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HTTPDictionarySource::supportsSelectiveLoad() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2016-11-15 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-15 12:44:39 +00:00
|
|
|
bool HTTPDictionarySource::hasUpdateField() const
|
|
|
|
{
|
2018-02-15 13:08:23 +00:00
|
|
|
return !update_field.empty();
|
2018-01-15 12:44:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 19:51:06 +00:00
|
|
|
DictionarySourcePtr HTTPDictionarySource::clone() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_unique<HTTPDictionarySource>(*this);
|
2016-11-15 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string HTTPDictionarySource::toString() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::URI uri(url);
|
|
|
|
return uri.toString();
|
2016-11-15 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 11:37:12 +00:00
|
|
|
void registerDictionarySourceHTTP(DictionarySourceFactory & factory)
|
|
|
|
{
|
2020-03-23 02:12:31 +00:00
|
|
|
auto create_table_source = [=](const DictionaryStructure & dict_struct,
|
2018-11-28 11:37:12 +00:00
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const std::string & config_prefix,
|
|
|
|
Block & sample_block,
|
2019-12-10 17:27:29 +00:00
|
|
|
const Context & context,
|
|
|
|
bool check_config) -> DictionarySourcePtr
|
2019-01-29 19:05:32 +00:00
|
|
|
{
|
2018-11-28 11:37:12 +00:00
|
|
|
if (dict_struct.has_expressions)
|
2018-12-10 15:25:45 +00:00
|
|
|
throw Exception{"Dictionary source of type `http` does not support attribute expressions", ErrorCodes::LOGICAL_ERROR};
|
2018-11-28 11:37:12 +00:00
|
|
|
|
2019-12-10 17:27:29 +00:00
|
|
|
return std::make_unique<HTTPDictionarySource>(
|
|
|
|
dict_struct, config, config_prefix + ".http",
|
|
|
|
sample_block, context, check_config);
|
2018-11-28 11:37:12 +00:00
|
|
|
};
|
2020-03-23 02:12:31 +00:00
|
|
|
factory.registerSource("http", create_table_source);
|
2018-11-28 11:37:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 19:51:06 +00:00
|
|
|
}
|