u=3004660631,1745089813&fm=253&fmt=auto&app=138&f=JPEG.webp
参考博客:SpringBoot集成Hadoop系列一 —— 对HDFS的文件操作

1.导入依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.hadoop</groupId>
  8. <artifactId>hadoop-common</artifactId>
  9. <version>3.3.3</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.apache.hadoop</groupId>
  13. <artifactId>hadoop-hdfs</artifactId>
  14. <version>3.3.3</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.apache.hadoop</groupId>
  18. <artifactId>hadoop-client</artifactId>
  19. <version>3.3.3</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.apache.hadoop</groupId>
  23. <artifactId>hadoop-mapreduce-client-core</artifactId>
  24. <version>3.3.3</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>cn.bestwu</groupId>
  28. <artifactId>ik-analyzers</artifactId>
  29. <version>5.1.0</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.projectlombok</groupId>
  33. <artifactId>lombok</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>com.alibaba</groupId>
  37. <artifactId>fastjson</artifactId>
  38. <version>1.2.46</version>
  39. </dependency>
  40. <!-- 此处的导入依赖与mapreduce有关 -->
  41. <dependency>
  42. <groupId>jdk.tools</groupId>
  43. <artifactId>jdk.tools</artifactId>
  44. <version>1.8</version>
  45. <scope>system</scope>
  46. <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
  47. </dependency>
  48. <dependency>
  49. <groupId>junit</groupId>
  50. <artifactId>junit</artifactId>
  51. <scope>test</scope>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.springframework.boot</groupId>
  55. <artifactId>spring-boot-starter-test</artifactId>
  56. <scope>test</scope>
  57. </dependency>

2.核心代码

application.properties

  1. # tomcat thread = 200
  2. server.tomcat.max-threads=1000
  3. # edit tomcat port
  4. server.port=8900
  5. # session time 30
  6. server.session-timeout=60
  7. spring.application.name=hadoop
  8. spring.servlet.multipart.max-file-size=50MB
  9. spring.servlet.multipart.max-request-size=50MB
  10. hdfs.path=hdfs://hadoop0:9000
  11. hdfs.username=root

HdfsConfig.java

  1. import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.context.annotation.Configuration;
  3. /**
  4. * @author HappyDragon1994
  5. */
  6. @Configuration
  7. public class HdfsConfig {
  8. @Value("${hdfs.path}")
  9. private String path;
  10. public String getPath() {
  11. return path;
  12. }
  13. public void setPath(String path) {
  14. this.path = path;
  15. }
  16. }

