一、环境搭建

  • 下载win平台下编译的hadoop安装包

https://archive.apache.org/dist/hadoop/common/hadoop-3.2.1/ — hadoop-3.2.1.tar.gz

  • 配置相关文件

https://github.com/steveloughran/winutils
下载bin,然后将winutils.exe复制到本地的hadoop/bin下

  • 配置hadoop环境变量

image.png

二、springboot集成hadoop

  1. //到服务器上修改hadoop的配置文件 hdfs-site.xml
  2. //去掉访问权限限制
  3. <property>
  4. <name>dfs.permissions</name>
  5. <value>false</value>
  6. </property>
  7. //在代码中修改
  8. System.setProperty("HADOOP_USER_NAME","root");
  9. //打印文件列表
  10. @Override
  11. public void listFile() throws Exception{
  12. Configuration conf = new Configuration();
  13. conf.set("fs.defaultFS", path);
  14. FileSystem fileSystem = FileSystem.get(conf);
  15. RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), false);
  16. while(listFiles.hasNext()){
  17. LocatedFileStatus locatedFileStatus = listFiles.next();
  18. String filename = locatedFileStatus.getPath().getName();
  19. System.out.println(filename);
  20. }
  21. }
  22. @Override
  23. public void uploadFile() throws Exception{
  24. Configuration conf = new Configuration();
  25. conf.set("fs.defaultFS", path);
  26. //可以通过以下方法伪造客户端,win平台调用linux的情况
  27. //System.setProperty("HADOOP_USER_NAME", "root");
  28. FileSystem fileSystem = FileSystem.get(conf);
  29. fileSystem.copyFromLocalFile(new Path("d://test.rar"), new Path("/"));
  30. //fileSystem.copyToLocalFile(false, new Path("d://"), new Path("/hello.txt"),true);
  31. fileSystem.close();
  32. }