package hdfsdemo;
import org.apache.hadoop.conf.Configuration;
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 java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class IODemo {
@Test
public void putFileToHDFS() throws IOException,InterruptedException, URISyntaxException{
//获取文件系统
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"),conf,"master");
//创建输入流
FileInputStream fis = new FileInputStream(new File("/home/master/Desktop/app/data/tbStock.txt"));
//创建输出流
FSDataOutputStream fos = fs.create(new Path("/data/demo02.txt"));
//流对拷
IOUtils.copyBytes(fis,fos,conf);
//关闭资源
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);
fs.close();
}
}
2020-12-09
最新推荐文章于 2024-09-07 21:34:24 发布