Hadoop之HDFS的API操作

1、环境准备

1.1、下载window版本的hadoop-3.1.0

在这里插入图片描述

1.2、配置HADOOP_HOME环境变量

Hadoop之HDFS的API操作_第1张图片

1.2、配置Path环境变量

Hadoop之HDFS的API操作_第2张图片

2、代码演示

2.1、创建maven项目,导入pom坐标

Hadoop之HDFS的API操作_第3张图片

<dependencies>
        <dependency>
            <groupId>org.apache.hadoopgroupId>
            <artifactId>hadoop-clientartifactId>
            <version>3.1.3version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
            <version>1.7.30version>
        dependency>
    dependencies>

2.2、日志配置

log4j.rootLogger=INFO, stdout
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/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

2.3、代码实现

package com.song.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

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

public class HdfsClient {

    private FileSystem fs;

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        // hdfs://hadoop102:8020 是nameNode的通信地址
        URI uri = new URI("hdfs://hadoop102:8020");
        //  获取文件系统
        Configuration configuration = new Configuration();
        // 定义具有操作权限的用户
        String user = "song";
        //  获取客户端对象
        fs = FileSystem.get(uri, configuration, user);
    }

    @After
    public void close() throws IOException {
        // 关闭资源
        fs.close();
    }

    //测试创建目录
    @Test
    public void testMkdirs() throws IOException, URISyntaxException, InterruptedException {

        // 业务操作
        fs.mkdirs(new Path("/xiyou/huaguoshan/"));
    }

    //测试上传
    @Test
    public void testPut() throws IOException, URISyntaxException, InterruptedException {

        // 业务操作
        /* *
         * 第一个参数:是否删除源文件
         * 第二个参数:目标文件存在,是否覆盖
         * 第三个参数:源文件地址
         * 第四个参数:目标文件地址
         * 参数优先级  从左到右 从低到高
         * hdfs-default.xml  ==>  hdfs-site.xml ==> 在项目资源目录下的配置文件 ==> 代码里面的配置
         */
        fs.copyFromLocalFile(false, true, new Path("D:\\test_data\\sunwukong.txt"), new Path("/xiyou/huaguoshan"));
    }


    //测试下载
    @Test
    public void testGet() throws IOException, URISyntaxException, InterruptedException {

        // 业务操作
        /* *
         * 第一个参数:是否删除源文件
         * 第二个参数:源文件地址
         * 第三个参数:目标文件地址
         * 第四个参数:是否开启文件校验
         * 参数优先级  从左到右 从低到高
         * hdfs-default.xml  ==>  hdfs-site.xml ==> 在项目资源目录下的配置文件 ==> 代码里面的配置
         */
        fs.copyToLocalFile(false, new Path("/xiyou/huaguoshan/sunwukong"), new Path("D:\\test_data\\sunwukong"), false);
    }

    //测试删除
    @Test
    public void testRm() throws IOException, URISyntaxException, InterruptedException {

        // 业务操作
        /* *
         * 第一个参数:是否删除源文件
         * 第二个参数:源文件地址
         */
        fs.delete(new Path("/xiyou"), true);
    }

    //测试删除
    @Test
    public void testMv() throws IOException, URISyntaxException, InterruptedException {

        // 业务操作
        /* *
         * 第一个参数:源文件
         * 第二个参数:目标文件
         */
        fs.rename(new Path("/jinguo/shuguo.txt"), new Path("/shuguo1.txt"));
    }


    //  获取文件详情
    @Test
    public void testListFiles() throws IOException {
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
        while (listFiles.hasNext()) {
            LocatedFileStatus fileStatus = listFiles.next();

            System.out.println("========" + fileStatus.getPath() + "=========");
            System.out.println(fileStatus.getPermission());
            System.out.println(fileStatus.getOwner());
            System.out.println(fileStatus.getGroup());
            System.out.println(fileStatus.getLen());
            System.out.println(fileStatus.getModificationTime());
            System.out.println(fileStatus.getReplication());
            System.out.println(fileStatus.getBlockSize());
            System.out.println(fileStatus.getPath().getName());

            // 获取块信息
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            System.out.println(Arrays.toString(blockLocations));
        }
    }
    @Test
    public void testListStatus() throws IOException, InterruptedException, URISyntaxException{

        // 2 判断是文件还是文件夹
        FileStatus[] listStatus = fs.listStatus(new Path("/"));

        for (FileStatus fileStatus : listStatus) {
            // 如果是文件
            if (fileStatus.isFile()) {
                System.out.println("文件:"+fileStatus.getPath().getName());
            }else {
                System.out.println("目录:"+fileStatus.getPath().getName());
            }
        }
    }


}


你可能感兴趣的:(大数据系列,hadoop,hdfs,big,data)