接http://blog.csdn.net/tracymkgld/article/details/17552189
上篇没提到Trash具体怎么工作,接着看一下:
if(!skipTrash) {
try {
Trash trashTmp = new Trash(srcFs, getConf());
if (trashTmp.moveToTrash(src)) {//new 一个trash,然后把要删的文件名字符串扔给它
System.out.println("Moved to trash: " + src);
return;
}
} catch (IOException e) {
Exception cause = (Exception) e.getCause();
String msg = "";
if(cause != null) {
msg = cause.getLocalizedMessage();
}
System.err.println("Problem with Trash." + msg +". Consider using -skipTrash option");
throw e;
}
}
删除文件一般都会经过Trash,从FsShell的代码看就是new一个Trash对象,然后把要删的文件路径传给它就这么简单。
进入看看什么是Trash呢?
private final FileSystem fs;
private final Path trash;//private static final Path TRASH = new Path(".Trash/");
private final Path current;
private final long interval;
public Trash(FileSystem fs, Configuration conf) throws IOException {
super(conf);
this.fs = fs;
this.trash = new Path(fs.getHomeDirectory(), TRASH);
this.current = new Path(trash, CURRENT);//private static final Path CURRENT = new Path("Current");
this.interval = conf.getLong("fs.trash.interval", 60) * MSECS_PER_MINUTE;//集群默认配置清理trash的时间是1小时,实际这个时间可以灵活调整,也可以手工清理Trash,目前线上集群是2天。
}
可以看到Trash对象初始化的时候,要传递hdfs的文件系统句柄,它里边有个Path对象叫trash,这个Path指向用户家目录的.Trash目录
什么是家目录,看一眼你就知道了:
public Path getHomeDirectory() {
return new Path("/user/"+System.getProperty("user.name"))
.makeQualified(this);
}
家目录是指hdfs上/user/用户名这个目录,用户名是你客户端使用的用户名,关于 Kerbose 统一认证这里就不讲了。总之知道哪里是家目录就行了,类似linux的/home/username/目录
再看moveToTrash方法的片段: