当前位置:   article > 正文

大数据学习:使用Java API操作HDFS

使用java api操作hdfs

一、创建Maven项目

在这里插入图片描述

二、添加依赖

  • pom.xml文件里添加hadoopjunit依赖
    在这里插入图片描述
<dependencies>
        <dependency>
            <!--hadoop客户端-->
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.3.4</version>
        </dependency>
        <!--单元调试框架-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

三、创建日志属性文件

  • resources目录里创建log4j.properties文件
    在这里插入图片描述
    在这里插入图片描述
  • 代码
log4j.rootLogger=stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/hdfs.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

四、在HDFS上创建文件

  • /ied01目录创建hadoop2.txt文件
  • 创建net.xxr.hdfs包,在包里创建CreateFileOnHDFS
    在这里插入图片描述
    在这里插入图片描述
  • 编写create1()方法
package net.xxr.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;

import java.net.URI;

public class CreateFileOnHDFS {
    public void create1() throws Exception{
        // 创建配置对象
        Configuration conf = new Configuration();
        // 定义统一资源标识符
        String uri = "hdfs://master:9000";
        // 创建文件系统对象(基于HDFS的文件系统)
        FileSystem fs = FileSystem.get(new URI(uri), conf);
        // 创建路径对象(指向文件)
        Path path = new Path(uri + "/ied01/hadoop2.txt");
        // 基于路径对象创建文件
        boolean result = fs.createNewFile(path);
        // 根据返回值判断文件是否创建成功
        if (result) {
            System.out.println("文件[" + path + "]创建成功!");
        }else {
            System.out.println("文件[" + path + "]创建失败!");
        }
    }
}


  • 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
  • 结果
    在这里插入图片描述
  • 利用HDFS集群WebUI查看
    在这里插入图片描述
  • 编写create2()方法,实现判断文件是否存在
 @Test
    public void create2() throws Exception{
        // 创建配置对象
        Configuration conf = new Configuration();
        // 定义统一资源标识符
        String uri = "hdfs://master:9000";
        // 创建文件系统对象(基于HDFS的文件系统)
        FileSystem fs = FileSystem.get(new URI(uri), conf);
        // 创建路径对象(指向文件)
        Path path = new Path(uri + "/ied01/hadoop2.txt");
        // 判断路径对象指向的文件是否存在
        if (fs.exists(path)) {
            // 提示用户文件已存在
            System.out.println("文件[" + path + "]已存在!");
        }else{
            // 基于路径对象创建文件
            boolean result = fs.createNewFile(path);
            // 根据返回值判断文件是否创建成功
            if (result) {
                System.out.println("文件[" + path + "]创建成功!");
            }else {
                System.out.println("文件[" + path + "]创建失败!");
            }

        }
    }
  • 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
  • 结果
    在这里插入图片描述

五、写入HDFS文件

  • net.xxr.hdfs包里创建WriteFileOnHDFS
    在这里插入图片描述
1、将数据直接写入HDFS文件
package net.xxr.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;

import java.net.URI;

/*
功能:写入HDFS文件
作者:小小榕
日期:2022年11月30日
 */

public class WriteFileOnHDFS {
    @Test
    public void write1() throws Exception{
        // 创建配置对象
        Configuration conf = new Configuration();
        conf.set("dfs.client.use.datanode.hostname","true");
        // 定义统一资源标识符
        String uri = "hdfs://master:9000";
        // 创建文件系统对象(基于HDFS的文件系统)
        FileSystem fs = FileSystem.get(new URI(uri), conf,"root");
        // 创建路径对象(指向文件)
        Path path = new Path(uri + "/ied01/hadoop2.txt");
        // 创建文件系统数据字节输出流
        FSDataOutputStream out = fs.create(path);
        // 通过字节输出流向文件写数据
        out.write("Hello Hadoop World".getBytes());
        // 关闭输出流
        out.close();
        // 关闭文件系统对象
        fs.close();
        System.out.println("文件[" + path + "]写入成功");
    }
}

  • 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
  • 结果
    在这里插入图片描述
  • 利用HDFS集群WebUI查看
    在这里插入图片描述
2、将本地文件写入HDFS文件
  • 在项目根目录创建一个文本文件test.txt
    在这里插入图片描述
  • 创建create2()方法
@Test
    public void write2() throws Exception {
        // 创建配置对象
        Configuration conf = new Configuration();
        // 设置数据节点主机名属性
        conf.set("dfs.client.use.datanode.hostname", "true");
        // 定义uri字符串
        String uri = "hdfs://master:9000";
        // 创建文件系统对象
        FileSystem fs = FileSystem.get(new URI(uri), conf, "root");
        // 创建路径对象(指向目录或文件)
        Path path = new Path(uri + "/ied01/exam2.txt");
        // 创建文件系统数据字节输出流对象
        FSDataOutputStream out = fs.create(path);
        // 创建文件字符输入流对象
        FileReader fr = new FileReader("test.txt");
        // 创建缓冲字符输入流对象
        BufferedReader br = new BufferedReader(fr);
        // 定义行字符串
        String nextLine = "";
        // 通过循环读取缓冲字符输入流
        while ((nextLine = br.readLine()) != null) {
            // 在控制台输出读取的行
            System.out.println(nextLine);
            // 通过文件系统数据字节输出流对象写入指定文件
            out.write(nextLine.getBytes());
        }
        // 关闭文件系统字节输出流
        out.close();
        // 关闭缓冲字符输入流
        br.close();
        // 关闭文件字符输入流
        fr.close();
        // 提示用户写入文件成功
        System.out.println("本地文件[test.txt]成功写入[" + path + "]!");
    }
  • 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
  • 结果
    在这里插入图片描述
  • 其实这个方法的功能就是将本地文件复制(上传)到HDFS,有没有更简单的处理方法呢?有的,通过使用一个工具类IOUtils来完成文件的相关操作
  • 编写create2_()方法
