当前位置:   article > 正文

Java实现hdfs的8个api操作_hdfs api

hdfs api

一、预处理准备

1. 配置本地hadoop3.1.3目录文件

先将Linux上的hadoop解压版(tar -zxvf解压)复制一份,再删除(rm -rf)其share子目录(不然下载的文件比较大)

在这里插入图片描述

利用MobaXterm下载到本地(如D:\othersSofts目录下),改名为hadoop3.1.3,再在linux虚拟机上删除供下载用的hadoop目录文件

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

在其/bin目录下添加其所需要的Windows依赖,遇到同名文件则替换

在这里插入图片描述

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

2. 配置环境变量

在这里插入图片描述

在这里插入图片描述

二、Maven项目依赖

创建一个Maven项目,在pom.xml中添加如下依赖:

 <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>

    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>3.1.3</version>
    </dependency>

    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs</artifactId>
      <version>3.1.3</version>
    </dependency>

    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>3.1.3</version>
    </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

三、Java源代码

package org.igeek.hdfsapi;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * 使用hdfs的api进行操作
 */
public class HdfsApiTest {

    //文件系统对象
    FileSystem fs = null;

    /**
     * 初始化文件系统
     *
     * @throws URISyntaxException
     * @throws IOException
     */
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {

        //创建一个配置对象
        Configuration conf = new Configuration();

        // hdfs中的NameNode地址

        //方法一: ip地址:端口
//      String hdfsPath ="hdfs://192.168.31.53:8020";

        //方法二:hostname:端口
        String hdfsPath = "hdfs://bigdata03:8020";

        String userName = "root";

        //获取文件系统对象
        fs = FileSystem.get(new URI(hdfsPath), conf, userName);

        System.out.println("hdfs文件系统对象初始化完成!");

    }

    /**
     * 关闭文件系统
     *
     * @throws IOException
     */
    @After
    public void close() throws IOException {
        fs.close();
        System.out.println("hdfs文件系统已关闭!");

    }

    /**
     * 创建hdfs上的路径
     *
     * @throws IOException
     */

    @Test
    public void createPath() throws IOException {
        boolean createFlag = fs.mkdirs(new Path("/hdfs_api"));
        if (createFlag) {
            System.out.println("路径创建成功");
        } else {
            System.out.println("路径创建失败");

        }

    }

    /**
     * 删除hdfs上的路径
     *
     * @throws IOException
     */

    @Test
    public void deletePath() throws IOException {
        Path path = new Path("/test");
        //判断删除路径是否存在

        if (fs.exists(path)) {

            //使用递归删除
            boolean deleteFlag = fs.delete(path, true);
            if (deleteFlag) {
                System.out.println("删除路径成功");
            } else {
                System.out.println("删除路径失败");
            }

        }
    }


    /**
     * 创建hdfs文件并写入数据
     * @throwsIOException
     */
    @Test
    public void createFile() throws IOException {
        //创建文件路径
        Path path = new Path("/hdfs_api/add.txt");

        FSDataOutputStream fos = fs.create(path);
        //写出的数据
        String content = "我是通过api写入的数据";
        //通过流进行数据写出
        fos.write(content.getBytes());
        // 流的刷新
        fos.flush();
        }


    /**
    *删除hdfs上的文件
    *@throws IOException
    */
@Test
public void deleteFile() throws IOException{
        boolean deleteFlag = fs.deleteOnExit(new Path( "/touchz.txt"));

        if(deleteFlag){
            System.out. println("删除文件成功");
        } else {
            System. out. println("删除文件失败");
        }
}

    /**
     * hdfs上的文件移动路径并改名
     *@throws IOException
     */

    @Test
    public void moveFile() throws IOException {
    //文件的源路径
        Path src = new Path("/put.txt");
    //文件移动后的路径
        Path dst = new Path("/hdfs_api/put_new.txt");
        boolean renameFlag = fs.rename(src, dst);
        if (renameFlag) {
            System.out.println("文件移动成功");
        } else {
            System.out.println("文件移动失败");
        }
    }

    /**
     * 读取hdfs上的文件内容
     * @throws IOException
     */

    @Test
    public void readFile() throws IOException {
        FSDataInputStream fis = fs.open(new Path("/hdfs_api/add.txt"));
        IOUtils.copyBytes(fis, System.out, 2048, false);
        System.out.println("\n");

    }

    /**
     * 上传windows下的本地文件到hdfs上
     * @throws IOException
     */

    @Test
    public void uploadEile() throws IOException {
        //要上传的hdfs路径
        Path src = new Path("D:\\othersofts\\hadoop3.1.3\\etc\\hadoop\\core-site.xml");
        //文件的本地路径
        Path dst = new Path("/hdfs_api");
        fs.copyFromLocalFile(true, src, dst);
        System.out.println("文件从本地上传hdfs成功");

    }

    /**
     * 从hdfs上下载文件到本地
     * @throws IOException
     */
    @Test
    public void downloadFile() throws IOException {
        // hdfs路径
        Path src = new Path("/hdfs_api/add.txt");

        //本地路径
        Path dst = new Path("D:\\");

        fs.copyToLocalFile(false, src, dst,false);
        System.out.println("下载文件成功");

    }
    }

  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208

四、api操作的实现

注意:要在每个注解@Test下右击执行对应的方法,如:
在这里插入图片描述

