利用 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.  
    public static void main (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.  
    public static void iteratorShowFiles (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

转载于:https://www.cnblogs.com/xuehu666/p/10564562.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值