@Test
    public void write2_() throws Exception {
        // 创建配置对象
        Configuration conf = new Configuration();
        // 设置数据节点主机名属性
        conf.set("dfs.client.use.datanode.hostname", "true");
        // 定义uri字符串
        String uri = "hdfs://master:9000";
        // 创建文件系统对象
        FileSystem fs = FileSystem.get(new URI(uri), conf, "root");
        // 创建路径对象(指向目录或文件)
        Path path = new Path(uri + "/ied01/test2.txt");
        // 创建文件系统数据字节输出流对象
        FSDataOutputStream out = fs.create(path);
        // 创建文件字节输入流对象
        FileInputStream in = new FileInputStream("test.txt");
        // 利用IOUtils类提供的字节拷贝方法来复制文件
        IOUtils.copyBytes(in, out, conf);
        // 关闭文件字节输入流
        in.close();
        // 关闭文件系统数据字节输出流
        out.close();
        // 关闭文件系统
        fs.close();
        // 提示用户写入文件成功
        System.out.println("本地文件[test.txt]成功写入[" + path + "]!");
    }
  • 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
  • 结果
    在这里插入图片描述
  • 查看/ied01/test.txt内容
    在这里插入图片描述

六、读取HDFS文件

  • net.xxr.hdfs包里创建ReadFileOnHDFS
    在这里插入图片描述
1、读取HDFS文件直接在控制台显示
  • 编写read1()方法
package net.xxr.hdfs;

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.junit.Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;

/*
功能:读取HDFS文件
作者:小小榕
日期:2022年11月30日
 */

public class ReadFileOnHDFS {
    @Test
    public void read1() throws Exception {
        // 创建配置对象
        Configuration conf = new Configuration();
        // 设置数据节点主机名属性
        conf.set("dfs.client.use.datanode.hostname", "true");
        // 定义uri字符串
        String uri = "hdfs://master:9000";
        // 创建文件系统对象
        FileSystem fs = FileSystem.get(new URI(uri), conf, "root");
        // 创建路径对象(指向目录或文件)
        Path path = new Path(uri + "/ied01/test2.txt");
        // 创建文件系统数据字节输入流对象
        FSDataInputStream in = fs.open(path);
        // 创建缓冲字符输入流对象,提高读取效率(字节流-->字符流-->缓冲流)
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        // 定义行字符串
        String nextLine = "";
        // 通过循环读取缓冲字符输入流
        while ((nextLine = br.readLine()) != null) {
            // 在控制台输出读取的行内容
            System.out.println(nextLine);
        }
        // 关闭缓冲字符输入流
        br.close();
        // 关闭文件系统数据字节输入流
        in.close();
        // 关闭文件系统
        fs.close();
    }
}


  • 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
  • 结果
    在这里插入图片描述
  • 利用IOUtils类简化代码
  • 创建read1_()测试方法
    在这里插入图片描述
2、读取HDFS文件,保存为本地文件
  • 任务:将/ied01/test2.txt下载到项目下download目录里
  • 创建download目录
    在这里插入图片描述
  • 创建read2()方法
@Test
    public void read2() throws Exception {
        // 创建配置对象
        Configuration conf = new Configuration();
        // 设置数据节点主机名属性
        conf.set("dfs.client.use.datanode.hostname", "true");
        // 定义uri字符串
        String uri = "hdfs://master:9000";
        // 创建文件系统对象
        FileSystem fs = FileSystem.get(new URI(uri), conf, "root");
        // 创建路径对象(指向目录或文件)
        Path path = new Path(uri + "/ied01/test2.txt");
        // 创建文件系统数据字节输入流对象
        FSDataInputStream in = fs.open(path);
        // 创建文件字节输出流
        FileOutputStream out = new FileOutputStream("download/exam.txt");
        // 读取HDFS文件(靠输入流),写入本地文件(靠输出流)
        IOUtils.copyBytes(in, out, conf);
        // 关闭文件系统数据字节输入流
        in.close();
        // 关闭文件字节输出流
        out.close();
        // 关闭文件系统
        fs.close();
        // 提示用户文件下载成功
        System.out.println("文件[" + path + "]下载到本地文件[download/exam.txt]!");
    }
  • 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
  • 结果
    在这里插入图片描述
    在这里插入图片描述
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/721824
推荐阅读
相关标签
  

闽ICP备14008679号