HdfsDAO.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package cn.seecoder.ai.dao;
  2. import cn.seecoder.ai.config.AppConfig;
  3. import cn.seecoder.ai.util.FileHelper;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.FileStatus;
  7. import org.apache.hadoop.fs.FileSystem;
  8. import org.apache.hadoop.fs.Path;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Component;
  11. import java.io.IOException;
  12. /**
  13. * this file is used to complete hdfs operates and mongodb operates
  14. * MongoDB, we now create user, fileLoc as collections
  15. */
  16. @Component
  17. @Slf4j
  18. public class HdfsDAO {
  19. private final AppConfig appConfig;
  20. //connect to hdfs
  21. private FileSystem hdfs;
  22. private Configuration conf;
  23. private String hdfsServerAddr;
  24. private String hdfsUri; //get hdfs Address
  25. @Autowired
  26. public HdfsDAO(AppConfig appConfig) { //在初始化方法中使用到的bean需要以注入初始化方法的形式导入,否则读取到的引用可能是null。
  27. this.appConfig = appConfig;
  28. //hdfs主机地址 hdfs://ip:port/
  29. hdfsServerAddr= appConfig.getHdfsServerAddr();
  30. //hdfs存储位置 hdfs://ip:port/user-space/*/
  31. hdfsUri= appConfig.getHdfsUri();
  32. conf = new Configuration();
  33. setUserSpace();
  34. setConf();
  35. }
  36. //保证hdfs存储位置可以访问
  37. private void setUserSpace(){
  38. //先根据主机ip设置配置
  39. conf.set("fs.defaultFS", hdfsServerAddr);
  40. try {
  41. hdfs = FileSystem.get(conf);
  42. //若不存在文件夹,新建
  43. //若文件夹已存在,新建失败
  44. hdfs.mkdirs(new Path(hdfsUri));
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. //set the configuration and get connection
  50. private void setConf(){
  51. conf.set("fs.defaultFS", hdfsUri);
  52. log.info("当前主机下的hdfs目录是:"+hdfsUri);
  53. //System.out.println(conf);
  54. try {
  55. hdfs = FileSystem.get(conf);
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. //put the local file to the hdfs
  61. public String putFile(String localFile){
  62. setConf();
  63. String fileName = FileHelper.getFile(localFile);//get the fileName
  64. String hdfsPathStr = hdfsUri + fileName;
  65. Path localPath = new Path(localFile);
  66. Path hdfsPath = new Path(hdfsPathStr);
  67. /*System.out.println(localPath);
  68. System.out.println(hdfsPath);*/
  69. try {
  70. hdfs.copyFromLocalFile(localPath,hdfsPath);
  71. hdfs.close();
  72. return hdfsPathStr;
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. return null;
  76. }
  77. }
  78. //delete file not in mongodb
  79. public boolean deleteFileInHdfs(String path, boolean isFolder){
  80. setConf();
  81. log.info("delete HDFS file: " + path);
  82. boolean isdelete = false;
  83. Path hdfsPath = new Path(path);
  84. try {
  85. if (hdfs.exists(hdfsPath)) {
  86. isdelete = hdfs.delete(hdfsPath, isFolder);
  87. }
  88. hdfs.close();
  89. } catch (IOException e) {
  90. e.printStackTrace();
  91. }
  92. System.out.println(isdelete);
  93. return isdelete;
  94. }
  95. //get all files in data
  96. public FileStatus[] getStatus(String path){
  97. FileStatus[] fileStatuses =null;
  98. try {
  99. fileStatuses = hdfs.listStatus(new Path(path));
  100. } catch (IOException e) {
  101. e.printStackTrace();
  102. }
  103. return fileStatuses;
  104. }
  105. //
  106. public boolean exist(String path){
  107. setConf();
  108. log.info("file exist: " + path);
  109. Path localPath = new Path(path);
  110. try {
  111. if (hdfs.exists(localPath)) {
  112. hdfs.close();
  113. return true;
  114. }
  115. }catch (Exception e){
  116. e.printStackTrace();
  117. }
  118. return false;
  119. }
  120. }