HdfsController.java

  1. package com.zym.controller;
  2. import com.zym.entity.User;
  3. import com.zym.result.Result;
  4. import com.zym.service.HdfsService;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.apache.hadoop.fs.BlockLocation;
  8. import org.springframework.web.bind.annotation.*;
  9. import org.springframework.web.multipart.MultipartFile;
  10. import java.util.List;
  11. import java.util.Map;
  12. @RestController
  13. @RequestMapping("/hadoop/hdfs")
  14. @Slf4j
  15. public class HdfsController {
  16. /**
  17. * 创建文件夹
  18. * @param path
  19. * @return
  20. * @throws Exception
  21. */
  22. @RequestMapping(value = "mkdir", method = RequestMethod.POST)
  23. @ResponseBody
  24. public Result mkdir(@RequestParam("path") String path) throws Exception {
  25. if (StringUtils.isEmpty(path)) {
  26. System.out.println("请求参数为空");
  27. return new Result(Result.FAILURE, "请求参数为空");
  28. }
  29. // 创建空文件夹
  30. boolean isOk = HdfsService.mkdir(path);
  31. if (isOk) {
  32. log.debug("文件夹创建成功");
  33. return new Result(Result.SUCCESS, "文件夹创建成功");
  34. } else {
  35. log.debug("文件夹创建失败");
  36. return new Result(Result.FAILURE, "文件夹创建失败");
  37. }
  38. }
  39. /**
  40. * 读取HDFS目录信息
  41. * @param path
  42. * @return
  43. * @throws Exception
  44. */
  45. @PostMapping("/readPathInfo")
  46. public Result readPathInfo(@RequestParam("path") String path) throws Exception {
  47. List<Map<String, Object>> list = HdfsService.readPathInfo(path);
  48. return new Result(Result.SUCCESS, "读取HDFS目录信息成功", list);
  49. }
  50. /**
  51. * 获取HDFS文件在集群中的位置
  52. * @param path
  53. * @return
  54. * @throws Exception
  55. */
  56. @PostMapping("/getFileBlockLocations")
  57. public Result getFileBlockLocations(@RequestParam("path") String path) throws Exception {
  58. BlockLocation[] blockLocations = HdfsService.getFileBlockLocations(path);
  59. return new Result(Result.SUCCESS, "获取HDFS文件在集群中的位置", blockLocations);
  60. }
  61. /**
  62. * 创建文件
  63. * @param path
  64. * @return
  65. * @throws Exception
  66. */
  67. @PostMapping("/createFile")
  68. public Result createFile(@RequestParam("path") String path, @RequestParam("file") MultipartFile file)
  69. throws Exception {
  70. if (StringUtils.isEmpty(path) || null == file.getBytes()) {
  71. return new Result(Result.FAILURE, "请求参数为空");
  72. }
  73. HdfsService.createFile(path, file);
  74. return new Result(Result.SUCCESS, "创建文件成功");
  75. }
  76. /**
  77. * 读取HDFS文件内容
  78. * @param path
  79. * @return
  80. * @throws Exception
  81. */
  82. @PostMapping("/readFile")
  83. public Result readFile(@RequestParam("path") String path) throws Exception {
  84. String targetPath = HdfsService.readFile(path);
  85. return new Result(Result.SUCCESS, "读取HDFS文件内容", targetPath);
  86. }
  87. /**
  88. * 读取HDFS文件转换成Byte类型
  89. * @param path
  90. * @return
  91. * @throws Exception
  92. */
  93. @PostMapping("/openFileToBytes")
  94. public Result openFileToBytes(@RequestParam("path") String path) throws Exception {
  95. byte[] files = HdfsService.openFileToBytes(path);
  96. return new Result(Result.SUCCESS, "读取HDFS文件转换成Byte类型", files);
  97. }
  98. /**
  99. * 读取HDFS文件装换成User对象
  100. * @param path
  101. * @return
  102. * @throws Exception
  103. */
  104. @PostMapping("/openFileToUser")
  105. public Result openFileToUser(@RequestParam("path") String path) throws Exception {
  106. User user = HdfsService.openFileToObject(path, User.class);
  107. return new Result(Result.SUCCESS, "读取HDFS文件装换成User对象", user);
  108. }
  109. /**
  110. * 读取文件列表
  111. * @param path
  112. * @return
  113. * @throws Exception
  114. */
  115. @PostMapping("/listFile")
  116. public Result listFile(@RequestParam("path") String path) throws Exception {
  117. if (StringUtils.isEmpty(path)) {
  118. return new Result(Result.FAILURE, "请求参数为空");
  119. }
  120. List<Map<String, String>> returnList = HdfsService.listFile(path);
  121. return new Result(Result.SUCCESS, "读取文件列表成功", returnList);
  122. }
  123. /**
  124. * 重命名文件
  125. * @param oldName
  126. * @param newName
  127. * @return
  128. * @throws Exception
  129. */
  130. @PostMapping("/renameFile")
  131. public Result renameFile(@RequestParam("oldName") String oldName, @RequestParam("newName") String newName)
  132. throws Exception {
  133. if (StringUtils.isEmpty(oldName) || StringUtils.isEmpty(newName)) {
  134. return new Result(Result.FAILURE, "请求参数为空");
  135. }
  136. boolean isOk = HdfsService.renameFile(oldName, newName);
  137. if (isOk) {
  138. return new Result(Result.SUCCESS, "文件重命名成功");
  139. } else {
  140. return new Result(Result.FAILURE, "文件重命名失败");
  141. }
  142. }
  143. /**
  144. * 删除文件
  145. * @param path
  146. * @return
  147. * @throws Exception
  148. */
  149. @PostMapping("/deleteFile")
  150. public Result deleteFile(@RequestParam("path") String path) throws Exception {
  151. boolean isOk = HdfsService.deleteFile(path);
  152. if (isOk) {
  153. return new Result(Result.SUCCESS, "delete file success");
  154. } else {
  155. return new Result(Result.FAILURE, "delete file fail");
  156. }
  157. }
  158. /**
  159. * 上传文件
  160. * @param path
  161. * @param uploadPath
  162. * @return
  163. * @throws Exception
  164. */
  165. @PostMapping("/uploadFile")
  166. public Result uploadFile(@RequestParam("path") String path, @RequestParam("uploadPath") String uploadPath)
  167. throws Exception {
  168. HdfsService.uploadFile(path, uploadPath);
  169. return new Result(Result.SUCCESS, "upload file success");
  170. }
  171. /**
  172. * 下载文件
  173. * @param path
  174. * @param downloadPath
  175. * @return
  176. * @throws Exception
  177. */
  178. @PostMapping("/downloadFile")
  179. public Result downloadFile(@RequestParam("path") String path, @RequestParam("downloadPath") String downloadPath)
  180. throws Exception {
  181. HdfsService.downloadFile(path, downloadPath);
  182. return new Result(Result.SUCCESS, "download file success");
  183. }
  184. /**
  185. * HDFS文件复制
  186. * @param sourcePath
  187. * @param targetPath
  188. * @return
  189. * @throws Exception
  190. */
  191. @PostMapping("/copyFile")
  192. public Result copyFile(@RequestParam("sourcePath") String sourcePath, @RequestParam("targetPath") String targetPath)
  193. throws Exception {
  194. HdfsService.copyFile(sourcePath, targetPath);
  195. return new Result(Result.SUCCESS, "copy file success");
  196. }
  197. /**
  198. * 查看文件是否已存在
  199. * @param path
  200. * @return
  201. * @throws Exception
  202. */
  203. @PostMapping("/existFile")
  204. public Result existFile(@RequestParam("path") String path) throws Exception {
  205. boolean isExist = HdfsService.existFile(path);
  206. return new Result(Result.SUCCESS, "file isExist: " + isExist);
  207. }
  208. }

