zoukankan      html  css  js  c++  java
  • 利用 Hadoop FileSystem listStatus 遍历文件目录 实现HDFS操作

    package com.chd.demo;

    import org.apache.hadoop.fs.FileStatus;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;

    public class ShowPathUtils
    {
    public void ShowPath(FileSystem hdfs, Path path)
    {
    try{

    if(hdfs == null || path == null){
    return;
    }
    //获取文件列表
    FileStatus[] files = hdfs.listStatus(path);

    //展示文件信息
    for (int i = 0; i < files.length; i++) {
    try{
    if(files[i].isDirectory()){
    System.out.println(">>>" + files[i].getPath()
    + ", dir owner:" + files[i].getOwner());
    //递归调用
    ShowPath(hdfs,files[i].getPath());
    }else if(files[i].isFile()){
    System.out.println(" " + files[i].getPath()
    + ", length:" + files[i].getLen()
    + ", owner:" + files[i].getOwner());
    }
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }

    利用 hadoop FileSystem listStatus 遍历文件目录 实现HDFS操作

    1.  
      package com.feng.test.hdfs.filesystem;
    2.  
       
    3.  
      import java.net.URI;
    4.  
       
    5.  
      import org.apache.hadoop.conf.Configuration;
    6.  
      import org.apache.hadoop.fs.FileStatus;
    7.  
      import org.apache.hadoop.fs.FileSystem;
    8.  
      import org.apache.hadoop.fs.Path;
    9.  
       
    10.  
      /**
    11.  
      * 遍历文件目录
    12.  
      * 远程调用机器 需要 liunx 修改 /etc/hosts 添加 10.11.12.4 master
    13.  
      * @author feng
    14.  
      *
    15.  
      */
    16.  
      public classFileList{
    17.  
       
    18.  
      publicstaticvoidmain(String[] args) {
    19.  
      FileSystem hdfs = null;
    20.  
      try{
    21.  
      Configuration config = new Configuration();
    22.  
      // 程序配置
    23.  
      config.set("fs.default.name", "hdfs://master:9000");
    24.  
      //config.set("hadoop.job.ugi", "feng,111111");
    25.  
      //config.set("hadoop.tmp.dir", "/tmp/hadoop-fengClient");
    26.  
      //config.set("dfs.replication", "1");
    27.  
      //config.set("mapred.job.tracker", "master:9001");
    28.  
       
    29.  
      hdfs = FileSystem.get(new URI("hdfs://master:9000"),
    30.  
      config, "feng");
    31.  
      Path path = new Path("/");
    32.  
       
    33.  
      iteratorShowFiles(hdfs, path);
    34.  
       
    35.  
      }catch(Exception e){
    36.  
      e.printStackTrace();
    37.  
      }finally{
    38.  
      if(hdfs != null){
    39.  
      try {
    40.  
      hdfs.closeAll();
    41.  
      } catch (Exception e) {
    42.  
      e.printStackTrace();
    43.  
      }
    44.  
      }
    45.  
      }
    46.  
      }
    47.  
       
    48.  
      /**
    49.  
      *
    50.  
      * @param hdfs FileSystem 对象
    51.  
      * @param path 文件路径
    52.  
      */
    53.  
      publicstaticvoiditeratorShowFiles(FileSystem hdfs, Path path){
    54.  
      try{
    55.  
      if(hdfs == null || path == null){
    56.  
      return;
    57.  
      }
    58.  
      //获取文件列表
    59.  
      FileStatus[] files = hdfs.listStatus(path);
    60.  
       
    61.  
      //展示文件信息
    62.  
      for (int i = 0; i < files.length; i++) {
    63.  
      try{
    64.  
      if(files[i].isDirectory()){
    65.  
      System.out.println(">>>" + files[i].getPath()
    66.  
      + ", dir owner:" + files[i].getOwner());
    67.  
      //递归调用
    68.  
      iteratorShowFiles(hdfs, files[i].getPath());
    69.  
      }else if(files[i].isFile()){
    70.  
      System.out.println(" " + files[i].getPath()
    71.  
      + ", length:" + files[i].getLen()
    72.  
      + ", owner:" + files[i].getOwner());
    73.  
      }
    74.  
      }catch(Exception e){
    75.  
      e.printStackTrace();
    76.  
      }
    77.  
      }
    78.  
      }catch(Exception e){
    79.  
      e.printStackTrace();
    80.  
      }
    81.  
      }
    82.  
       
    83.  
      }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    HDU 1850 Being a Good Boy in Spring Festival
    UESTC 1080 空心矩阵
    HDU 2491 Priest John's Busiest Day
    UVALive 6181
    ZOJ 2674 Strange Limit
    UVA 12532 Interval Product
    UESTC 1237 质因子分解
    UESTC 1014 Shot
    xe5 android listbox的 TMetropolisUIListBoxItem
    xe5 android tts(Text To Speech)
  • 原文地址:https://www.cnblogs.com/xuehu666/p/10564562.html
Copyright © 2011-2022 走看看