Eclipse实现DFS部分操作复习(1)

来源:互联网 发布:怎么利用淘宝联盟赚钱 编辑:程序博客网 时间:2024/06/03 10:49

本文用于复习《Hadoop权威指南》第三章部分内容

代码来自于书中,仅有少部分修改,主要是为了回忆起来方便,顺便用下刚刚学习的MarkDown

在文章eclipse实现word count中就有关于如何在eclipse中开发hadoop项目,链接如下Eclipse实现Hadoop WordCount


  • 用setURLStreamHandlerFactory方法来实现以标准格式显示Hadoop文件系统里面的文件,但是特别注意setURLStreamHandlerFactory只能用一次,在这里URL括号里面也可以是args[0]之类的,然后把args[0]设置为路径所指文件
    public class URLCat {      static {         URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());      }       public static void main(String[] args) throws Exception {         InputStream in = null;         try {         in = new URL("hdfs://localhost:9000/user/wyh/dfstrain.txt").openStream();         IOUtils.copyBytes(in, System.out, 4096, false);            } finally {              IOUtils.closeStream(in);               }             }          }
  • 用FileSystem API来实现以标准格式显示Hadoop文件系统里面的文件,uri也可以是args[0],这里用seek方法来使得从流的第几个字节开始copy,但是seek会导致效率变低
     public class FileSystemDoubleCat {        public static void main(String[] args) throws Exception {            String uri = "hdfs://localhost:9000/user/wyh/dfstrain.txt";            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(5); // go back to the fifth byte of the file              IOUtils.copyBytes(in, System.out, 4096, false);              } finally {                IOUtils.closeStream(in);            }      }    }
  • 用FileSystem API和Progressable方法来实现本地文件复制到Hadoop文件系统并显示进度,可以直接把文件放到包里面尝试

    public class FileCopyWithProgress {  public static void main(String[] args) throws Exception {    String localSrc = "dfstrain2.txt";    String dst = "hdfs://localhost:9000/user/wyh/dfstrain2.txt";    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);  }}
0 0
原创粉丝点击