import java.io.IOException;
import java.util.Scanner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class Remove {
/**
* 删除文件
*/
public static boolean rm(Configuration conf, String remoteFilePath) {
Path remotePath = new Path(remoteFilePath);
try (FileSystem fs = FileSystem.get(conf)) {
return fs.delete(remotePath, false);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://master:9000");
String remoteFilePath = "/user/hadoop/dir/myLocalFile.txt"; // HDFS路径
try {
RemoveOrMake.rm(conf, remoteFilePath); // 删除
System.out.println("删除文件: " + remoteFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}