forked from apache/orc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrcHdfsFile.cc
173 lines (151 loc) · 5.55 KB
/
OrcHdfsFile.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "orc/OrcFile.hh"
#include "Adaptor.hh"
#include "orc/Exceptions.hh"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "hdfspp/hdfspp.h"
namespace orc {
class HdfsFileInputStream : public InputStream {
private:
std::string filename;
std::unique_ptr<hdfs::FileHandle> file;
std::unique_ptr<hdfs::FileSystem> file_system;
uint64_t totalLength;
const uint64_t READ_SIZE = 1024 * 1024; //1 MB
public:
HdfsFileInputStream(std::string _filename) {
filename = _filename ;
//Building a URI object from the given uri_path
hdfs::URI uri;
try {
uri = hdfs::URI::parse_from_string(filename);
} catch (const hdfs::uri_parse_error&) {
throw ParseError("Malformed URI: " + filename);
}
//This sets conf path to default "$HADOOP_CONF_DIR" or "/etc/hadoop/conf"
//and loads configs core-site.xml and hdfs-site.xml from the conf path
hdfs::ConfigParser parser;
if(!parser.LoadDefaultResources()){
throw ParseError("Could not load default resources. ");
}
auto stats = parser.ValidateResources();
//validating core-site.xml
if(!stats[0].second.ok()){
throw ParseError(stats[0].first + " is invalid: " + stats[0].second.ToString());
}
//validating hdfs-site.xml
if(!stats[1].second.ok()){
throw ParseError(stats[1].first + " is invalid: " + stats[1].second.ToString());
}
hdfs::Options options;
if(!parser.get_options(options)){
throw ParseError("Could not load Options object. ");
}
hdfs::IoService * io_service = hdfs::IoService::New();
//Wrapping file_system into a unique pointer to guarantee deletion
file_system = std::unique_ptr<hdfs::FileSystem>(
hdfs::FileSystem::New(io_service, "", options));
if (file_system.get() == nullptr) {
throw ParseError("Can't create FileSystem object. ");
}
hdfs::Status status;
//Checking if the user supplied the host
if(!uri.get_host().empty()){
//Using port if supplied, otherwise using "" to look up port in configs
std::string port = uri.has_port() ?
std::to_string(uri.get_port()) : "";
status = file_system->Connect(uri.get_host(), port);
if (!status.ok()) {
throw ParseError("Can't connect to " + uri.get_host()
+ ":" + port + ". " + status.ToString());
}
} else {
status = file_system->ConnectToDefaultFs();
if (!status.ok()) {
if(!options.defaultFS.get_host().empty()){
throw ParseError("Error connecting to " +
options.defaultFS.str() + ". " + status.ToString());
} else {
throw ParseError(
"Error connecting to the cluster: defaultFS is empty. "
+ status.ToString());
}
}
}
if (file_system.get() == nullptr) {
throw ParseError("Can't connect the file system. ");
}
hdfs::FileHandle *file_raw = nullptr;
status = file_system->Open(uri.get_path(), &file_raw);
if (!status.ok()) {
throw ParseError("Can't open "
+ uri.get_path() + ". " + status.ToString());
}
//Wrapping file_raw into a unique pointer to guarantee deletion
file.reset(file_raw);
hdfs::StatInfo stat_info;
status = file_system->GetFileInfo(uri.get_path(), stat_info);
if (!status.ok()) {
throw ParseError("Can't stat "
+ uri.get_path() + ". " + status.ToString());
}
totalLength = stat_info.length;
}
uint64_t getLength() const override {
return totalLength;
}
uint64_t getNaturalReadSize() const override {
return READ_SIZE;
}
void read(void* buf,
uint64_t length,
uint64_t offset) override {
if (!buf) {
throw ParseError("Buffer is null");
}
hdfs::Status status;
size_t total_bytes_read = 0;
size_t last_bytes_read = 0;
do {
status = file->PositionRead(buf,
static_cast<size_t>(length) - total_bytes_read,
static_cast<off_t>(offset + total_bytes_read), &last_bytes_read);
if(!status.ok()) {
throw ParseError("Error reading the file: " + status.ToString());
}
total_bytes_read += last_bytes_read;
} while (total_bytes_read < length);
}
const std::string& getName() const override {
return filename;
}
~HdfsFileInputStream() override;
};
HdfsFileInputStream::~HdfsFileInputStream() {
}
std::unique_ptr<InputStream> readHdfsFile(const std::string& path) {
return std::unique_ptr<InputStream>(new HdfsFileInputStream(path));
}
}