每日总结

HDFS 的 API 操作

package com.atguigu.hdfs;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;
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;

import static com.google.gson.internal.bind.TypeAdapters.URI;

public class HdfsClient {

    private FileSystem fs;
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        URI uri=new URI("hdfs://hadoop102:8020");
        // 1 获取文件系统

        Configuration configuration = new Configuration();
        // FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration);
        fs = FileSystem.get(uri, configuration,"chenghaixiang");
    }
    @After
    // 3 关闭资源
    public void close() throws IOException {
        fs.close();
    }

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

        // 2 创建目录
        fs.mkdirs(new Path("/xiyou/huaguoshan/"));

    }
    @Test
    //上传
    public void testPut() throws IOException {
        //1.表示删除原数据,2.是否覆盖,3.原数据路径,4.目标数据路径
        fs.copyFromLocalFile(false,false,new Path("D:\\student1.txt"),new Path("hdfs://hadoop102/xiyou/huaguoshan"));
    }
    @Test
    //文件下载
    public void testGet() throws IOException {
        //1.原文件是否删除 2.原文件hdfs路径 3.目标地址路径 4.是否不进行crc校验
        fs.copyToLocalFile(false,new Path("hdfs://hadoop102/xiyou/huaguoshan"),new Path("D:\\"),false);
    }
    @Test
    //删除
    public void testRm() throws IOException {
        //1.删除路径 2.是否递归删除
        fs.delete(new Path("/xiyou/huaguoshan/student1.txt"),false);
        //删除非空目录
        /*fs.delete(new Path("/xiyou/huaguoshan/"),true);*/
    }
    @Test
    //文件更名和移动
    public void testmv() throws IOException {
        //1.原文件路径 2.目标路径
        //对文件名修改
        /*fs.rename(new Path("/xiyou/huaguoshan/student1.txt"),new Path("/xiyou/huaguoshan/chenghaixiangstudent.txt"));*/
        //对文件的移动和修改
        fs.rename(new Path("/xiyou/huaguoshan/chenghaixiangstudent.txt"),new Path("/xiyou/huaguoshan1/xx.txt"));
        //目录的更名
        //fs.rename(new Path("/xiyou/huaguoshan"),new Path("/xiyou/huaguoshanchenghai"));
    }

    //获取文件详细信息
    @Test
    public void fileDetail() 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));

        }
    }
}

 

 

 

 

posted @ 2021-09-16 23:33  chenghaixinag  阅读(37)  评论(0编辑  收藏  举报