HDFS的API操作

Myth丶恋晨 2022-08-31 07:21 223阅读 0赞

对于hdfs的shell命令操作在前一篇的笔记中已经学习,最近对hdfs的API操作进行了学习,主要包括:
1、hadoop的连接和关闭
2、文件目录的创建
3、文件上传
4、文件下载
5、文件删除
6、文件的更名和移动
7、获取文件详细信息
8、判断是文件还是目录

全部操作代码如下:

  1. package com.yasin.hdfs;
  2. /* 1.获取一个客户端对象 2.执行相关的操作命令 3.关闭资源 */
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.fs.*;
  5. import org.junit.After;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import java.io.IOException;
  9. import java.net.URI;
  10. import java.net.URISyntaxException;
  11. import java.util.Arrays;
  12. public class HdfsClient {
  13. private FileSystem fs;
  14. /* // 快捷键 ctrl + p获取函数的参数说明 alt+enter抛出异常 (ctrl+alt+l:格式化) */
  15. @Before
  16. public void init() throws URISyntaxException, IOException, InterruptedException {
  17. //连接集群nn地址
  18. URI uri = new URI("hdfs://hadoop102:8020");
  19. //创建一个配置文件
  20. Configuration configuration = new Configuration();
  21. //获取到了客户端对象
  22. String usr = "yasin";
  23. fs = FileSystem.get(uri, configuration, usr);
  24. }
  25. @After
  26. public void close() throws IOException {
  27. //关闭资源
  28. fs.close();
  29. }
  30. //创建一个目录
  31. @Test
  32. public void testmkdir() throws URISyntaxException, IOException, InterruptedException {
  33. fs.mkdirs(new Path("/xiyou/huaguoshan3"));
  34. }
  35. //文件上传
  36. @Test
  37. public void testPut() throws IOException {
  38. fs.copyFromLocalFile(false, true, new Path("D:\\sunwukong.txt"), new Path("hdfs://hadoop102:/xiyou/huaguoshan"));
  39. }
  40. //文件下载
  41. @Test
  42. public void testGet() throws IOException {
  43. fs.copyToLocalFile(false, new Path("hdfs://hadoop102/xiyou/"), new Path("D:\\"), true);
  44. }
  45. //文件删除
  46. @Test
  47. public void testDel() throws IOException {
  48. //删除文件
  49. //fs.delete(new Path("hdfs://hadoop102/jdk-8u212-linux-x64.tar.gz"),false);
  50. //删除空目录
  51. fs.delete(new Path("hdfs://hadoop102:/xiyou/"), true);
  52. }
  53. //文件更名和移动
  54. @Test
  55. public void testmv() throws IOException {
  56. //对文件名修改
  57. //fs.rename(new Path("/input/word.txt"),new Path("/input/ss.txt"));
  58. //文件的移动和更名
  59. //fs.rename(new Path("/input/ss.txt"),new Path("/cls.txt"));
  60. //目录的更名
  61. fs.rename(new Path("/input"), new Path("/output"));
  62. }
  63. // 获取文件详细信息
  64. @Test
  65. public void fileDetail() throws IOException {
  66. //获取所有文件信息
  67. RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
  68. //遍历文件
  69. while (listFiles.hasNext()) {
  70. LocatedFileStatus fileStatus = listFiles.next();
  71. System.out.println("========" + fileStatus.getPath() + "======");
  72. System.out.println(fileStatus.getPermission());
  73. System.out.println(fileStatus.getOwner());
  74. System.out.println(fileStatus.getGroup());
  75. System.out.println(fileStatus.getModificationTime());
  76. System.out.println(fileStatus.getBlockSize());
  77. System.out.println(fileStatus.getPath().getName());
  78. System.out.println(fileStatus.getReplication());
  79. // 获取块信息
  80. BlockLocation[] blockLocations = fileStatus.getBlockLocations();
  81. System.out.println(Arrays.toString(blockLocations));
  82. }
  83. }
  84. //判断是文件夹还是文件
  85. @Test
  86. public void testfile() throws IOException {
  87. FileStatus[] listStatus = fs.listStatus(new Path("/"));
  88. for (FileStatus status : listStatus) {
  89. if (status.isFile()) {
  90. System.out.println("文件:" + status.getPath().getName());
  91. } else {
  92. System.out.println("路径:" + status.getPath().getName());
  93. }
  94. }
  95. }
  96. }

发表评论

表情:
评论列表 (有 0 条评论,223人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HDFSAPI操作

    对于hdfs的shell命令操作在前一篇的笔记中已经学习,最近对hdfs的API操作进行了学习,主要包括: 1、hadoop的连接和关闭 2、文件目录的创建 3、文