ClickHouse/dbms/IO/HDFSCommon.cpp

63 lines
1.8 KiB
C++
Raw Normal View History

2019-01-19 20:17:19 +00:00
#include <IO/HDFSCommon.h>
2019-09-05 14:42:17 +00:00
#include <Poco/URI.h>
2019-01-19 20:17:19 +00:00
#if USE_HDFS
#include <Common/Exception.h>
2019-01-19 20:17:19 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int NETWORK_ERROR;
}
2019-07-09 17:35:47 +00:00
2019-09-05 14:42:17 +00:00
HDFSBuilderPtr createHDFSBuilder(const std::string & uri_str)
2019-01-19 20:17:19 +00:00
{
2019-09-05 14:42:17 +00:00
const Poco::URI uri(uri_str);
2019-01-19 20:17:19 +00:00
auto & host = uri.getHost();
auto port = uri.getPort();
2019-09-20 11:26:00 +00:00
const std::string path = "//";
if (host.empty())
2019-01-19 20:17:19 +00:00
throw Exception("Illegal HDFS URI: " + uri.toString(), ErrorCodes::BAD_ARGUMENTS);
HDFSBuilderPtr builder(hdfsNewBuilder());
if (builder == nullptr)
throw Exception("Unable to create builder to connect to HDFS: " + uri.toString() + " " + std::string(hdfsGetLastError()),
ErrorCodes::NETWORK_ERROR);
hdfsBuilderConfSetStr(builder.get(), "input.read.timeout", "60000"); // 1 min
hdfsBuilderConfSetStr(builder.get(), "input.write.timeout", "60000"); // 1 min
hdfsBuilderConfSetStr(builder.get(), "input.connect.timeout", "60000"); // 1 min
2019-07-09 17:35:47 +00:00
std::string user_info = uri.getUserInfo();
if (!user_info.empty() && user_info.front() != ':')
{
std::string user;
2020-03-08 21:18:53 +00:00
size_t delim_pos = user_info.find(':');
2019-07-09 17:35:47 +00:00
if (delim_pos != std::string::npos)
user = user_info.substr(0, delim_pos);
else
2019-07-09 17:35:47 +00:00
user = user_info;
hdfsBuilderSetUserName(builder.get(), user.c_str());
2019-07-09 10:26:06 +00:00
}
2019-01-19 20:17:19 +00:00
hdfsBuilderSetNameNode(builder.get(), host.c_str());
if (port != 0)
{
hdfsBuilderSetNameNodePort(builder.get(), port);
}
2019-01-19 20:17:19 +00:00
return builder;
}
HDFSFSPtr createHDFSFS(hdfsBuilder * builder)
{
HDFSFSPtr fs(hdfsBuilderConnect(builder));
if (fs == nullptr)
throw Exception("Unable to connect to HDFS: " + std::string(hdfsGetLastError()),
ErrorCodes::NETWORK_ERROR);
return fs;
}
}
#endif