通过IO流操作HDFS

package com.atguigu.hdfs;

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.Test;
import org.mortbay.util.IO;

/**通过IO流操作HDFS
 * @author huangyu
 *
 */
public class IOToHdfs {
	@Test
	// 文件的上传 从本地通过IO流的方式上传到HDFS服务器
	public void putFileToHDFS() throws IOException, InterruptedException,
			URISyntaxException {
		Configuration conf = new Configuration();
		// 1,获取文件系统
		FileSystem fileSystem = FileSystem.get(
				new URI("hdfs://hadoop103:9000"), conf, "atguigu");
		// 2,获取输入流
		FileInputStream fsInputStream = new FileInputStream(new File(
				"e:/hadoop-2.7.2.tar.gz"));
		// 3,获取输出流
		FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path(
				"/user/atguigu/hadoop-2.7.2.tar.gz"));
		// 4,流的拷贝
		try {
			IOUtils.copyBytes(fsInputStream, fsDataOutputStream, conf);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			// 5,关闭资源
			IOUtils.closeStream(fsInputStream);
			IOUtils.closeStream(fsDataOutputStream);
		}

	}
	@Test
	// 文件的下载
	public void getFileFromHDFS() throws IOException, InterruptedException,
			URISyntaxException {
		Configuration conf = new Configuration();
		// 1,获取文件系统
		FileSystem fileSystem = FileSystem.get(
				new URI("hdfs://hadoop103:9000"), conf, "atguigu");
		// 2,获取输入流
		FSDataInputStream fsDataInputStream = fileSystem.open(new Path(
				"/user/atguigu/test/a.txt"));
		// 3,获取输出流
		FileOutputStream fos = new FileOutputStream(new File("e:/huangyu.txt"));

		
		// 5,关闭资源
		try {
			// 4,流的拷贝
			IOUtils.copyBytes(fsDataInputStream, fos, conf);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			IOUtils.closeStream(fsDataInputStream);
			IOUtils.closeStream(fos);
		}

	}
	@Test
	//下载大文件的第一块数据
	public void getFileFromHDFSSeek1() throws IOException, InterruptedException, URISyntaxException{
		Configuration conf = new Configuration();
		//1,获取文件系统
		FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop103:9000"), conf, "atguigu");
		//2,获取输入流
		FSDataInputStream fsDataInputStream = fileSystem.open(new Path("/user/atguigu/hadoop-2.7.2.tar.gz"));
		//3,获取输出流
		FileOutputStream fos = new FileOutputStream(new File("e:/1/hadoop-2.7.2.tar.gz.part1"));
		
		
		
		try {
			//4,流的拷贝	
			byte[] buf = new byte[1024 * 1024];
			
			for(int i = 0;i < 128;i++){
				fsDataInputStream.read(buf);
				fos.write(buf);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			IOUtils.closeStream(fsDataInputStream);
			IOUtils.closeStream(fos);
		}
	}
	@Test
	//下载大文件的第二块数据
	public void getFileFromHDFSSeek2() throws IOException, InterruptedException, URISyntaxException{
		Configuration conf = new Configuration();
		//1,获取文件系统
		FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop103:9000"), conf, "atguigu");
		//2,获取输入流
		FSDataInputStream fsDataInputStream = fileSystem.open(new Path("/user/atguigu/hadoop-2.7.2.tar.gz"));
		//3,获取输出流
		FileOutputStream fos = new FileOutputStream(new File("e:/1/hadoop-2.7.2.tar.gz.part2"));
		//4,流的对接,定位到128M
		fsDataInputStream.seek(1024*1024*128);
		
		//6,关闭资源
		try {
			//5,流的对接
			IOUtils.copyBytes(fsDataInputStream, fos, conf);
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			IOUtils.closeStream(fsDataInputStream);
			IOUtils.closeStream(fos);
		}
		
	}
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值