首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDFS JAVAAPI总结

HDFS JAVAAPI总结

作者头像
Maynor
发布2022-11-30 13:53:16
3820
发布2022-11-30 13:53:16
举报

maven仓库

<dependencies>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-hdfs</artifactId>
        <version>2.7.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-mapreduce-client-core</artifactId>
        <version>2.7.5</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

javaApi

package com.nzqk.demo;

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

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

/**
 * @Version 1.0
 * @Author:zhaoJiaCai
 * @Date:2020/12/10 星期四   07:59
 */
public class demo_03 {


    /**
     * 创建文件夹
     */
    @Test
    public void demo_01() {

        Configuration conf = new Configuration();
        boolean boo = false;
        try {
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.10.3:8020"), conf);
            boo = fs.createNewFile(new Path("/111.txt"));
        } catch (Exception ex) {
        }

        if (boo) {
            System.out.println("创建文件成功!");
        } else {
            System.out.println("创建文件失败!");
        }
    }

    /**
     * 修改
     */
    @Test
    public void demo_02() {
        Configuration conf = new Configuration();
        boolean boo = false;
        try {
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.10.3:8020"), conf);
            boo = fs.rename(new Path("/111.txt"), new Path("/222.txt"));
        } catch (Exception ex) {
        }

        if (boo) {
            System.out.println("修改文件成功!");
        } else {
            System.out.println("修改文件失败!");
        }
    }

    /**
     * 查看   所有文件
     */
    @Test
    public void demo_03() {

        try {
            //1 获取文件系统
            Configuration configuration = new Configuration();
            FileSystem fs = FileSystem.get(new URI("hdfs://192.168.10.3:8020"), configuration, "root");

            // 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();
        } catch (Exception ex) {
        }
    }


    /**
     * 查看 遍历根目录下的所有文件 文件夹
     */
    @Test
    public void testListStatus() throws IOException, InterruptedException, URISyntaxException {

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

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

        for (FileStatus fileStatus : listStatus) {

            System.out.println("============================");

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

            } else {
                System.out.println("d:" + fileStatus.getPath().getName());
            }

            //获取路径
            System.out.println(fileStatus.getPath().getParent().toString());

            String str = fileStatus.getPath().getParent().toString();
            String[] split = str.split("\\/");

            str = "";
            for (int i = 3; i < split.length; i++) {
                str += "/" + split[i];
            }

            System.out.println(str);

            // 长度
            System.out.println(fileStatus.getLen());

            // 权限
            System.out.println(fileStatus.getPermission());

            // 分组
            System.out.println(fileStatus.getGroup());


            System.out.println("文件的块大小: " + (fileStatus.getBlockSize() / 1024 / 1024));
            System.out.println("目录所有者:  " + fileStatus.getOwner());
            System.out.println("目录备份数: " + fileStatus.getReplication());


        }

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

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

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

        // 2 执行删除
        boolean delete = fs.delete(new Path("/222.txt"), true);

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

    /**
     * 下载
     */
    @Test
    public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException {

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

        // 2 执行下载操作
        // boolean delSrc 指是否将原文件删除
        // Path src 指要下载的文件路径
        // Path dst 指将文件下载到的路径
        // boolean useRawLocalFileSystem 是否开启文件校验
        fs.copyToLocalFile(false, new Path("/demo_01/part-m-00000"), new Path("e:/1.txt"), true);

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


    /**
     * 上传
     */
    @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.10.3:8020"), configuration, "root");

        // 2 上传文件
        fs.copyFromLocalFile(new Path("e:/A.png"), new Path("/A.png"));

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

        System.out.println("over");
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-12-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • maven仓库
  • javaApi
相关产品与服务
云硬盘
云硬盘(Cloud Block Storage,CBS)为您提供用于 CVM 的持久性数据块级存储服务。云硬盘中的数据自动地在可用区内以多副本冗余方式存储,避免数据的单点故障风险,提供高达99.9999999%的数据可靠性。同时提供多种类型及规格,满足稳定低延迟的存储性能要求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档