hdfs相关API

hdfs 服务端shell命令

hadoop fs -moveFromLocal 

hadoop fs   -copyFromLocal            hadoop fs -put

hadoop fs -appendToFile

hadoop fs  -copyToLocal             -get



hadoop fs -ls                      查询目录

hadoop fs -cat                     输出

hadoop  fs  -chgrp -chmod -chown   修改文件权限

hadoop fs -mkdir                   创建文件夹

hadoop fs -cp                      从hdfs的一个地方拷贝到另外一个地方

Hadoop fs -mv                      从hdfs的一个地方移动到另外的一个地方

hadoop fs -tail                    查看文件最后一kb

hadoop fs -rm                      删除文件

hadoop fs -rm -r                   递归删除文件及里面的内容

hadoop fs -du -s -h                统计文件夹的大小信息

hadoop fs -du -h                   统计文件具体大小

hadoop fs -setrep                  设置文件副本数量,只有当datanode的数量增加到对应的数量的时候,副本数才会生效

相关API客户端操作

/**
 * @auther :atom
 * @date :2022/1/21 23:17
 */

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

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        //链接的参数
        URI uri = new URI("hdfs://hadoop102:8020");
        //创建一个配置文件
        Configuration configuration = new Configuration();

        configuration.set("dfs.replication", "2");
        //获取到客户端对象
        String user = "atguigu";
        fileSystem = FileSystem.get(uri, configuration, user);


    }

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


    }

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

        //创建一个文件夹作
        fileSystem.mkdirs(new Path("/xiyou/huaguoshan2"));

    }

    @Test
    public void testput() throws IOException {
        //上传一个文件:
        //参数一表示删除元数据,参数二表示是否允许覆盖,第三个参数表示元数据路径,参数四表示目的地址路径
        fileSystem.copyFromLocalFile(false, true,
                new Path("f:\\sunwukong.txt"),
                new Path("hdfs://hadoop102/xiyou/huaguoshan"));
    }

    @Test
    public void testput2() throws IOException {
        fileSystem.copyFromLocalFile(false, true, new Path("f:\\sunwukong.txt"),
                new Path("hdfs://hadoop102/xiyou/huaguoshan/sunwukong"));
    }
    /*参数优先级
     * hdfs-default.xml=>hdfs-site.xml=>resources目录下的配置文件优先级=>代码里面的配置默认值最高
     * */

    //文件下载
    /*
     * 关于路径问题,/文件夹名 作为srcpath下载下来是一个文件夹
     * /问价夹名/文件名 下载下来是一个文件
     * /问价夹名/文件名 上传上去是一个具有完整目录的文件,且会自动创建缺失的目录
     * 若是上传时的路径不带后缀
     * */
    @Test
    public void testget() throws IOException {
        //参数解读,参数一表示源文件是否删除,参数二表示源文件的路径,参数三表示目标地址路径,参数四表示是否校验
        fileSystem.copyToLocalFile(true, new Path("hdfs://hadoop102/xiyou/huaguoshan1"),
                new Path("f:\\"), false);
    }

    //文件删除
    /*
     * 参数问题;参数一表示要删除的路径,参数二表示是否递归删除
     * */
    @Test
    public void testrm() throws IOException {
        //删除文件
        fileSystem.delete(new Path("hdfs://Hadoop102/jdk-8u212-linux-x64.tar.gz"), false);

        //删除空目录
        fileSystem.delete(new Path("hdfs://Hadoop102/xiyou"), false);
        //删除非空目录,如果不使用递归删除就会返回 PathIsNotEmptyDirectoryException
        fileSystem.delete(new Path("hdfs://Hadoop102/output"), true);
    }

    //测试文件的更名和移动
    //参数解读:第一个参数表示源文件路径,参数二表示目标文件路径
    @Test
    public void testmove() throws IOException {
        //文件的更名和移动
        fileSystem.rename(new Path("hdfs://hadoop102/input/xx.txt"),
                new Path("hdfs://hadoop102/input/word.txt"));

        //目录的更名
        fileSystem.rename(new Path("hdfs://hadoop102/cls"),
                new Path("hdfs://hadoop102//input"));

    }

    //查看hdfs上的文件权限信息
    //参数问题:第一个参数表示文件路径,第二个参数表示是否递归
    @Test
    public void testfiledetail() throws IOException {
        //获取所有文件信息
        RemoteIterator<LocatedFileStatus> listfiles = fileSystem.listFiles(new Path("hdfs://hadoop102/input/"), 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));

        }
    }


    //文件夹和文件的判断
    @Test
    public void testboolfile() throws IOException {
        FileStatus[] listStatus = fileSystem.listStatus(new Path("hdfs://hadoop102/"));

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

        }
    }
}

posted @ 2022-01-21 23:04  ftwftw  阅读(74)  评论(0)    收藏  举报