下面列出了怎么用org.apache.hadoop.fs.TrashPolicy的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testMoveToTrash() throws IOException {
Path hadoopUtilsTestDir = new Path(Files.createTempDir().getAbsolutePath(), "HadoopUtilsTestDir");
Configuration conf = new Configuration();
// Set the time to keep it in trash to 10 minutes.
// 0 means object will be deleted instantly.
conf.set("fs.trash.interval", "10");
FileSystem fs = FileSystem.getLocal(conf);
Trash trash = new Trash(fs, conf);
TrashPolicy trashPolicy = TrashPolicy.getInstance(conf, fs, fs.getHomeDirectory());
Path trashPath = trashPolicy.getCurrentTrashDir();
fs.mkdirs(hadoopUtilsTestDir);
Assert.assertTrue(fs.exists(hadoopUtilsTestDir));
trash.moveToTrash(hadoopUtilsTestDir.getParent());
Assert.assertFalse(fs.exists(hadoopUtilsTestDir));
Assert.assertTrue(fs.exists(trashPath));
}
public void testFileDeleteWithTrash() throws IOException {
Configuration conf = new Configuration();
conf.set("fs.trash.interval", "10"); // 10 minute
// create cluster
MiniDFSCluster cluster = new MiniDFSCluster(conf, 1, true, null);
FileSystem fs = null;
try {
cluster.waitActive();
fs = cluster.getFileSystem();
// create file1.
Path dir = new Path("/foo");
Path file1 = new Path(dir, "file1");
createFile(fs, file1);
System.out.println("testFileCreationDeleteParent: "
+ "Created file " + file1);
fs.delete(file1, true);
// create file2.
Path file2 = new Path("/tmp", "file2");
createFile(fs, file2);
System.out.println("testFileCreationDeleteParent: "
+ "Created file " + file2);
fs.delete(file2, true);
TrashPolicy trashPolicy = TrashPolicy.getInstance(conf, fs, fs.getHomeDirectory());
Path trashRoot = trashPolicy.getCurrentTrashDir();
TestTrash.checkTrash(fs, trashRoot, file1);
TestTrash.checkNotInTrash(fs, trashRoot, file2.toString());
} finally {
fs.close();
cluster.shutdown();
}
}
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));
}
}
@Before
public void setup() throws Exception {
this.conf = (distributed ?
MiniDFSTest.getConfiguration() :
new Configuration());
this.fs = FileSystem.get(conf);
this.trashPolicy = TrashPolicy.getInstance(conf, fs, fs.getHomeDirectory());
this.repo = newRepo();
this.strategy = new PartitionStrategy.Builder()
.year("timestamp")
.month("timestamp")
.day("timestamp")
.build();
this.testDescriptor = new DatasetDescriptor.Builder()
.schemaUri("resource:standard_event.avsc")
.partitionStrategy(strategy)
.build();
repo.delete("ns", "test");
this.unbounded = repo.create("ns", "test", testDescriptor);
this.valueDescriptor = new DatasetDescriptor.Builder().schemaUri("resource:value.avsc").build();
repo.delete("ns", "value");
this.valueView = repo.create("ns", "value", valueDescriptor);
this.testValueView = repo.load("ns", "value", TestValue.class);
}