User.java

  1. package com.zym.entity;
  2. import org.apache.hadoop.io.Writable;
  3. import java.io.DataInput;
  4. import java.io.DataOutput;
  5. import java.io.IOException;
  6. /**
  7. * @author HappyDragon1994
  8. */
  9. public class User implements Writable {
  10. private String username;
  11. private Integer age;
  12. private String address;
  13. public User() {
  14. super();
  15. // TODO Auto-generated constructor stub
  16. }
  17. public User(String username, Integer age, String address) {
  18. super();
  19. this.username = username;
  20. this.age = age;
  21. this.address = address;
  22. }
  23. @Override
  24. public void write(DataOutput output) throws IOException {
  25. // 把对象序列化
  26. output.writeChars(username);
  27. output.writeInt(age);
  28. output.writeChars(address);
  29. }
  30. @Override
  31. public void readFields(DataInput input) throws IOException {
  32. // 把序列化的对象读取到内存中
  33. username = input.readUTF();
  34. age = input.readInt();
  35. address = input.readUTF();
  36. }
  37. public String getUsername() {
  38. return username;
  39. }
  40. public void setUsername(String username) {
  41. this.username = username;
  42. }
  43. public Integer getAge() {
  44. return age;
  45. }
  46. public void setAge(Integer age) {
  47. this.age = age;
  48. }
  49. public String getAddress() {
  50. return address;
  51. }
  52. public void setAddress(String address) {
  53. this.address = address;
  54. }
  55. @Override
  56. public String toString() {
  57. return "User [username=" + username + ", age=" + age + ", address=" + address + "]";
  58. }
  59. }

Result.java

  1. package com.zym.result;
  2. import lombok.Data;
  3. @Data
  4. public class Result {
  5. private String resCode;
  6. private String resDes;
  7. private Object data;
  8. public static final String FAILURE="sys-00-01";
  9. public static final String SUCCESS = "SUCCESS";
  10. public Result(String resCode,String resDes,Object data){
  11. this.resCode = resCode;
  12. this.resDes = resDes;
  13. this.data=data;
  14. }
  15. public Result(String resCode,String resDes){
  16. this.resCode = resCode;
  17. this.resDes = resDes;
  18. }
  19. }

