HDFS常用API

URL读取数据

InputStream in = null;
try {
	in = new URL("hdfs://hadoop:9000/input/text1.txt").openStream();
	IOUtils.copyBytes(in, System.out, 4096, false);
}finally{
	IOUtils.closeStream(in);
}
FIleSystem

读数据

String uri = "hdfs://centos1:9000/input/bank_log.txt";
FileSystem fs =FileSystem.get(URI.create(uri), conf); 
InputStream in = null;
OutputStream out = null;
try {
	in = fs.open(new Path(uri));
	IOUtils.copyBytes(in, System.out, 4096, false);
}finally{
	IOUtils.closeStream(in);
}

获取文件元数据

    Path file = new Path("/dir/file");
    FileStatus stat = fs.getFileStatus(file);
    assertThat(stat.getPath().toUri().getPath(), is("/dir/file"));
    assertThat(stat.isDirectory(), is(false));
    assertThat(stat.getLen(), is(7L));
    assertThat(stat.getModificationTime(),
        is(lessThanOrEqualTo(System.currentTimeMillis())));
    assertThat(stat.getReplication(), is((short) 1));
    assertThat(stat.getBlockSize(), is(128 * 1024 * 1024L));
    assertThat(stat.getOwner(), is(System.getProperty("user.name")));
    assertThat(stat.getGroup(), is("supergroup"));
    assertThat(stat.getPermission().toString(), is("rw-r--r--"));
列出文件

String uri = "hdfs://centos1:9000/input/";
FileSystem fs =FileSystem.get(URI.create(uri), conf); 
//FileStatus[] status = fs.globStatus(new Path("/*"), new PathFilter)
FileStatus[] status = fs.globStatus(new Path("/*"));
// FileStatus[] status =  fs.listStatus(new Path(uri));
Path[] listPath = FileUtil.stat2Paths(status);
for(Path p:listPath){
	System.out.println(p);
}

PathFilter

public class RegexExcludePathFilter implements PathFilter {
  
  private final String regex;

  public RegexExcludePathFilter(String regex) {
    this.regex = regex;
  }

  public boolean accept(Path path) {
    return !path.toString().matches(regex);
  }
}



FSDataInputStream

String uri = "hdfs://hadoop:9000/input/text1.txt";
FileSystem fs =FileSystem.get(URI.create(uri), conf); 
FSDataInputStream in = null;
try {
   in = fs.open(new Path(uri));
   IOUtils.copyBytes(in, System.out, 4096, false);
   //seek移动到文件中任意一个绝对位置
   //inputSream.skip() 只能相对当前位置定位到另一个新位置
   in.seek(0);
   IOUtils.copyBytes(in, System.out, 4096, false);
}finally{
   IOUtils.closeStream(in);
}

FSDataOutputStream

写数据

String localUri = "F:/NL/hadoop/input/bank_log.txt";
String uri = "hdfs://centos1:9000/input/bank_log.txt";
InputStream in = new BufferedInputStream(new FileInputStream(localUri));
FileSystem fs =FileSystem.get(URI.create(uri), conf); 
OutputStream out = fs.create(new Path(uri), new Progressable() {
public void progress() {
	// TODO Auto-generated method stub
	System.out.print(".");
 }
});
IOUtils.copyBytes(in, out, 4096, false);






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值