Hadoop文件系统的操作

从Hadoop文件系统中读取文件
使用java.net.URL对象打开数据流,从中读取数据:
InputStream in = null;
try{
in = new URL("hdfs://host/paath").openStream();
}finally{
IOUtils.closeStream(in);
}


通过URLStreamHandler实例以标准输出方式显示Hadoop文件系统的文件
public class URLCat{
static{
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
}
public static void main(String[] args) throws Exception{
try{
in = new URL(args[0]).openStream();
IOUtils.copyBytes(in, System.out, 4096, false);
}finally{
IOUtils.closeStream(in);
}
}
}


直接使用FileSystem以标准输出格式显示Hadoop文件系统中的文件
public class FileSystemCat{
public static void main(String[] args) throws Exception{
String uri = args[0];
Configuration conf  = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri),conf);
InputStream in = null;
try{
in = fs.open(new Path(uri));
IOUtils.copyBytes(in, System.out, 4096, false);
}finally{
IOUtils.closeStream(in);
}
}
}
FileSystem对象中的open()方法返回的是FSDataInputStream对象。
public class FSDataInputStream extends DataInputStream implementation  Seekable,PositionedReadable{
}
public interface Seekable{
void seek(long pos) throws IOException;
long getPos() throws IOException;
boolean seekToNewSource(long targetPos) throws IOWxception;
}


使用seek()方法,将Hadoop文件系统中的一个文件在标准输出上显示两次
public class FileSystemDoubleCat{
public static void main(String[] args) throws Exxception{
String uri = args[0];
Configuration conf  = new Configuration();
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);
in.seek(0);
IOutils.copyBytes(in, System.out, 4096, false);
}finally{
IOUtils.closeStream(in);
}
}
}
FSDataInputStream类也实现了PositionedReadable接口,从一个指定偏移量处读取文件的一部分:
public interface PositionedReadable{
public int read(long position, byte[] buffer, int offset, int length) throws IOException;
public void redFully(long position, byte[] buffer, int offset, int length) throws IOException;
public void readFully(long position, byte[] buffer) throws IOException;
}






写入数据
将本地文件复制到Hadoop文件系统
public class FileCopyWithProgress{
public static void main(String[] args) throws IOException{
String localSrc = args[0];
String dst = args[1];
InputStream in = new BufferedInputStream(new FileInputStream(localSrc));

Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dst),conf);
OutputStream out = fs.create(new Path(dst), new Progressable()){
public void progress(){
System.out.print(".");
}
}
IOUtils.copyBytes(in, out, 4096, true);
}
}




显示Hadoop文件系统中一组路径的文件信息
public class ListStatus {


  public static void main(String[] args) throws Exception {
    String uri = args[0];
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(uri), conf);
    
    Path[] paths = new Path[args.length];
    for (int i = 0; i < paths.length; i++) {
      paths[i] = new Path(args[i]);
    }
    
    FileStatus[] status = fs.listStatus(paths);
    Path[] listedPaths = FileUtil.stat2Paths(status);
    for (Path p : listedPaths) {
      System.out.println(p);
    }
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值