美文网首页
Java代码操作HDFS

Java代码操作HDFS

作者: 虫儿飞ZLEI | 来源:发表于2019-07-14 19:28 被阅读0次

1. 创建文件夹示例

1.1 环境准备

java

hadoop
解压hadoop到本地,配置环境变量

image.png

1.2 新建maven项目

引入依赖:

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.8</version>
            <scope>system</scope>
            <systemPath>C:\SoftWare\Java\jdk1.8.0_211\lib\tools.jar</systemPath>
        </dependency>
    </dependencies>

编写客户端代码:

    @Test
    public void testMkdirs() throws IOException, InterruptedException, URISyntaxException {

        // 1 获取文件系统
        Configuration configuration = new Configuration();

        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");

        // 2 创建目录
        fs.mkdirs(new Path("/ff/qq/qq"));

        // 3 关闭资源
        fs.close();
    }

more:

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

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

public class HdfsClient{
    /**
     * 创建文件夹
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void testMkdirs() throws IOException, InterruptedException, URISyntaxException {

        // 1 获取文件系统
        Configuration configuration = new Configuration();

        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");

        // 2 创建目录
        fs.mkdirs(new Path("/ff/qq/qq"));

        // 3 关闭资源
        fs.close();
    }

    /**
     * 从本地上传文件
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {

        // 1 获取文件系统
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication", "2");
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");

        // 2 上传文件
        fs.copyFromLocalFile(new Path("C:\\Users\\zl\\Desktop\\banzhang.txt"), new Path("/banzhang.txt"));

        // 3 关闭资源
        fs.close();

        System.out.println("over");
    }

    /**
     * 下载文件到本地
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");

        // 2 执行下载操作
        // boolean delSrc 指是否将原文件删除
        // Path src 指要下载的文件路径
        // Path dst 指将文件下载到的路径
        // boolean useRawLocalFileSystem 是否开启文件校验
        fs.copyToLocalFile(false, new Path("/banzhang.txt"), new Path("C:\\Users\\zl\\Desktop\\banzhang2.txt"), true);

        // 3 关闭资源
        fs.close();
    }

    /**
     * 删除文件夹
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void testDelete() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");

        // 2 执行删除
        fs.delete(new Path("/test"), true);

        // 3 关闭资源
        fs.close();
    }

    /**
     * 重命名文件
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void testRename() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");

        // 2 修改文件名称
        fs.rename(new Path("/banzhang.txt"), new Path("/banhua.txt"));

        // 3 关闭资源
        fs.close();
    }


    /**
     * 列出文件信息
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void testListFiles() throws IOException, InterruptedException, URISyntaxException{

        // 1获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");

        // 2 获取文件详情
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);

        while(listFiles.hasNext()){
            LocatedFileStatus status = listFiles.next();

            // 输出详情
            // 文件名称
            System.out.println(status.getPath().getName());
            // 长度
            System.out.println(status.getLen());
            // 权限
            System.out.println(status.getPermission());
            // 分组
            System.out.println(status.getGroup());

            // 获取存储的块信息
            BlockLocation[] blockLocations = status.getBlockLocations();

            for (BlockLocation blockLocation : blockLocations) {

                // 获取块存储的主机节点
                String[] hosts = blockLocation.getHosts();

                for (String host : hosts) {
                    System.out.println(host);
                }
            }

            System.out.println("-----------分割线----------");
        }

        // 3 关闭资源
        fs.close();
    }

    /**
     * 判断文件是文件还是 文件夹
     * @throws IOException
     * @throws InterruptedException
     * @throws URISyntaxException
     */
    @Test
    public void testListStatus() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取文件配置信息
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.91.129:9000"), configuration, "zl");

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

        for (FileStatus fileStatus : listStatus) {

            // 如果是文件
            if (fileStatus.isFile()) {
                System.out.println("f:"+fileStatus.getPath().getName());
            }else {
                System.out.println("d:"+fileStatus.getPath().getName());
            }
        }

        // 3 关闭资源
        fs.close();
    }

}

相关文章

  • Java代码操作HDFS

    1. 创建文件夹示例 1.1 环境准备 java hadoop解压hadoop到本地,配置环境变量 1.2 新建m...

  • HDFS的JAVA api操作——学习总结

    目录一、简单JAVA知识二、HDFS的JAVA api操作三、我的学习代码四、我的问题 一、简单JAVA知识 因为...

  • Hadoop HDFS文件操作API

    使用JAVA操作HDFS: 使用Shell操作HDFS: Usage: hadoop fs [generic op...

  • HDFS 配置本地客户端

    之前,一直通过Linux命令操作HDFS。接下来,在本地配置HDFS客户端,通过编写代码操作HDFS。 环境: m...

  • java操作HDFS

    操作步骤 IDEA + MAVEN 创建JAVA工程本地安装maven,IDEA配置maven并创建相应的mave...

  • hadoop笔记3--hdfs操作

    这篇文章先记录一下在java代码中对hdfs的基本操作。 打开eclipse,新建一个Map/Reduce工程。后...

  • 使用IO流操作HDFS

    除了可以使用系统API进行HDFS操作,还可以通过Java的IO流进行文件的上传和下载。适用于HDFS的自定义操作...

  • java api操作HDFS

    如果是使用maven的话,导入如下依赖即可,否则需要在解压好的hadoop文件夹下找到common文件夹和hdfs...

  • java api操作HDFS

    如果是使用maven的话,导入如下依赖即可,否则需要在解压好的hadoop文件夹下找到common文件夹和hdfs...

  • java api 操作hdfs

    引入pom 依赖。hadoop-client 版本对应 cdh版本

网友评论

      本文标题:Java代码操作HDFS

      本文链接:https://www.haomeiwen.com/subject/iuuikctx.html