package com.zbb.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;
public class HdfsIO {
@Test
public void putFileToHDFS() throws Exception{
Configuration conf = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf, "zbb");
//创建输入流
FileInputStream fis = new FileInputStream(new File("e:/hello.txt"));
//创建输出流
FSDataOutputStream fos = fileSystem.create(new Path("/hello4.txt"), true);
//拷贝
IOUtils.copyBytes(fis, fos, conf);
//关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}
@Test
public void getFileFromHdfs()throws Exception{
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop102:9000"),configuration,"zbb");
FSDataInputStream fis = fileSystem.open(new Path("/hello4.txt"));
//创建输出流
FileOutputStream fos = new FileOutputStream(new File("e:/hello3.txt"));
IOUtils.copyBytes(fis, fos,configuration);
fis.close();
fos.close();
}
@Test
public void readFileSeek1() throws IOException, InterruptedException, URISyntaxException {
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "zbb");
// 2 获取输入流
FSDataInputStream fis = fs.open(new Path("/user/zbb/softwark/hadoop-2.7.2.tar.gz"));
// 3 创建输出流
FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-2.7.2.tar.gz.part1"));
// 4 流的拷贝
byte[] buf = new byte[1024];
for (int i = 0; i < 1024*128; i++) {
fis.read(buf);
fos.write(buf);
}
// 5 关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}
@Test
public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{
// 1 获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "zbb");
// 2 打开输入流
FSDataInputStream fis = fs.open(new Path("/user/zbb/softwark/hadoop-2.7.2.tar.gz"));
// 3 定位输入数据位置
fis.seek(1024*1024*128);
// 4 创建输出流
FileOutputStream fos = new FileOutputStream(new File("e:/hadoop-2.7.2.tar.gz.part2"));
// 5 流的对拷
IOUtils.copyBytes(fis, fos, configuration);
// 6 关闭资源
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
}
}
大文件可在cmd中使用 type 文件名>>文件名,合并文件