/
FileSystemDatasetRepository.java
445 lines (382 loc) · 15.5 KB
/
FileSystemDatasetRepository.java
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/**
* Copyright 2013 Cloudera Inc.
*
* Licensed 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.
*/
package com.cloudera.data.filesystem;
import com.cloudera.data.*;
import com.cloudera.data.filesystem.impl.Accessor;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Supplier;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
import org.apache.avro.Schema;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* <p>
* A {@link DatasetRepository} that stores data in a Hadoop {@link FileSystem}.
* </p>
* <p>
* Given a {@link FileSystem}, a root directory, and a {@link MetadataProvider},
* this {@link DatasetRepository} implementation can load and store
* {@link Dataset}s on both local filesystems as well as the Hadoop Distributed
* FileSystem (HDFS). Users may directly instantiate this class with the three
* dependencies above and then perform dataset-related operations using any of
* the provided methods. The primary methods of interest will be
* {@link #create(String, DatasetDescriptor)}, {@link #get(String)}, and
* {@link #drop(String)} which create a new dataset, load an existing
* dataset, or delete an existing dataset, respectively. Once a dataset has been created
* or loaded, users can invoke the appropriate {@link Dataset} methods to get a reader
* or writer as needed.
* </p>
*
* @see DatasetRepository
* @see Dataset
* @see DatasetDescriptor
* @see PartitionStrategy
* @see MetadataProvider
*/
public class FileSystemDatasetRepository implements DatasetRepository {
private static final Logger logger = LoggerFactory
.getLogger(FileSystemDatasetRepository.class);
static {
Accessor.setDefault(new AccessorImpl());
}
private final MetadataProvider metadataProvider;
private final Path rootDirectory;
private final FileSystem fileSystem;
/**
* Construct a {@link FileSystemDatasetRepository} on the given {@link FileSystem} and
* root directory, and a {@link FileSystemMetadataProvider} with the same {@link
* FileSystem} and root directory.
*
* @param fileSystem the filesystem to store metadata and datasets in
* @param rootDirectory the root directory for metadata and datasets
*/
public FileSystemDatasetRepository(FileSystem fileSystem, Path rootDirectory) {
this(fileSystem, rootDirectory,
new FileSystemMetadataProvider(fileSystem, rootDirectory));
}
/**
* Construct a {@link FileSystemDatasetRepository} with a root directory at the
* given {@link URI}, and a {@link FileSystemMetadataProvider} with the same root
* directory.
*
* @param uri the root directory for metadata and datasets
* @since 0.3.0
*/
public FileSystemDatasetRepository(URI uri) {
Preconditions.checkArgument(uri != null,
"URI provider can not be null");
this.rootDirectory = new Path(uri);
try {
fileSystem = rootDirectory.getFileSystem(new Configuration());
} catch (IOException e) {
throw new DatasetRepositoryException("Problem creating " +
"FileSystemDatasetRepository.", e);
}
this.metadataProvider = new FileSystemMetadataProvider(fileSystem, rootDirectory);
}
/**
* Construct a {@link FileSystemDatasetRepository} on the given {@link FileSystem} and
* root directory, with the given {@link MetadataProvider} for metadata storage.
*
* @param fileSystem the filesystem to store datasets in
* @param rootDirectory the root directory for datasets
* @param metadataProvider the provider for metadata storage
*/
public FileSystemDatasetRepository(FileSystem fileSystem, Path rootDirectory,
MetadataProvider metadataProvider) {
Preconditions.checkArgument(fileSystem != null,
"FileSystem can not be null");
Preconditions.checkArgument(rootDirectory != null,
"Root directory can not be null");
Preconditions.checkArgument(metadataProvider != null,
"Metadata provider can not be null");
this.fileSystem = fileSystem;
this.rootDirectory = rootDirectory;
this.metadataProvider = metadataProvider;
}
@Override
public Dataset create(String name, DatasetDescriptor descriptor) {
Preconditions.checkArgument(name != null, "Name can not be null");
Preconditions.checkArgument(descriptor != null,
"Descriptor can not be null");
Schema schema = descriptor.getSchema();
Path datasetPath = pathForDataset(name);
try {
if (fileSystem.exists(datasetPath)) {
throw new DatasetRepositoryException("Attempt to create an existing dataset:" + name);
}
} catch (IOException e) {
throw new DatasetRepositoryException("Internal error while determining if dataset path already exists:" + datasetPath, e);
}
logger.debug("Creating dataset:{} schema:{} datasetPath:{}", new Object[] {
name, schema, datasetPath });
try {
if (!fileSystem.mkdirs(datasetPath)) {
throw new DatasetRepositoryException("Failed to make dataset path:" + datasetPath);
}
} catch (IOException e) {
throw new DatasetRepositoryException("Internal failure while creating dataset path:" + datasetPath, e);
}
metadataProvider.save(name, descriptor);
return new FileSystemDataset.Builder()
.name(name)
.fileSystem(fileSystem)
.descriptor(descriptor)
.directory(pathForDataset(name))
.partitionKey(
descriptor.isPartitioned() ? com.cloudera.data.impl.Accessor.getDefault()
.newPartitionKey() : null).get();
}
@Override
public Dataset update(String name, DatasetDescriptor descriptor) {
DatasetDescriptor oldDescriptor = metadataProvider.load(name);
if (!oldDescriptor.getFormat().equals(descriptor.getFormat())) {
throw new DatasetRepositoryException("Cannot change dataset format from " +
oldDescriptor.getFormat() + " to " + descriptor.getFormat());
}
if (oldDescriptor.isPartitioned() != descriptor.isPartitioned()) {
throw new DatasetRepositoryException("Cannot change an unpartitioned dataset to " +
" partitioned or vice versa.");
} else if (oldDescriptor.isPartitioned() && descriptor.isPartitioned() &&
!oldDescriptor.getPartitionStrategy().equals(descriptor.getPartitionStrategy())) {
throw new DatasetRepositoryException("Cannot change partition strategy from " +
oldDescriptor.getPartitionStrategy() + " to " + descriptor.getPartitionStrategy());
}
// check can read records written with old schema using new schema
Schema oldSchema = oldDescriptor.getSchema();
Schema newSchema = descriptor.getSchema();
if (!SchemaValidationUtil.canRead(oldSchema, newSchema)) {
throw new DatasetRepositoryException("New schema cannot read data written using " +
"old schema. New schema: " + newSchema.toString(true) + "\nOld schema: " +
oldSchema.toString(true));
}
metadataProvider.save(name, descriptor);
return new FileSystemDataset.Builder()
.name(name)
.fileSystem(fileSystem)
.descriptor(descriptor)
.directory(pathForDataset(name))
.partitionKey(
descriptor.isPartitioned() ? com.cloudera.data.impl.Accessor.getDefault()
.newPartitionKey() : null).get();
}
@Override
public Dataset get(String name) {
Preconditions.checkArgument(name != null, "Name can not be null");
logger.debug("Loading dataset:{}", name);
Path datasetDirectory = pathForDataset(name);
DatasetDescriptor descriptor = metadataProvider.load(name);
FileSystemDataset ds = new FileSystemDataset.Builder()
.fileSystem(fileSystem).descriptor(descriptor)
.directory(datasetDirectory).name(name).get();
logger.debug("Loaded dataset:{}", ds);
return ds;
}
@Override
public boolean drop(String name) {
Preconditions.checkArgument(name != null, "Name can not be null");
logger.debug("Dropping dataset:{}", name);
Path datasetPath = pathForDataset(name);
try {
if (metadataProvider.delete(name) && fileSystem.exists(datasetPath)) {
if (fileSystem.delete(datasetPath, true)) {
return true;
} else {
throw new DatasetRepositoryException("Failed to delete dataset name:" + name
+ " data path:" + datasetPath);
}
} else {
return false;
}
} catch (IOException e) {
throw new DatasetRepositoryException("Internal failure to test if dataset path exists:" + datasetPath);
}
}
/**
* Get a {@link PartitionKey} corresponding to a partition's filesystem path
* represented as a {@link URI}. If the path is not a valid partition,
* then {@link IllegalArgumentException} is thrown. Note that the partition does not
* have to exist.
* @param dataset the filesystem dataset
* @param partitionPath a directory path where the partition data is stored
* @return a partition key representing the partition at the given path
* @since 0.4.0
*/
public static PartitionKey partitionKeyForPath(Dataset dataset, URI partitionPath) {
Preconditions.checkState(dataset.getDescriptor().isPartitioned(),
"Attempt to get a partition on a non-partitioned dataset (name:%s)",
dataset.getName());
Preconditions.checkArgument(dataset instanceof FileSystemDataset,
"Dataset is not a FileSystemDataset");
FileSystemDataset fsDataset = (FileSystemDataset) dataset;
FileSystem fs = fsDataset.getFileSystem();
URI partitionUri = fs.makeQualified(new Path(partitionPath)).toUri();
URI directoryUri = fsDataset.getDirectory().toUri();
URI relativizedUri = directoryUri.relativize(partitionUri);
if (relativizedUri.equals(partitionUri)) {
throw new IllegalArgumentException(String.format("Partition URI %s has different " +
"root directory to dataset (directory: %s).", partitionUri, directoryUri));
}
Iterable<String> parts = Splitter.on('/').split(relativizedUri.getPath());
PartitionStrategy partitionStrategy = dataset.getDescriptor().getPartitionStrategy();
List<FieldPartitioner> fieldPartitioners = partitionStrategy.getFieldPartitioners();
if (Iterables.size(parts) > fieldPartitioners.size()) {
throw new IllegalArgumentException(String.format("Too many partition directories " +
"for %s (%s), expecting %s.", partitionUri, Iterables.size(parts),
fieldPartitioners.size()));
}
List<Object> values = Lists.newArrayList();
int i = 0;
for (String part : parts) {
Iterator<String> split = Splitter.on('=').split(part).iterator();
String fieldName = split.next();
FieldPartitioner fp = fieldPartitioners.get(i++);
if (!fieldName.equals(fp.getName())) {
throw new IllegalArgumentException(String.format("Unrecognized partition name " +
"'%s' in partition %s, expecting '%s'.", fieldName, partitionUri,
fp.getName()));
}
if (!split.hasNext()) {
throw new IllegalArgumentException(String.format("Missing partition value for " +
"'%s' in partition %s.", fieldName, partitionUri));
}
String stringValue = split.next();
Object value = fp.valueFromString(stringValue);
values.add(value);
}
return com.cloudera.data.impl.Accessor.getDefault().newPartitionKey(
values.toArray(new Object[values.size()]));
}
/**
* <p>
* Implementations should return the fully-qualified path of the data directory for
* the dataset with the given name.
* </p>
* <p>
* This method is for internal use only and users should not call it directly.
* </p>
* @since 0.2.0
*/
protected Path pathForDataset(String name) {
Preconditions.checkState(rootDirectory != null,
"Dataset repository root directory can not be null");
return new Path(rootDirectory, name.replace('.', '/'));
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("rootDirectory", rootDirectory)
.add("metadataProvider", metadataProvider)
.add("fileSystem", fileSystem).toString();
}
/**
* @return the root directory in the filesystem where datasets are stored.
*/
public Path getRootDirectory() {
return rootDirectory;
}
/**
* @return the {@link FileSystem} on which datasets are stored.
*/
public FileSystem getFileSystem() {
return fileSystem;
}
/**
* @return the {@link MetadataProvider} being used by this repository.
* @since 0.2.0
*/
public MetadataProvider getMetadataProvider() {
return metadataProvider;
}
/**
* A fluent builder to aid in the construction of {@link FileSystemDatasetRepository}
* instances.
* @since 0.2.0
*/
public static class Builder implements Supplier<FileSystemDatasetRepository> {
private FileSystem fileSystem;
private Path rootDirectory;
private MetadataProvider metadataProvider;
private Configuration configuration;
/**
* The root directory for metadata and dataset files.
*/
public Builder rootDirectory(Path path) {
this.rootDirectory = path;
return this;
}
/**
* The root directory for metadata and dataset files.
*/
public Builder rootDirectory(URI uri) {
this.rootDirectory = new Path(uri);
return this;
}
/**
* The {@link FileSystem} to store metadata and dataset files in. Optional. If not
* specified, the default filesystem will be used.
*/
public Builder fileSystem(FileSystem fileSystem) {
this.fileSystem = fileSystem;
return this;
}
/**
* The {@link MetadataProvider} for metadata storage. Optional. If not
* specified, a {@link FileSystemMetadataProvider} will be used.
*/
public Builder metadataProvider(MetadataProvider metadataProvider) {
this.metadataProvider = metadataProvider;
return this;
}
/**
* The {@link Configuration} used to find the {@link FileSystem}. Optional. If not
* specified, the default configuration will be used.
* @since 0.3.0
*/
public Builder configuration(Configuration configuration) {
this.configuration = configuration;
return this;
}
@Override
public FileSystemDatasetRepository get() {
Preconditions.checkState(this.rootDirectory != null, "No root directory defined");
if (fileSystem == null) {
if (configuration == null) {
configuration = new Configuration();
}
try {
fileSystem = rootDirectory.getFileSystem(configuration);
} catch (IOException e) {
throw new DatasetRepositoryException("Problem creating " +
"FileSystemDatasetRepository.", e);
}
}
if (metadataProvider == null) {
metadataProvider = new FileSystemMetadataProvider(fileSystem, rootDirectory);
}
return new FileSystemDatasetRepository(fileSystem, rootDirectory, metadataProvider);
}
}
}