ClickHouse/dbms/IO/ReadBufferFromHDFS.cpp

75 lines
1.8 KiB
C++
Raw Normal View History

#include "ReadBufferFromHDFS.h"
2019-01-17 11:26:29 +00:00
#if USE_HDFS
2019-01-19 20:17:19 +00:00
#include <IO/HDFSCommon.h>
#include <hdfs/hdfs.h>
2019-01-17 11:26:29 +00:00
2019-02-10 17:40:52 +00:00
2019-01-17 11:26:29 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int NETWORK_ERROR;
2019-01-19 20:17:19 +00:00
extern const int CANNOT_OPEN_FILE;
2019-01-17 11:26:29 +00:00
}
2020-03-09 01:03:43 +00:00
ReadBufferFromHDFS::~ReadBufferFromHDFS() = default;
2019-01-17 11:26:29 +00:00
struct ReadBufferFromHDFS::ReadBufferFromHDFSImpl
{
2019-09-05 14:42:17 +00:00
std::string hdfs_uri;
2019-01-17 11:26:29 +00:00
hdfsFile fin;
2019-01-19 20:17:19 +00:00
HDFSBuilderPtr builder;
HDFSFSPtr fs;
2019-01-19 23:51:03 +00:00
2020-03-18 03:27:32 +00:00
explicit ReadBufferFromHDFSImpl(const std::string & hdfs_name_)
2019-01-17 11:26:29 +00:00
: hdfs_uri(hdfs_name_)
2019-01-19 20:17:19 +00:00
, builder(createHDFSBuilder(hdfs_uri))
, fs(createHDFSFS(builder.get()))
2019-01-17 11:26:29 +00:00
{
2019-09-05 14:42:17 +00:00
const size_t begin_of_path = hdfs_uri.find('/', hdfs_uri.find("//") + 2);
const std::string path = hdfs_uri.substr(begin_of_path);
2019-01-19 20:17:19 +00:00
fin = hdfsOpenFile(fs.get(), path.c_str(), O_RDONLY, 0, 0, 0);
2019-01-18 18:57:11 +00:00
if (fin == nullptr)
throw Exception("Unable to open HDFS file: " + path + " error: " + std::string(hdfsGetLastError()),
2019-01-19 20:17:19 +00:00
ErrorCodes::CANNOT_OPEN_FILE);
2019-01-17 11:26:29 +00:00
}
int read(char * start, size_t size)
{
2019-01-19 20:17:19 +00:00
int bytes_read = hdfsRead(fs.get(), fin, start, size);
2019-01-17 11:26:29 +00:00
if (bytes_read < 0)
2019-09-05 14:42:17 +00:00
throw Exception("Fail to read HDFS file: " + hdfs_uri + " " + std::string(hdfsGetLastError()),
ErrorCodes::NETWORK_ERROR);
2019-01-17 11:26:29 +00:00
return bytes_read;
}
2019-01-19 20:17:19 +00:00
~ReadBufferFromHDFSImpl()
{
hdfsCloseFile(fs.get(), fin);
}
2019-01-17 11:26:29 +00:00
};
ReadBufferFromHDFS::ReadBufferFromHDFS(const std::string & hdfs_name_, size_t buf_size)
: BufferWithOwnMemory<ReadBuffer>(buf_size)
, impl(std::make_unique<ReadBufferFromHDFSImpl>(hdfs_name_))
{
}
bool ReadBufferFromHDFS::nextImpl()
{
int bytes_read = impl->read(internal_buffer.begin(), internal_buffer.size());
if (bytes_read)
working_buffer.resize(bytes_read);
else
return false;
return true;
}
}
#endif