How to delete files in Hadoop?
Hadoop has the capability to delete files using either command line tools or the Hadoop API. If using command line tools, you can use the following command to delete files:
hadoop fs -rm <文件路径>
In this case,
To delete a file using the Hadoop API, you can use the delete() method of the FileSystem class. Here is an example code in Java for deleting a file.
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HadoopDeleteFileExample {
public static void main(String[] args) {
try {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path filePath = new Path("<文件路径>");
boolean deleted = fs.delete(filePath, true);
if (deleted) {
System.out.println("文件删除成功");
} else {
System.out.println("文件删除失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
In the code above, replace
Please note that the above code needs to be run on a Hadoop cluster and requires proper configuration of Hadoop environment variables and relevant configuration files.