HdfsService.java

  1. package com.zym.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.*;
  6. import org.apache.hadoop.hdfs.web.JsonUtil;
  7. import org.apache.hadoop.io.IOUtils;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.stereotype.Component;
  10. import org.springframework.web.multipart.MultipartFile;
  11. import javax.annotation.PostConstruct;
  12. import java.io.BufferedReader;
  13. import java.io.InputStreamReader;
  14. import java.net.URI;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. @Component
  20. public class HdfsService {
  21. @Value("${hdfs.path}")
  22. private String path;
  23. @Value("${hdfs.username}")
  24. private String username;
  25. private static String hdfsPath;
  26. private static String hdfsName;
  27. private static final int bufferSize = 1024 * 1024 * 64;
  28. @PostConstruct
  29. public void getPath() {
  30. hdfsPath = this.path;
  31. }
  32. @PostConstruct
  33. public void getName() {
  34. hdfsName = this.username;
  35. }
  36. public static String getHdfsPath() {
  37. return hdfsPath;
  38. }
  39. public String getUsername() {
  40. return username;
  41. }
  42. /**
  43. * 获取HDFS配置信息
  44. * @return
  45. */
  46. private static Configuration getConfiguration() {
  47. Configuration configuration = new Configuration();
  48. configuration.set("fs.defaultFS", hdfsPath);
  49. return configuration;
  50. }
  51. /**
  52. * 获取HDFS文件系统对象
  53. * @return
  54. * @throws Exception
  55. */
  56. public static FileSystem getFileSystem() throws Exception {
  57. // 客户端去操作hdfs时是有一个用户身份的,默认情况下hdfs客户端api会从jvm中获取一个参数作为自己的用户身份
  58. // DHADOOP_USER_NAME=hadoop
  59. // 也可以在构造客户端fs对象时,通过参数传递进去
  60. FileSystem fileSystem = FileSystem.get(new URI(hdfsPath), getConfiguration(), hdfsName);
  61. return fileSystem;
  62. }
  63. /**
  64. * 在HDFS创建文件夹
  65. * @param path
  66. * @return
  67. * @throws Exception
  68. */
  69. public static boolean mkdir(String path) throws Exception {
  70. if (StringUtils.isEmpty(path)) {
  71. return false;
  72. }
  73. if (existFile(path)) {
  74. return true;
  75. }
  76. FileSystem fs = getFileSystem();
  77. // 目标路径
  78. Path srcPath = new Path(path);
  79. boolean isOk = fs.mkdirs(srcPath);
  80. fs.close();
  81. return isOk;
  82. }
  83. /**
  84. * 判断HDFS文件是否存在
  85. * @param path
  86. * @return
  87. * @throws Exception
  88. */
  89. public static boolean existFile(String path) throws Exception {
  90. if (StringUtils.isEmpty(path)) {
  91. return false;
  92. }
  93. FileSystem fs = getFileSystem();
  94. Path srcPath = new Path(path);
  95. boolean isExists = fs.exists(srcPath);
  96. return isExists;
  97. }
  98. /**
  99. * 读取HDFS目录信息
  100. * @param path
  101. * @return
  102. * @throws Exception
  103. */
  104. public static List<Map<String, Object>> readPathInfo(String path) throws Exception {
  105. if (StringUtils.isEmpty(path)) {
  106. return null;
  107. }
  108. if (!existFile(path)) {
  109. return null;
  110. }
  111. FileSystem fs = getFileSystem();
  112. // 目标路径
  113. Path newPath = new Path(path);
  114. FileStatus[] statusList = fs.listStatus(newPath);
  115. List<Map<String, Object>> list = new ArrayList<>();
  116. if (null != statusList && statusList.length > 0) {
  117. for (FileStatus fileStatus : statusList) {
  118. Map<String, Object> map = new HashMap<>();
  119. map.put("filePath", fileStatus.getPath());
  120. map.put("fileStatus", fileStatus.toString());
  121. list.add(map);
  122. }
  123. return list;
  124. } else {
  125. return null;
  126. }
  127. }
  128. /**
  129. * HDFS创建文件
  130. * @param path
  131. * @param file
  132. * @throws Exception
  133. */
  134. public static void createFile(String path, MultipartFile file) throws Exception {
  135. if (StringUtils.isEmpty(path) || null == file.getBytes()) {
  136. return;
  137. }
  138. String fileName = file.getOriginalFilename();
  139. FileSystem fs = getFileSystem();
  140. // 上传时默认当前目录,后面自动拼接文件的目录
  141. Path newPath = new Path(path + "/" + fileName);
  142. // 打开一个输出流
  143. FSDataOutputStream outputStream = fs.create(newPath);
  144. outputStream.write(file.getBytes());
  145. outputStream.close();
  146. fs.close();
  147. }
  148. /**
  149. * 读取HDFS文件内容
  150. * @param path
  151. * @return
  152. * @throws Exception
  153. */
  154. public static String readFile(String path) throws Exception {
  155. if (StringUtils.isEmpty(path)) {
  156. return null;
  157. }
  158. if (!existFile(path)) {
  159. return null;
  160. }
  161. FileSystem fs = getFileSystem();
  162. // 目标路径
  163. Path srcPath = new Path(path);
  164. FSDataInputStream inputStream = null;
  165. try {
  166. inputStream = fs.open(srcPath);
  167. // 防止中文乱码
  168. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  169. String lineTxt = "";
  170. StringBuffer sb = new StringBuffer();
  171. while ((lineTxt = reader.readLine()) != null) {
  172. sb.append(lineTxt);
  173. }
  174. return sb.toString();
  175. } finally {
  176. inputStream.close();
  177. fs.close();
  178. }
  179. }
  180. /**
  181. * 读取HDFS文件列表
  182. * @param path
  183. * @return
  184. * @throws Exception
  185. */
  186. public static List<Map<String, String>> listFile(String path) throws Exception {
  187. if (StringUtils.isEmpty(path)) {
  188. return null;
  189. }
  190. if (!existFile(path)) {
  191. return null;
  192. }
  193. FileSystem fs = getFileSystem();
  194. // 目标路径
  195. Path srcPath = new Path(path);
  196. // 递归找到所有文件
  197. RemoteIterator<LocatedFileStatus> filesList = fs.listFiles(srcPath, true);
  198. List<Map<String, String>> returnList = new ArrayList<>();
  199. while (filesList.hasNext()) {
  200. LocatedFileStatus next = filesList.next();
  201. String fileName = next.getPath().getName();
  202. Path filePath = next.getPath();
  203. Map<String, String> map = new HashMap<>();
  204. map.put("fileName", fileName);
  205. map.put("filePath", filePath.toString());
  206. returnList.add(map);
  207. }
  208. fs.close();
  209. return returnList;
  210. }
  211. /**
  212. * HDFS重命名文件
  213. * @param oldName
  214. * @param newName
  215. * @return
  216. * @throws Exception
  217. */
  218. public static boolean renameFile(String oldName, String newName) throws Exception {
  219. if (StringUtils.isEmpty(oldName) || StringUtils.isEmpty(newName)) {
  220. return false;
  221. }
  222. FileSystem fs = getFileSystem();
  223. // 原文件目标路径
  224. Path oldPath = new Path(oldName);
  225. // 重命名目标路径
  226. Path newPath = new Path(newName);
  227. boolean isOk = fs.rename(oldPath, newPath);
  228. fs.close();
  229. return isOk;
  230. }
  231. /**
  232. * 删除HDFS文件
  233. * @param path
  234. * @return
  235. * @throws Exception
  236. */
  237. public static boolean deleteFile(String path) throws Exception {
  238. if (StringUtils.isEmpty(path)) {
  239. return false;
  240. }
  241. if (!existFile(path)) {
  242. return false;
  243. }
  244. FileSystem fs = getFileSystem();
  245. Path srcPath = new Path(path);
  246. boolean isOk = fs.deleteOnExit(srcPath);
  247. fs.close();
  248. return isOk;
  249. }
  250. /**
  251. * 上传HDFS文件
  252. * @param path
  253. * @param uploadPath
  254. * @throws Exception
  255. */
  256. public static void uploadFile(String path, String uploadPath) throws Exception {
  257. if (StringUtils.isEmpty(path) || StringUtils.isEmpty(uploadPath)) {
  258. return;
  259. }
  260. FileSystem fs = getFileSystem();
  261. // 上传路径
  262. Path clientPath = new Path(path);
  263. // 目标路径
  264. Path serverPath = new Path(uploadPath);
  265. // 调用文件系统的文件复制方法,第一个参数是否删除原文件true为删除,默认为false
  266. fs.copyFromLocalFile(false, clientPath, serverPath);
  267. fs.close();
  268. }
  269. /**
  270. * 下载HDFS文件
  271. * @param path
  272. * @param downloadPath
  273. * @throws Exception
  274. */
  275. public static void downloadFile(String path, String downloadPath) throws Exception {
  276. if (StringUtils.isEmpty(path) || StringUtils.isEmpty(downloadPath)) {
  277. return;
  278. }
  279. FileSystem fs = getFileSystem();
  280. // 上传路径
  281. Path clientPath = new Path(path);
  282. // 目标路径
  283. Path serverPath = new Path(downloadPath);
  284. // 调用文件系统的文件复制方法,第一个参数是否删除原文件true为删除,默认为false
  285. fs.copyToLocalFile(false, clientPath, serverPath);
  286. fs.close();
  287. }
  288. /**
  289. * HDFS文件复制
  290. * @param sourcePath
  291. * @param targetPath
  292. * @throws Exception
  293. */
  294. public static void copyFile(String sourcePath, String targetPath) throws Exception {
  295. if (StringUtils.isEmpty(sourcePath) || StringUtils.isEmpty(targetPath)) {
  296. return;
  297. }
  298. FileSystem fs = getFileSystem();
  299. // 原始文件路径
  300. Path oldPath = new Path(sourcePath);
  301. // 目标路径
  302. Path newPath = new Path(targetPath);
  303. FSDataInputStream inputStream = null;
  304. FSDataOutputStream outputStream = null;
  305. try {
  306. inputStream = fs.open(oldPath);
  307. outputStream = fs.create(newPath);
  308. IOUtils.copyBytes(inputStream, outputStream, bufferSize, false);
  309. } finally {
  310. inputStream.close();
  311. outputStream.close();
  312. fs.close();
  313. }
  314. }
  315. /**
  316. * 打开HDFS上的文件并返回byte数组
  317. * @param path
  318. * @return
  319. * @throws Exception
  320. */
  321. public static byte[] openFileToBytes(String path) throws Exception {
  322. if (StringUtils.isEmpty(path)) {
  323. return null;
  324. }
  325. if (!existFile(path)) {
  326. return null;
  327. }
  328. FileSystem fs = getFileSystem();
  329. // 目标路径
  330. Path srcPath = new Path(path);
  331. try {
  332. FSDataInputStream inputStream = fs.open(srcPath);
  333. return IOUtils.readFullyToByteArray(inputStream);
  334. } finally {
  335. fs.close();
  336. }
  337. }
  338. /**
  339. * 打开HDFS上的文件并返回java对象
  340. * @param path
  341. * @return
  342. * @throws Exception
  343. */
  344. public static <T extends Object> T openFileToObject(String path, Class<T> clazz) throws Exception {
  345. if (StringUtils.isEmpty(path)) {
  346. return null;
  347. }
  348. if (!existFile(path)) {
  349. return null;
  350. }
  351. String jsonStr = readFile(path);
  352. return JSONObject.parseObject(jsonStr,clazz);
  353. }
  354. /**
  355. * 获取某个文件在HDFS的集群位置
  356. * @param path
  357. * @return
  358. * @throws Exception
  359. */
  360. public static BlockLocation[] getFileBlockLocations(String path) throws Exception {
  361. if (StringUtils.isEmpty(path)) {
  362. return null;
  363. }
  364. if (!existFile(path)) {
  365. return null;
  366. }
  367. FileSystem fs = getFileSystem();
  368. // 目标路径
  369. Path srcPath = new Path(path);
  370. FileStatus fileStatus = fs.getFileStatus(srcPath);
  371. return fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
  372. }
  373. }

代码地址:???

……哎,开源路漫漫,Gitee现在仓库公开还要审核……下次改用GitHub吧,这次的代码上面都贴出来了,复制粘贴即可~

部分Postman测试截图:
image.png
image.png
image.png
image.png
image.png
image.png
image.png