【问题标题】:Delete hdfs folder from java从java中删除hdfs文件夹
【发布时间】:2015-02-27 14:39:45
【问题描述】:

在边缘节点上运行的 java 应用程序中,如果存在 hdfs 文件夹,我需要删除它。在运行文件夹中输出的 mapreduce 作业(使用 spark)之前,我需要这样做。

我发现我可以用这个方法

org.apache.hadoop.fs.FileUtil.fullyDelete(new File(url))

但是,我只能使其与本地文件夹(即正在运行的计算机上的文件 url)一起使用。我尝试使用类似的东西:

url = "hdfs://hdfshost:port/the/folder/to/delete";

hdfs://hdfshost:port 是 hdfs namenode IPC。我将它用于mapreduce,所以它是正确的。 然而它什么也没做。

那么,我应该使用什么网址,或者有其他方法吗?

注意:here 是有问题的简单项目。

【问题讨论】:

    标签: java hadoop hdfs


    【解决方案1】:

    这对我有用。

    只需在我的 WordCount 程序中添加以下代码即可:

    import org.apache.hadoop.fs.*;
    
    ...
    Configuration conf = new Configuration();
    
    Path output = new Path("/the/folder/to/delete");
    FileSystem hdfs = FileSystem.get(URI.create("hdfs://namenode:port"),conf);
    
    // delete existing directory
    if (hdfs.exists(output)) {
        hdfs.delete(output, true);
    }
    
    Job job = Job.getInstance(conf, "word count");
    ...
    

    您需要显式添加hdfs://hdfshost:port 以获取分布式文件系统。否则代码仅适用于本地文件系统。

    【讨论】:

      【解决方案2】:

      我是这样做的:

          Configuration conf = new Configuration();
          conf.set("fs.hdfs.impl",org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
          conf.set("fs.file.impl",org.apache.hadoop.fs.LocalFileSystem.class.getName());
          FileSystem  hdfs = FileSystem.get(URI.create("hdfs://<namenode-hostname>:<port>"), conf);
          hdfs.delete("/path/to/your/file", isRecursive);
      

      您的文件路径中不需要hdfs://hdfshost:port/

      【讨论】:

      • 完美,可行。另外我希望我可以使用hdfs 来执行其他标准文件系统操作。
      • @Juh_ 是的,你可以用上面的hdfs 做很多事情——创建文件、列出文件、删除等等
      • 现在好像不推荐使用删除方法了。
      【解决方案3】:

      如果需要删除目录下的所有文件:

      1) 检查您的目录中有多少文件。

      2) 以后全部删除

           public void delete_archivos_dedirectorio() throws IOException {
      
      //namenode= hdfs://ip + ":" + puerto 
      
                  Path directorio = new Path(namenode + "//test//"); //nos situamos en la ruta//
                  FileStatus[] fileStatus = hdfsFileSystem.listStatus(directorio); //listamos los archivos que hay actualmente en ese directorio antes de hacer nada
                  int archivos_basura =  fileStatus.length; //vemos cuandoarchivos hay en el directorio antes de hacer nada, y luego iteramos hasta el nuemro de archivos que haya y llos vamos borrando para luego ir crandolos de nuevo en el writte.
      
      
                  for (int numero = 0; numero <= archivos_basura ; numero++) {
      
                      Path archivo = new Path(namenode + "//test//" + numero + ".txt");
      
                      try {
      
                          if(hdfsFileSystem.exists(archivo)) {
      
                              try {
                                  hdfsFileSystem.delete(archivo, true);
                              } catch (IOException ex) {
                                  System.out.println(ex.getMessage());
                              }
                          }
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
      

      祝你好运:)

      【讨论】:

        猜你喜欢
        • 2016-03-12
        • 2011-04-16
        • 1970-01-01
        • 1970-01-01
        • 2014-12-28
        • 2017-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多