1. 实现前的准备

再在Maven项目中完成初始化文件系统(注解使用@Before)和关闭文件系统(注解使用@After)部分:

 //文件系统对象
    FileSystem fs = null;

    /**
     * 初始化文件系统
     *
     * @throws URISyntaxException
     * @throws IOException
     */
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {

        //创建一个配置对象
        Configuration conf = new Configuration();

        // hdfs中的NameNode地址

        //方法一: ip地址:端口
//      String hdfsPath ="hdfs://192.168.31.53:8020";

        //方法二:hostname:端口
        String hdfsPath = "hdfs://bigdata03:8020";

        String userName = "root";

        //获取文件系统对象
        fs = FileSystem.get(new URI(hdfsPath), conf, userName);

        System.out.println("hdfs文件系统对象初始化完成!");

    }

    /**
     * 关闭文件系统
     *
     * @throws IOException
     */
    @After
    public void close() throws IOException {
        fs.close();
        System.out.println("hdfs文件系统已关闭!");

    }
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

2. 创建hdfs上的路径

    /**
     * 创建hdfs上的路径
     *
     * @throws IOException
     */

    @Test
    public void createPath() throws IOException {
        boolean createFlag = fs.mkdirs(new Path("/hdfs_api"));
        if (createFlag) {
            System.out.println("路径创建成功");
        } else {
            System.out.println("路径创建失败");

        }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在这里插入图片描述

在这里插入图片描述

输入hadoop指令上查看创建/hdfs_api的结果:

在这里插入图片描述

3. 删除hdfs上的路径

  /**
     * 删除hdfs上的路径
     *
     * @throws IOException
     */

    @Test
    public void deletePath() throws IOException {
        Path path = new Path("/test");
        //判断删除路径是否存在

        if (fs.exists(path)) {

            //使用递归删除
            boolean deleteFlag = fs.delete(path, true);
            if (deleteFlag) {
                System.out.println("删除路径成功");
            } else {
                System.out.println("删除路径失败");
            }

        }
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述

在这里插入图片描述

输入hadoop指令上查看删除/test的结果:

在这里插入图片描述

4. 创建hdfs文件并写入数据

/**
     * 创建hdfs文件并写入数据
     * @throwsIOException
     */
    @Test
    public void createFile() throws IOException {
        //创建文件路径
        Path path = new Path("/hdfs_api/add.txt");

        FSDataOutputStream fos = fs.create(path);
        //写出的数据
        String content = "我是通过api写入的数据";
        //通过流进行数据写出
        fos.write(content.getBytes());
        // 流的刷新
        fos.flush();
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在这里插入图片描述

在这里插入图片描述

输入hadoop指令上查看写入的数据

在这里插入图片描述

5. 删除hdfs上的文件

  /**
    *删除hdfs上的文件
    *@throws IOException
    */
@Test
public void deleteFile() throws IOException{
        boolean deleteFlag = fs.deleteOnExit(new Path( "/touchz.txt"));

        if(deleteFlag){
            System.out. println("删除文件成功");
        } else {
            System. out. println("删除文件失败");
        }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述

在这里插入图片描述

输入hadoop指令发现已有的/touchz.txt之后被删除:

在这里插入图片描述

6. hdfs上的文件移动路径并改名

 /**
     * hdfs上的文件移动路径并改名
     *@throws IOException
     */

    @Test
    public void moveFile() throws IOException {
    //文件的源路径
        Path src = new Path("/put.txt");
    //文件移动后的路径
        Path dst = new Path("/hdfs_api/put_new.txt");
        boolean renameFlag = fs.rename(src, dst);
        if (renameFlag) {
            System.out.println("文件移动成功");
        } else {
            System.out.println("文件移动失败");
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在这里插入图片描述

在这里插入图片描述

输入hadoop指令查看:

在这里插入图片描述

7.读取hdfs上的文件内容

/**
     * 读取hdfs上的文件内容
     * @throws IOException
     */

    @Test
    public void readFile() throws IOException {
        FSDataInputStream fis = fs.open(new Path("/hdfs_api/add.txt"));
        IOUtils.copyBytes(fis, System.out, 2048, false);
        System.out.println("\n");

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

在这里插入图片描述

8.上传windows下的本地文件到hdfs上

/**
     * 上传windows下的本地文件到hdfs上
     * @throws IOException
     */

    @Test
    public void uploadEile() throws IOException {
        //要上传的hdfs路径
        Path src = new Path("D:\\othersofts\\hadoop3.1.3\\etc\\hadoop\\core-site.xml");
        //文件的本地路径
        Path dst = new Path("/hdfs_api");
        fs.copyFromLocalFile(true, src, dst);
        System.out.println("文件从本地上传hdfs成功");

    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这里插入图片描述

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

9. 从hdfs上下载文件到本地

/**
     * 从hdfs上下载文件到本地
     * @throws IOException
     */
    @Test
    public void downloadFile() throws IOException {
        // hdfs路径
        Path src = new Path("/hdfs_api/add.txt");

        //本地路径
        Path dst = new Path("D:\\");

        fs.copyToLocalFile(false, src, dst,false);
        System.out.println("下载文件成功");

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

下载结果:

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/480493
推荐阅读
相关标签
  

闽ICP备14008679号