zl程序教程

您现在的位置是:首页 >  其他

当前栏目

HDFS实战之下载文件

2023-09-14 09:13:17 时间

HDFS实战之下载文件

  1. 源码如下
package shen.liu.hdfs.practice;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

public class HDFSFileDownload {
    public static void main(String args[]) throws IOException {
        System.out.println("args = "+args);
        if(args.length < 2) {
            System.out.println("parameter error");
        }else {
            OutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File(args[1])));
            //文件可能没有找到---->FileNotFoundException
            /*1.Creates a new File instance by converting the given pathname 
             *string into an abstract pathname.
             * 
             */

            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(conf);

            Path hdfs = new Path(args[0]);
            InputStream hadoopIn = null;
            int bufferSize = 4096;
            if(args.length >= 3) {
                bufferSize = Integer.parseInt(args[2]);             
            }try {
                hadoopIn = fs.open(hdfs,bufferSize);
                IOUtils.copyBytes(hadoopIn, bos,4096,false);
            }finally {
                IOUtils.closeStream(hadoopIn);
                IOUtils.closeStream(bos);
            }
        }
    }
}