HDFS依赖的第三方包:

  hadoop 1.x版本:

  commons-configuration-1.6.jar

  commons-lang-2.4.jar

  commons-loggin-1.1.1.jar

  hadoop-core-1.2.1.jar

  log4j-1.2.15.jar

  hadoop2.x版本:

  hadoop-mapreduce-client-core-2.2.0.jar

  hadoop-common-2.2.0.jar 

  hadoop-mapreduce-client-common-2.2.0.jar

  hadoop-mapreduce-client-jobclient-2.2.0.jar

注意:可根据自身的情况和版本信息手动添加或maven仓库依赖。

文件操作

  1 工具类的创建:

  创建一个工具类HDFSUtils,主要用来加载公共信息:  

 1 package com.laowang.utils;
 2 
 3 import org.apache.hadoop.conf.Configuration;
 4 import org.apache.hadoop.fs.FileSystem;
 5 import org.apache.hadoop.hdfs.DistributedFileSystem;
 6 import java.io.IOException;
 7 import java.net.URI;
 8 
 9 /**
10  * @author laowang
11  * @version v1.0.0
12  * @apiNote HDFS 工具类
13  * @since 2018/4/26 10:36
14  */
15 public class HDFSUtil {
16     /**
17      * @author laowang
18      * @version v1.0.0
19      * @apiNote 获取FileSystem
20      * @since 2018/4/26 10:39
21      */
22     public static FileSystem getFileSystem() {
23         FileSystem hdfs = null;
24         //获取配置文件信息
25         Configuration conf = new Configuration();
26         conf.set("fs.hdfs.impl",DistributedFileSystem.class.getName());
27         try {
28             //获取文件系统
29             hdfs = FileSystem.get(URI.create("hdfs://master01:9000"), conf);
30         } catch (IOException e) {
31             e.printStackTrace();
32         }
33         return hdfs;
34     }
35 
36     /**
37      * 关闭文件流
38      * @param fileSystem
39      */
40     public static boolean closeFileSystem(FileSystem fileSystem) {
41         boolean flag = false;
42         if (fileSystem != null) {
43             try {
44                 fileSystem.close();
45                 flag = true;
46             } catch (Exception e) {
47                 e.printStackTrace();
48             }
49         }
50         return flag;
51     }
52 }
HDFSUtils

相关文章:

  • 2021-10-07
  • 2022-01-18
  • 2021-05-16
  • 2021-07-26
  • 2022-12-23
  • 2021-11-18
  • 2022-02-06
  • 2021-06-19
猜你喜欢
  • 2021-11-22
  • 2019-08-07
  • 2021-09-12
  • 2021-10-02
  • 2021-04-03
  • 2022-02-25
  • 2021-12-22
相关资源
相似解决方案