下面列出了org.apache.hadoop.fs.Path#mergePaths ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Path formPath(String dirOrFile)
{
String base = this.config.getHDFSWarehouse();
String path = dirOrFile;
while (base.endsWith("/")) {
base = base.substring(0, base.length() - 2);
}
if (!path.startsWith("/")) {
path = "/" + path;
}
return Path.mergePaths(new Path(base), new Path(path));
}
public FSDataOutputStream openHdfsFile(String fileName, String folderName) {
logger.debug("Begin file opening");
FSDataOutputStream fsOS = null;
Path filePath = null;
try {
FileSystem fs = hdfs.getFs();
filePath = fs.getWorkingDirectory();
if (folderName != null && folderName.length() > 0) {
filePath = Path.mergePaths(filePath, new Path(Path.SEPARATOR, folderName));
if (!fs.exists(filePath) || !fs.isDirectory(filePath)) {
fs.mkdirs(filePath);
}
}
filePath = Path.mergePaths(filePath, new Path(Path.SEPARATOR + fileName));
boolean existsFile = fs.exists(filePath);
if (existsFile) {
logger.debug("File is already present in folder, it will be deleted and replaced with new file");
fs.delete(filePath, true);
}
fsOS = fs.create(filePath, true);
} catch (IOException e) {
logger.error("Impossible to open file in File System");
throw new SpagoBIRuntimeException("Impossible to open file in File System" + e);
}
logger.debug("File opened");
return fsOS;
}
public static void assertDirectoriesInTrash(FileSystem fs, TrashPolicy trashPolicy, Path... dirs)
throws IOException {
Path trashDir = trashPolicy.getCurrentTrashDir();
for (Path path : dirs) {
Path trashPath = Path.mergePaths(trashDir, fs.makeQualified(path));
assertTrue("Directory should exist in trash: " + trashPath, fs.exists(trashPath));
}
}
@Test
public void testDeleteToTrashOrSkipTrash() throws Exception {
final String hostPrefix = OZONE_OFS_URI_SCHEME + "://" + omServiceId;
OzoneConfiguration clientConf = getClientConfForOFS(hostPrefix, conf);
OzoneFsShell shell = new OzoneFsShell(clientConf);
FileSystem fs = FileSystem.get(clientConf);
final String strDir1 = hostPrefix + "/volumed2t/bucket1/dir1";
// Note: CURRENT is also privately defined in TrashPolicyDefault
final Path trashCurrent = new Path("Current");
final String strKey1 = strDir1 + "/key1";
final Path pathKey1 = new Path(strKey1);
final Path trashPathKey1 = Path.mergePaths(new Path(
new OFSPath(strKey1).getTrashRoot(), trashCurrent), pathKey1);
final String strKey2 = strDir1 + "/key2";
final Path pathKey2 = new Path(strKey2);
final Path trashPathKey2 = Path.mergePaths(new Path(
new OFSPath(strKey2).getTrashRoot(), trashCurrent), pathKey2);
int res;
try {
res = ToolRunner.run(shell, new String[]{"-mkdir", "-p", strDir1});
Assert.assertEquals(0, res);
// Check delete to trash behavior
res = ToolRunner.run(shell, new String[]{"-touch", strKey1});
Assert.assertEquals(0, res);
// Verify key1 creation
FileStatus statusPathKey1 = fs.getFileStatus(pathKey1);
Assert.assertEquals(strKey1, statusPathKey1.getPath().toString());
// rm without -skipTrash. since trash interval > 0, should moved to trash
res = ToolRunner.run(shell, new String[]{"-rm", strKey1});
Assert.assertEquals(0, res);
// Verify that the file is moved to the correct trash location
FileStatus statusTrashPathKey1 = fs.getFileStatus(trashPathKey1);
// It'd be more meaningful if we actually write some content to the file
Assert.assertEquals(
statusPathKey1.getLen(), statusTrashPathKey1.getLen());
Assert.assertEquals(
fs.getFileChecksum(pathKey1), fs.getFileChecksum(trashPathKey1));
// Check delete skip trash behavior
res = ToolRunner.run(shell, new String[]{"-touch", strKey2});
Assert.assertEquals(0, res);
// Verify key2 creation
FileStatus statusPathKey2 = fs.getFileStatus(pathKey2);
Assert.assertEquals(strKey2, statusPathKey2.getPath().toString());
// rm with -skipTrash
res = ToolRunner.run(shell, new String[]{"-rm", "-skipTrash", strKey2});
Assert.assertEquals(0, res);
// Verify that the file is NOT moved to the trash location
try {
fs.getFileStatus(trashPathKey2);
Assert.fail("getFileStatus on non-existent should throw.");
} catch (FileNotFoundException ignored) {
}
} finally {
shell.close();
}
}
protected Path addPrefix(Path path) {
return Path.mergePaths(new Path(PATH_PREFIX), path);
}
protected Path addPrefix(Path path) {
return Path.mergePaths(new Path(PATH_PREFIX), path);
}