Hadoop——HDFS实例

package com.atguigu.hdfs;

/*
客户端代码常用套路:
1、获取一个客户端对象
2、执行相关的操作命令
3、关闭资源
HDFS zookeeper
 */

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.lang.reflect.Array;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

import org.apache.hadoop.fs.FileSystem;

public class HdfsClient {

    private FileSystem fs;

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {

        //连接的集群nn地址
        URI uri = new URI("hdfs://hadoop102:8020");
        //配置一个文件
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication","2");
        //用户
        String user = "atguigu";

        //1、获取到了客户对象
        fs = FileSystem.get(uri, configuration, user);
    }

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

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

        //2、创建一个文件夹
        fs.mkdirs(new Path("/xiyou/huaguoshan1"));

    }
    /*
    参数优先级:从低到高
    hdfs-default.xml => hdfs-site.xml => 在项目资源目录下的配置文件 => 代码里面的配置
     */
    //上传
    @Test
    public void testPut() throws IOException {
        //参数解读:
        // 参数一:表示删除元数据;
        // 参数二:是否允许覆盖
        //参数三:元数据路径
        //参数四:目的地路径
        fs.copyFromLocalFile(false,true,new Path("E:\\sunwukong.txt"),new Path("hdfs://hadoop102/xiyou/huaguoshan"));
    }

    //文件下载
    @Test
    public void testGet() throws IOException {
        //参数的解读:
        // 参数一:原文件是否删除
        //参数二:原文件路径HDFS
        //参数三:目标地址路径win
        //参数四:
        fs.copyToLocalFile(true,new Path("hdfs://hadoop102/xiyou/huaguoshan/sunwukong.txt"),new Path("F:\\AI"),true);
    }

    //删除
    @Test
    public void testRm() throws IOException {
         /*
         参数解读:
         参数一:要删除的路径
         参数二:是否递归删除
          */
        fs.delete(new Path("hdfs://hadoop102/output2"),true);

        //删除空目录或文件,不需要递归
        fs.delete(new Path("hdfs://hadoop102/xiyou"),false);

        //删除非空目录,需要递归
        fs.delete(new Path("hdfs://hadoop102/jinguo"),true);
    }

    //文件的更名和移动
    @Test
    public void testMv() throws IOException {
        /*
        参数解读:
        参数一:源文件路径
        参数二:目标文件路径
         */
        //文件名称的修改
        fs.rename(new Path("hdfs://hadoop102/input/word.txt"),new Path("hdfs://hadoop102/input/ss.txt"));

        //文件的移动和更名
        fs.rename(new Path("hdfs://hadoop102/input/ss.txt"),new Path("hdfs://hadoop102/cls.txt"));

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

    }

    //获取文件详情
    @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.getBlockLocations());
            System.out.println(fileStatus.getPath().getName());

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

        }
    }

    //判断是文件夹还是文件
    @Test
    public void testFile() throws IOException {
        FileStatus[] listStatus = fs.listStatus(new Path("/"));
        for (FileStatus status : listStatus) {
            if (status.isFile()) {
                System.out.println("文件:"+status.getPath().getName());
            }else{
                System.out.println("目录:"+status.getPath().getName());
            }
        }
    }


}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值