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);
            }
        }
    }
}
Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