HDFS API编程(一)

实践1 – 开发环境搭建

Intellij IDEA
使用maven 管理项目

  1. 创建项目
    创建项目

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

点击Finish出现如下界面
需等待右下角蓝条加载完成(下载时间较长,需耐心等待蓝条加载完成)

在这里插入图片描述
显示 maven execution finished 说明加载完成
在这里插入图片描述

在pom.xml文件中junit 版本最好高于4.10以上方便做测试
所有对pom.xml文件的修改需等待蓝条下载结束

1
2
3
4
5
6
7
8
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

注意main文件夹下java文件夹和test文件夹下java文件夹的颜色有区别,

在这里插入图片描述
2. 添加hadoop依赖包(下载时间较长,需耐心等待)

  • 在pom.xml文件中对<dependencies>中添加
1
2
3
4
5
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0-cdh5.7.0</version>
</dependency>
  • 由于采用cdh版本,如出现下载失败,添加cdh仓库
1
2
3
4
5
6
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
</repository>
</repositories>
  • 对于版本信息<version>2.6.0-cdh5.7.0</version>会多次使用,定义一个变量,添加

    1
    2
    3
    <properties>
    <hadoop.version>2.6.0-cdh5.7.0</hadoop.version>
    </properties>
  • 整体变更为

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <hadoop.version>2.6.0-cdh5.7.0</hadoop.version>
    </properties>

    <repositories>
    <repository>
    <id>cloudera</id>
    <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>
    </repositories>

    <dependencies>
    <dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>${hadoop.version}</version>
    </dependency>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
    </dependency>
    </dependencies>

    按图中点击显示依赖包
    在这里插入图片描述

  • 成功显示hadoop依赖包,无红色波浪线说明下载完成。
    在这里插入图片描述

    实践2 – 应用程序开发

    使用java api操作HDFS文件系统

在test文件夹下创建java类HDFSApp.class
在这里插入图片描述

  • 添加代码
    1
    2
    3
    4
    5
    6
    7
    8
    public static void main(String[] args) throws Exception{
    Configuration configuration = new Configuration();
    FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.2.131:8020"),configuration,"hadoop");
    Path path = new Path("/hdfsapi/task");
    boolean result = fileSystem.mkdirs(path);
    System.out.println(result);

    }
    运行显示 表示成功 (warn不用管)
    在这里插入图片描述
    访问http://192.168.2.131:50070/explorer.html#/成功出现hdfsapi文件夹
    在这里插入图片描述

实践3 – jUnit封装

单元测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static final String HDFS_PATH = "hdfs://192.168.2.131:8020";
FileSystem fileSystem = null;
Configuration configuration = null;

@Before
public void setUp() throws Exception {
System.out.println("--------setUp---------");

configuration = new Configuration();
configuration.set("dfs.replication","1");

fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration, "hadoop");
}

/**
* 创建HDFS文件夹
*/
@Test
public void mkdir() throws Exception {
fileSystem.mkdirs(new Path("/hdfsapi/test"));
}

@After
public void tearDown() {
configuration = null;
fileSystem = null;
System.out.println("--------tearDown---------");
}

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2021 Inner peace All Rights Reserved.

UV : | PV :