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, refers to the path of the file to be deleted, which can be an absolute or relative path on HDFS.

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 with the path of the file you want to delete. The second parameter of the delete() method specifies whether to recursively delete all contents in the folder, if set to false, it will only delete the file itself.

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.

bannerAds