HDFS的API操作

package com.lijiankun.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;

/*

  • 客户端代码常用规则

  • 1.获取客户端对象

  • 2.执行相关操作命令

  • 3.关闭资源

  • 此规则适用于 HDFS ZOOKEEPER

  • */
    public class HdfsClient {

    private FileSystem fs;

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
    //连接集群的NameNode地址
    URI uri = new URI(“hdfs://hadoop02:8020”);
    //创建一个配置文件
    Configuration configuration = new Configuration();
    //测试参数优先级
    configuration.set(“dfs.replication”, “2”);
    //用户名
    String user = “root”;
    //获取客户端对象
    fs = FileSystem.get(uri, configuration,user);
    }

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

    @Test
    public void testMkdir() throws URISyntaxException, IOException, InterruptedException {
    //创建文件夹
    fs.mkdirs(new Path(“/xiyou/huaguoshan”));
    }

    // 测试上传
    @Test
    public void testPut() throws IOException {
    //参数解读:1.是否删除原数据 2.是否允许覆盖 3.原数据路径 4.目的地路径
    fs.copyFromLocalFile(
    false,true,
    new Path(“D:\sunwukong.txt”),
    new Path(“/xiyou/huaguoshan”));
    }

    //测试下载
    @Test
    public void testGet() throws IOException {
    //参数解读:1.是否删除原数据 2.源文件路径(HDFS中的) 3.目标地址路径(Windows中的) 4.是否开启文件校验
    fs.copyToLocalFile(
    false
    ,new Path(“/xiyou/huaguoshan/sunwukong.txt”),
    new Path(“D:\sunwukong.txt”),
    false);
    }

    //测试删除
    @Test
    public void testRm() throws IOException {
    //删除文件: 参数解读:1.文件路径 2.是否递归删除
    // fs.delete(
    // new Path(“/xiyou/huaguoshan/sunwukong.txt”),
    // false);

      //删除空目录:     参数解读:1.文件路径 2.是否递归删除
    

// fs.delete(
// new Path(“/xiyou/huaguoshan/”),
// false);

    //删除非空目录:     参数解读:1.文件路径 2.是否递归删除
    fs.delete(
            new Path("/jinguo/"),
            true);
}

//文件的更名和移动
@Test
public void testMove() throws IOException {

    //文件更名
    //参数解读: 参数1:源文件路径   参数2:目标文件路径

// fs.rename(new Path(“/wcinput/word.txt”),new Path(“/wcinput/wordRnamed.txt”));

    //文件移动
    //参数解读: 参数1:源文件路径   参数2:目标文件路径

// fs.rename(new Path(“/wcinput/wordRnamed.txt”),new Path(“/word.txt”));

    //目录的移动和更名
    //参数解读: 参数1:源文件路径   参数2:目标文件路径
    fs.rename(new Path("/wcinput/"),new Path("/wcinput2/"));
}

//获取文件详情信息
@Test
public void fileDetail() throws IOException {

    //获取所有文件信息
    //参数解读  1.路径  2. 是否递归查看子目录
    RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);

    while (listFiles.hasNext()) {
        LocatedFileStatus file = listFiles.next();
        System.out.println("================"+file.getPath()+"===============");
        System.out.println(file.getPermission());
        System.out.println(file.getOwner());
        System.out.println(file.getGroup());
        System.out.println(file.getLen());
        System.out.println(file.getModificationTime());
        System.out.println(file.getReplication());
        System.out.println(file.getBlockSize());
        System.out.println(file.getPath().getName());

        //获取块信息
        BlockLocation[] blockLocations = file.getBlockLocations();
        System.out.println(Arrays.toString(blockLocations));
    }
}

//判断是文件夹还是文件
@Test
public void testFile() throws IOException {

    //参数解读 1.文件路径
    FileStatus[] fileStatuses = fs.listStatus(new Path("/"));

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值