参数优先级排序:

  • 客户端代码中设置的值
  • ClassPath 下的用户自定义配置文件
  • 服务器的默认配置

一、HDFS文件上传

  1. /**
  2. * @Description 上传本地文件到hdfs
  3. * @MethodName testCopyFromLocalFile
  4. * @Param
  5. * @Return void
  6. * @Date 2019年10月28日 17:59:22
  7. * @Author Wells
  8. */
  9. @Test
  10. public void testCopyFromLocalFile() throws Exception {
  11. // 1.创建配置
  12. Configuration conf = new Configuration();
  13. conf.set("dfs.replication", "2");
  14. conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/core-site.xml"));
  15. conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/hdfs-site.xml"));
  16. // 2.获取文件系统,其中 uri 举例为:hdfs:xxx-xx-xx 或者 file:///,不填的话默认为本地系统:file:///
  17. FileSystem fs = FileSystem.get(new URI("hdfs://xxx-xx-xx"), conf, "root");
  18. // 3.上传本地文件到hdfs,其中 path 为全目录,不会拼接上面的 uri
  19. fs.copyFromLocalFile(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/data"),
  20. new Path("/tmp/wells/20191029/data"));
  21. // 4.关闭资源
  22. fs.close();
  23. }

二、HDFS文件下载

  1. /**
  2. * @Description 从hdfs上下载文件到本地
  3. * @MethodName testCopyToLocalFile
  4. * @Param
  5. * @Return void
  6. * @Date 2019年10月28日 18:03:54
  7. * @Author Wells
  8. */
  9. @Test
  10. public void testCopyToLocalFile() throws Exception {
  11. // 1.创建配置
  12. Configuration conf = new Configuration();
  13. conf.set("dfs.replication", "2");
  14. conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/core-site.xml"));
  15. conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/hdfs-site.xml"));
  16. // 2.获取文件系统
  17. FileSystem fs = FileSystem.get(new URI("hdfs://xxx-xx-xx"), conf, "root");
  18. // 3.下载hdfs文件到本地
  19. fs.copyToLocalFile(new Path("/tmp/wells/20191029/data"), new Path("/home/wells/Downloads/"));
  20. // 4.关闭资源
  21. fs.close();
  22. }

三、HDFS文件夹删除

  1. /**
  2. * @Description 删除hdfs文件夹/文件
  3. * @MethodName testDelete
  4. * @Param
  5. * @Return void
  6. * @Date 2019年10月28日 18:07:52
  7. * @Author Wells
  8. */
  9. @Test
  10. public void testDelete() throws Exception {
  11. Configuration conf = new Configuration();
  12. conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/core-site.xml"));
  13. conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/hdfs-site.xml"));
  14. // 1.获取文件系统
  15. FileSystem fs = FileSystem.get(new URI("hdfs://xxx-xx-xx"), conf, "root");
  16. // 2.执行删除
  17. fs.delete(new Path("/tmp/wells/20191029/data"), true);
  18. // 3.关闭资源
  19. fs.close();
  20. }

四、HDFS文件夹名修改

  1. /**
  2. * @Description 文件或文件夹重命名
  3. * @MethodName testRename
  4. * @Param
  5. * @Return void
  6. * @Date 2019年10月28日 18:10:08
  7. * @Author Wells
  8. */
  9. @Test
  10. public void testRename() throws Exception {
  11. Configuration conf = new Configuration();
  12. conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/core-site.xml"));
  13. conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/hdfs-site.xml"));
  14. // 1.获取文件系统
  15. FileSystem fs = FileSystem.get(new
  16. URI("hdfs://xxx-xx-xx"), conf, "root");
  17. // 2 修改文件名称
  18. fs.rename(new Path("/tmp/wells/20191029/data"), new Path("/tmp/wells/20191029/data2"));
  19. // 3 关闭资源
  20. fs.close();
  21. }

五、HDFS文件详情查看

  1. /**
  2. * @Description 获取文件夹下文件详情
  3. * @MethodName testListFiles
  4. * @Param
  5. * @Return void
  6. * @Date 2019年10月28日 18:13:41
  7. * @Author Wells
  8. */
  9. @Test
  10. public void testListFiles() throws Exception {
  11. Configuration conf = new Configuration();
  12. conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/core-site.xml"));
  13. conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/hdfs-site.xml"));
  14. // 1.获取文件系统
  15. FileSystem fs = FileSystem.get(new URI("hdfs://xxx-xx-xx"), conf, "root");
  16. // 2.获取文件夹下详情
  17. RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/tmp/wells"), true);
  18. while (listFiles.hasNext()) {
  19. LocatedFileStatus status = listFiles.next();
  20. // 3.输出详情
  21. // 文件名称
  22. System.out.println(status.getPath().getName());
  23. // 长度
  24. System.out.println(status.getLen());
  25. // 权限
  26. System.out.println(status.getPermission());
  27. // 分组
  28. System.out.println(status.getGroup());
  29. // 获取存储的块信息
  30. BlockLocation[] blockLocations = status.getBlockLocations();
  31. for (BlockLocation blockLocation : blockLocations) {
  32. // 获取块存储的主机节点
  33. String[] hosts = blockLocation.getHosts();
  34. for (String host : hosts) {
  35. System.out.println(host);
  36. }
  37. }
  38. System.out.println("---------------------");
  39. }
  40. // 3.关闭资源
  41. fs.close();
  42. }

六、HDFS文件和文件夹判断

  1. /**
  2. * @Description 判断是否是文件
  3. * @MethodName testListStatus
  4. * @Param
  5. * @Return void
  6. * @Date 2019年10月28日 18:15:08
  7. * @Author Wells
  8. */
  9. @Test
  10. public void testListStatus() throws Exception {
  11. // 1.获取文件配置信息
  12. Configuration conf = new Configuration();
  13. conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/core-site.xml"));
  14. conf.addResource(new Path("/home/wells/Projects/05-Github/01-java/mapreduce-demo/src/main/resources/hdfs-site.xml"));
  15. // 2.创建文件系统
  16. FileSystem fs = FileSystem.get(new URI("hdfs://xxx-xx-xx"), conf, "root");
  17. // 3.判断是文件还是文件夹
  18. FileStatus[] listStatus = fs.listStatus(new Path("/tmp/wells"));
  19. for (FileStatus fileStatus : listStatus) {
  20. // 如果是文件
  21. if (fileStatus.isFile()) {
  22. System.out.println("f:" + fileStatus.getPath().getName());
  23. } else {
  24. System.out.println("d:" + fileStatus.getPath().getName());
  25. }
  26. }
  27. // 3.关闭资源
  28. fs.close();
  29. }