API操作的HDFS系统都是框架封装好的,可以采用 I/O 流的方式实现数据的上传和下载。
HDFS文件上传
1、需求:将本地D盘上的honglou.txt文件上传到HDFS根目录
2、代码块
@Test
public void putFileToHDFS() throws IOException,InterruptedException,URISyntaxException{
//1、获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9000"),configuration,"root");
//2、创建输入流
FileInputStream fis = new FileInputStream(new Path("d:/honglou.txt"));
//3、获取输出流
FSDataOutputStream fos = fs.create(new Path("/honglou.txt"));
//4、流对拷
IOUtils.closeStream(fos);
IOUtils.closeStream(fis);