HDFS常用API操作、常用方法

将建立链接放在before(),关闭链接放在after()

package xyz.youngit.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;

public class HDFSClient {
    FileSystem fileSystem;

    /*
     * @description 建立链接
     * @param []
     * @return void
     */
    @Before
    public void before() throws IOException, InterruptedException {
        Configuration configuration = new Configuration();
//        configuration.set("dfs.replication","2");

        //1.新建HDFS对象
        fileSystem = FileSystem.get(URI.create("hdfs://hadoop129:8020"),
                configuration,"youngit");
    }

    /*
     * @description 上传
     * @param []
     * @return void
     */
    @Test
    public void put() throws IOException, InterruptedException {
        //2.操作集群
        fileSystem.copyFromLocalFile(new Path("D:\\testFolder"),
                new Path("/"));
    }

    // boolean delSrc 指是否将原文件删除
    // Path src 指要下载的文件路径
    // Path dst 指将文件下载到的路径
    // boolean useRawLocalFileSystem 是否开启文件校验
    @Test
    public void down() throws IOException, InterruptedException {
        //2.操作集群
        fileSystem.copyToLocalFile(false,new Path("/test.txt"),
                new Path("d:/test1.txt"),true);
    }

    /*
     * @description 删除操作
     * @param []
     * @return void
     */
    @Test
    public void delete() throws IOException, InterruptedException {
        //2.操作集群
        fileSystem.delete(new Path("/test2.txt"),true);
    }

    /*
     * @description 文件名更改
     * @param []
     * @return void
     */
    @Test
    public void testRename() throws IOException, InterruptedException {
        //2.操作集群
        fileSystem.rename(new Path("/test.txt"),new Path("/test1.txt"));
    }

    /*
     * @description 查看文件名称、权限、长度、块信息
     * @param []
     * @return void
     */
    @Test
    public void testListFiles() throws IOException {
        // 2 获取文件详情
        RemoteIterator<LocatedFileStatus> listFiles = fileSystem.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("-----------分割线------------");
        }
    }

    /*
     * @description 文件和文件夹的判断
     * @param []
     * @return void
     */
    @Test
    public void testListStatus() throws IOException, InterruptedException{
        // 2 判断是文件还是文件夹
        FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值