HadoopConfigurer.java 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package com.primeton.damp.core.config;
  2. import com.primeton.damp.principal.HadoopPrincipal;
  3. import com.primeton.damp.principal.HadoopPrincipalFactory;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.hadoop.fs.FileSystem;
  6. import org.apache.hadoop.fs.RawLocalFileSystem;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import java.io.IOException;
  11. import java.util.Optional;
  12. /**
  13. * <pre>
  14. *
  15. * Created by zhaopx.
  16. * User: zhaopx
  17. * Date: 2021/4/2
  18. * Time: 15:54
  19. *
  20. * </pre>
  21. *
  22. * @author zhaopx
  23. */
  24. @Configuration
  25. @Slf4j
  26. public class HadoopConfigurer {
  27. /**
  28. * hdfs
  29. */
  30. @Autowired
  31. HadoopProperties hadoopProperties;
  32. @Bean
  33. public FileSystem fs() throws IOException {
  34. String type = Optional.ofNullable(hadoopProperties.getHdfsType()).orElse("local");
  35. if("local".equalsIgnoreCase(type)) {
  36. // local
  37. log.info("use local file system");
  38. return new RawLocalFileSystem();
  39. } else if("cluster".equalsIgnoreCase(type)) {
  40. log.info("use hdfs filesystem");
  41. final HadoopPrincipal hadoopPrincipal = HadoopPrincipalFactory.getHadoopPrincipal(hadoopProperties.getTenentName(), hadoopProperties.getUserKeytab(), hadoopProperties.getKrb5File());
  42. final org.apache.hadoop.conf.Configuration conf = hadoopPrincipal.getConf(hadoopProperties.getCoreSite(), hadoopProperties.getHdfsSite());
  43. return FileSystem.get(conf);
  44. } else {
  45. throw new IllegalStateException("unkown hadoop type: " + type);
  46. }
  47. }
  48. }