每日总结

HDFS文件下载

@Test

public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{

 

    // 1 获取文件系统

    Configuration configuration = new Configuration();

    FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");

    

    // 2 执行下载操作

    // boolean delSrc 指是否将原文件删除

    // Path src 指要下载的文件路径

    // Path dst 指将文件下载到的路径

    // boolean useRawLocalFileSystem 是否开启文件校验

    fs.copyToLocalFile(false, new Path("/xiyou/huaguoshan/sunwukong.txt"), new Path("d:/sunwukong2.txt"), true);

    

    // 3 关闭资源

    fs.close();

}

注意:如果执行上面代码,下载不了文件,有可能是你电脑的微软支持的运行库少,需要安装一下微软运行库。

3.2.3 HDFS文件更名和移动

@Test

public void testRename() throws IOException, InterruptedException, URISyntaxException{

 

// 1 获取文件系统

Configuration configuration = new Configuration();

FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");

 

// 2 修改文件名称

fs.rename(new Path("/xiyou/huaguoshan/sunwukong.txt"), new Path("/xiyou/huaguoshan/meihouwang.txt"));

 

// 3 关闭资源

fs.close();

}

3.2.4 HDFS删除文件和目录

@Test

public void testDelete() throws IOException, InterruptedException, URISyntaxException{

 

// 1 获取文件系统

Configuration configuration = new Configuration();

FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");

 

// 2 执行删除

fs.delete(new Path("/xiyou"), true);

 

// 3 关闭资源

fs.close();

}

posted @ 2021-11-24 13:40  小萌新一枚lll  阅读(53)  评论(0编辑  收藏  举报