IOUtils方式上傳下載文件

package com.css.hdfs04;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Before;
import org.junit.Test;

/**
 * IOUtils方式上傳下載文件
 */
public class HdfsIo {
    Configuration conf = null;
    FileSystem fs =null;
    @Before
    public void init() throws IOException, InterruptedException, URISyntaxException {
        // 1.加載配置
        conf = new Configuration();
        // 2.構造客戶端
        fs = FileSystem.get(new URI("hdfs://192.168.146.132:9000/"), conf, "root");
    }
    
    /**
     * 文件上傳HDFS
     */
    @Test
    public void putFileToHDFS() throws IllegalArgumentException, IOException{
        // 1.獲取輸入流
        FileInputStream fis = new FileInputStream(new File("c:/hello.txt"));
        // 2.獲取輸出流
        FSDataOutputStream fos = fs.create(new Path("/hello.txt"));
        // 3.流的拷貝
        IOUtils.copyBytes(fis, fos, conf);
        // 4.關閉資源
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
    }
    
    /**
     * 文件下載HDFS
     */
    @Test
    public void getFileFromHDFS() throws IllegalArgumentException, IOException{
        // 1.獲取輸入流
        FSDataInputStream fis = fs.open(new Path("/hello"));
        // 2.獲取輸出流
        FileOutputStream fos = new FileOutputStream(new File("c:/hello"));
        // 3.流的對拷
        IOUtils.copyBytes(fis, fos, conf);
        // 4.關閉資源
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
    }
}
相關文章
相關標籤/搜索