hadoop 读取文件的两种方式

1.操作javaAPI方式

 static{
         URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
     }
     public static void main(String[] args) throws IOException {
         InputStream in=null;
         try {
            in=new URL(args[0]).openStream();
            IOUtils.copyBytes(in, System.out, 4096,false);
        } catch (Exception e) {
            // TODO: handle exception
        }
         finally {
            IOUtils.closeStream(in);
        }

2.操作org.apache.hadoop.fs.FileSystem方式

public static void main(String[] args) throws IOException {
		Configuration conf=new Configuration();
		String[]otherargs=new GenericOptionsParser(conf, args).getRemainingArgs();
		FileSystem fs=FileSystem.get(URI.create(otherargs[0]),conf);
		InputStream in=null;
		try {
			in=fs.open(new Path(otherargs[0]));
			IOUtils.copyBytes(in, System.out, 4096,false);
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally {
			IOUtils.closeStream(in);
		}

  

posted @ 2016-08-15 15:00  白开水加糖  阅读(1296)  评论(0编辑  收藏  举报