本文整理汇总了Java中org.apache.hadoop.fs.Trash.moveToTrash方法的典型用法代码示例。如果您正苦于以下问题:Java Trash.moveToTrash方法的具体用法?Java Trash.moveToTrash怎么用?Java Trash.moveToTrash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.hadoop.fs.Trash
的用法示例。
在下文中一共展示了Trash.moveToTrash方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: deleteDirectory
import org.apache.hadoop.fs.Trash; //导入方法依赖的package包/类
/**
* Delete the specified directory, using the trash as available.
*
* @param conf configuration object
* @param path path to delete
*
* @throws IOException if there's an error deleting the directory.
*/
public static void deleteDirectory(Configuration conf, Path path) throws IOException {
Trash trash = new Trash(path.getFileSystem(conf), conf);
try {
if (!trash.isEnabled()) {
LOG.debug("Trash is not enabled for " + path + " so deleting instead");
FileSystem fs = path.getFileSystem(conf);
fs.delete(path, true);
} else {
boolean removed = trash.moveToTrash(path);
if (removed) {
LOG.debug("Moved to trash: " + path);
} else {
LOG.error("Item already in trash: " + path);
}
}
} catch (FileNotFoundException e) {
LOG.debug("Attempting to delete non-existent directory " + path);
return;
}
}
示例2: trashNonDefaultFS
import org.apache.hadoop.fs.Trash; //导入方法依赖的package包/类
public static void trashNonDefaultFS(Configuration conf) throws IOException {
conf.set("fs.trash.interval", "10"); // 10 minute
// attempt non-default FileSystem trash
{
final FileSystem lfs = FileSystem.getLocal(conf);
Path p = TEST_DIR;
Path f = new Path(p, "foo/bar");
if (lfs.exists(p)) {
lfs.delete(p, true);
}
try {
f = writeFile(lfs, f);
FileSystem.closeAll();
FileSystem localFs = FileSystem.get(URI.create("file:///"), conf);
Trash lTrash = new Trash(localFs, conf);
lTrash.moveToTrash(f.getParent());
checkTrash(localFs, lTrash.getCurrentTrashDir(), f);
} finally {
if (lfs.exists(p)) {
lfs.delete(p, true);
}
}
}
}
示例3: testMoveToTrash
import org.apache.hadoop.fs.Trash; //导入方法依赖的package包/类
@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));
}
示例4: moveToTrash
import org.apache.hadoop.fs.Trash; //导入方法依赖的package包/类
public static void moveToTrash(Configuration conf, Path path) throws IOException {
Trash t = new Trash(conf);
boolean isMoved = t.moveToTrash(path);
t.expunge();
if (!isMoved) {
logger.error("Trash is not enabled or file is already in the trash.");
}
}
示例5: deleteNonexisting
import org.apache.hadoop.fs.Trash; //导入方法依赖的package包/类
/**
* Delete the dst files/dirs which do not exist in src
*
* @return total count of files and directories deleted from destination
* @throws IOException
*/
static private long deleteNonexisting(
FileSystem dstfs, FileStatus dstroot, Path dstsorted,
FileSystem jobfs, Path jobdir, JobConf jobconf, Configuration conf
) throws IOException {
if (dstroot.isFile()) {
throw new IOException("dst must be a directory when option "
+ Options.DELETE.cmd + " is set, but dst (= " + dstroot.getPath()
+ ") is not a directory.");
}
//write dst lsr results
final Path dstlsr = new Path(jobdir, "_distcp_dst_lsr");
try (final SequenceFile.Writer writer = SequenceFile.createWriter(jobconf,
Writer.file(dstlsr), Writer.keyClass(Text.class),
Writer.valueClass(NullWritable.class), Writer.compression(
SequenceFile.CompressionType.NONE))) {
//do lsr to get all file statuses in dstroot
final Stack<FileStatus> lsrstack = new Stack<FileStatus>();
for(lsrstack.push(dstroot); !lsrstack.isEmpty(); ) {
final FileStatus status = lsrstack.pop();
if (status.isDirectory()) {
for(FileStatus child : dstfs.listStatus(status.getPath())) {
String relative = makeRelative(dstroot.getPath(), child.getPath());
writer.append(new Text(relative), NullWritable.get());
lsrstack.push(child);
}
}
}
}
//sort lsr results
final Path sortedlsr = new Path(jobdir, "_distcp_dst_lsr_sorted");
SequenceFile.Sorter sorter = new SequenceFile.Sorter(jobfs,
new Text.Comparator(), Text.class, NullWritable.class, jobconf);
sorter.sort(dstlsr, sortedlsr);
//compare lsr list and dst list
long deletedPathsCount = 0;
try (SequenceFile.Reader lsrin =
new SequenceFile.Reader(jobconf, Reader.file(sortedlsr));
SequenceFile.Reader dstin =
new SequenceFile.Reader(jobconf, Reader.file(dstsorted))) {
//compare sorted lsr list and sorted dst list
final Text lsrpath = new Text();
final Text dstpath = new Text();
final Text dstfrom = new Text();
final Trash trash = new Trash(dstfs, conf);
Path lastpath = null;
boolean hasnext = dstin.next(dstpath, dstfrom);
while (lsrin.next(lsrpath, NullWritable.get())) {
int dst_cmp_lsr = dstpath.compareTo(lsrpath);
while (hasnext && dst_cmp_lsr < 0) {
hasnext = dstin.next(dstpath, dstfrom);
dst_cmp_lsr = dstpath.compareTo(lsrpath);
}
if (dst_cmp_lsr == 0) {
//lsrpath exists in dst, skip it
hasnext = dstin.next(dstpath, dstfrom);
} else {
//lsrpath does not exist, delete it
final Path rmpath = new Path(dstroot.getPath(), lsrpath.toString());
++deletedPathsCount;
if ((lastpath == null || !isAncestorPath(lastpath, rmpath))) {
if (!(trash.moveToTrash(rmpath) || dstfs.delete(rmpath, true))) {
throw new IOException("Failed to delete " + rmpath);
}
lastpath = rmpath;
}
}
}
}
return deletedPathsCount;
}
示例6: moveToTrash
import org.apache.hadoop.fs.Trash; //导入方法依赖的package包/类
/**
* Moves the object to the filesystem trash according to the file system policy.
* @param fs FileSystem object
* @param path Path to the object to be moved to trash.
* @throws IOException
*/
public static void moveToTrash(FileSystem fs, Path path) throws IOException {
Trash trash = new Trash(fs, new Configuration());
trash.moveToTrash(path);
}