HadoopFileSystem extends FileSystem<HadoopResourceId> { @Override protected void rename( List<HadoopResourceId> srcResourceIds, List<HadoopResourceId> destResourceIds) throws IOException { for (int i = 0; i < srcResourceIds.size(); ++i) { final Path srcPath = srcResourceIds.get(i).toPath(); final Path destPath = destResourceIds.get(i).toPath(); final org.apache.hadoop.fs.FileSystem fs = srcPath.getFileSystem(configuration); mkdirs(destPath); boolean success = fs.rename(srcPath, destPath); if (!success && fs.exists(srcPath) && fs.exists(destPath)) { LOG.debug( String.format( LOG_DELETING_EXISTING_FILE, Path.getPathWithoutSchemeAndAuthority(destPath))); fs.delete(destPath, false); success = fs.rename(srcPath, destPath); } if (!success) { if (!fs.exists(srcPath)) { throw new FileNotFoundException( String.format( "Unable to rename resource %s to %s as source not found.", srcPath, destPath)); } else if (fs.exists(destPath)) { throw new FileAlreadyExistsException( String.format( "Unable to rename resource %s to %s as destination already exists and couldn't be deleted.", srcPath, destPath)); } else { throw new IOException( String.format( "Unable to rename resource %s to %s. No further information provided by underlying filesystem.", srcPath, destPath)); } } } } HadoopFileSystem(String scheme, Configuration configuration);   }
@Test public void testRename() throws Exception { create("testFileA", "testDataA".getBytes(StandardCharsets.UTF_8)); create("testFileB", "testDataB".getBytes(StandardCharsets.UTF_8)); assertArrayEquals("testDataA".getBytes(StandardCharsets.UTF_8), read("testFileA", 0)); assertArrayEquals("testDataB".getBytes(StandardCharsets.UTF_8), read("testFileB", 0)); fileSystem.rename( ImmutableList.of(testPath("testFileA"), testPath("testFileB")), ImmutableList.of(testPath("renameFileA"), testPath("renameFileB"))); List<MatchResult> results = fileSystem.match(ImmutableList.of(testPath("*").toString())); assertEquals(Status.OK, Iterables.getOnlyElement(results).status()); assertThat( Iterables.getOnlyElement(results).metadata(), containsInAnyOrder( Metadata.builder() .setResourceId(testPath("renameFileA")) .setIsReadSeekEfficient(true) .setSizeBytes("testDataA".getBytes(StandardCharsets.UTF_8).length) .setLastModifiedMillis(lastModified("renameFileA")) .build(), Metadata.builder() .setResourceId(testPath("renameFileB")) .setIsReadSeekEfficient(true) .setSizeBytes("testDataB".getBytes(StandardCharsets.UTF_8).length) .setLastModifiedMillis(lastModified("renameFileB")) .build())); assertArrayEquals("testDataA".getBytes(StandardCharsets.UTF_8), read("renameFileA", 0)); assertArrayEquals("testDataB".getBytes(StandardCharsets.UTF_8), read("renameFileB", 0)); }
@Test(expected = FileNotFoundException.class) public void testRenameMissingSource() throws Exception { fileSystem.rename( ImmutableList.of(testPath("missingFile")), ImmutableList.of(testPath("testFileA"))); }
@Test(expected = FileNotFoundException.class) public void testRenameRetryScenario() throws Exception { testRename(); fileSystem.rename( ImmutableList.of(testPath("testFileA"), testPath("testFileB")), ImmutableList.of(testPath("renameFileA"), testPath("renameFileB"))); }