HDFS文件上传
1、源代码`
@Test
public void testCopyFromLocalFile() throws IOException,InterruptedException,URISyntaxException{
//1、获取文件系统
Configuration configuration = new Configuration();
configuration.set("dfs.replication","2");//副本数
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9000"),configuration,"root");//写自己的hadoop机器和用户名
//2、上传文件
fs.copyFromLocalFile(new Path("d:/test.txt"),new Path("/test.txt")); //本地路径和hdfs路径
//3、关闭
fs.close();
System.out.println("上传结束");
}
2、将hdfs-site.xml拷贝到项目的根目录下
HDFS文件下载
@Test
public void testCopyToLocalFile() throws IOException,InterruptedException,URISyntaxException{
//1、获取文件系统
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:9000")