Hadoop_API文件下载文件删除文件移动、更名

news/2024/5/20 2:31:41 标签: hadoop, hdfs, big data

1、完整代码

package com.atguigu.hdfs;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HdfsClient {
    private FileSystem fs;

    @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";
        //1.获取到客户端对象
        fs = FileSystem.get(uri, configuration,user);
    }

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


    /**
     * 文件下载
     */
    @Test
    public void testGet() throws IOException {
        //参数解读:参数1:表示是否删除原数据;参数2:原数据路径;参数3:目的地路径;参数4:校验
        fs.copyToLocalFile(true,new Path("hdfs://hadoop102/xiyou/huaguoshan"),new Path("D:\\code\\Hadoop"),true);
    }

    /**
     * 删除
     */
    @Test
    public void testRm() throws IOException {
        //参数解读:参数1:要删除的路径;参数2:是否递归删除
        //1、删除文件
        fs.delete(new Path("/1.txt"),false);

        //2、删除空目录
        fs.delete(new Path("/output2"),false);

        //3、删除非空目录(要递归删除)
        fs.delete(new Path("/jinguo"),true);
    }

    /**
     * 文件的更名和移动
     */
    @Test
    public void testmv() throws IOException {
        //参数解读:参数1:原文件路径;参数2:目标文件路径
        //对文件名称的修改
        fs.rename(new Path("/input/word.txt"),new Path("/input/ss.txt"));

        //文件移动、更名
        fs.rename(new Path("/input/ss.txt"),new Path("/1.txt"));

        //目录更名
        fs.rename(new Path("/input"),new Path("/ouput"));
    }
}

2、 各个功能

(1)文件下载

    /**
     * 文件下载
     */
    @Test
    public void testGet() throws IOException {
        //参数解读:参数1:表示是否删除原数据;参数2:原数据路径;参数3:目的地路径;参数4:校验
        fs.copyToLocalFile(true,new Path("hdfs://hadoop102/xiyou/huaguoshan"),new Path("D:\\code\\Hadoop"),true);
    }

(2) 删除(分文件和目录)

    /**
     * 删除
     */
    @Test
    public void testRm() throws IOException {
        //参数解读:参数1:要删除的路径;参数2:是否递归删除
        //1、删除文件
        fs.delete(new Path("/1.txt"),false);

        //2、删除空目录
        fs.delete(new Path("/output2"),false);

        //3、删除非空目录(要递归删除)
        fs.delete(new Path("/jinguo"),true);
    }

(3)文件的更名和移动

    /**
     * 文件的更名和移动
     */
    @Test
    public void testmv() throws IOException {
        //参数解读:参数1:原文件路径;参数2:目标文件路径
        //对文件名称的修改
        fs.rename(new Path("/input/word.txt"),new Path("/input/ss.txt"));

        //文件移动、更名
        fs.rename(new Path("/input/ss.txt"),new Path("/1.txt"));

        //目录更名
        fs.rename(new Path("/input"),new Path("/ouput"));
    }
}


http://www.niftyadmin.cn/n/1303263.html

相关文章

Hadoop_API获取文件详细信息判断是文件还是文件夹

1.获取文件详细信息 /*** 获取文件详细信息*/Testpublic void fileDetail() throws IOException {//参数解读&#xff1a;参数1&#xff1a;路径&#xff1b;参数2&#xff1a;是否递归RemoteIterator<LocatedFileStatus> listFiles fs.listFiles(new Path("/"…

Hadoop_MapReduce_WordCount案例错误:Shuffle$ShuffleError: error in shuffle in localfetcher#1

参考map100% reduce0%&error in shuffle in localfetcher#1_闲人编程的博客-CSDN博客 错误原因是电脑用户名有个空格&#xff0c;采用更改电脑用户名的方法有效解决 &#xff0c;或者在代码里设置&#xff08;没尝试&#xff09;

Hadoop_MapReduce_WordCount案例

目录 1、需求 &#xff08;1&#xff09;输入数据 &#xff08;2&#xff09;期望输出数据 2、实现&#xff08;本地测试&#xff09; &#xff08;1&#xff09;环境准备 1&#xff09;创建maven工程&#xff0c;MapReduceDemo&#xff08;maven官网下载maven&#xff0c…

Hadoop序列化案例实操

目录 1.需求 2.需求分析 3.自定义对象和三个类的程序 &#xff08;1&#xff09;编写流量统计的Bean对象 &#xff08;2&#xff09;编写Mapper类 &#xff08;3&#xff09;编写Reducer类 &#xff08;4&#xff09;编写Driver驱动类 3.结果 1.需求 统计每一个手机号耗…

Hadoop_MapReduce_Partition分区

shuffle是通过分区partitioner 分配给Reduce&#xff0c;一个partition对应一个Reduce&#xff0c;Partitioner是shuffle的一部分。 1.默认Partition分区 默认分区是根据key的hashCode对ReduceTasks个数取模得到的&#xff0c;用户没法控制哪个key存储到哪个分区。 2.自定义Pa…

IDEA 快捷键拆解系列(十三):Window 篇

这是IDEA快捷键拆解系列的第十三篇。 以下是关于Window导航项及其每一子项的拆解介绍&#xff0c;其中&#xff0c;加粗部分的选项是博主认为比较重要的。 Window Quick Run Root Maven Goal Ctrl Shift Alt RQuick Run Maven Goal Ctrl Alt RStore Current Layout as De…

Hadoop_MapReduce_Partition分区案例实操

目录 1.需求 2.需求分析 3.代码 &#xff08;1&#xff09;在之前的序列化案例实操的基础上&#xff0c;增加一个分区类 &#xff08;2&#xff09; 在driver类中增加自定义数据分区设置和ReduceTask设置 1.需求 将统计结果按照手机归属地不同省份输出到不同文件中&#x…

Hadoop_MapReduce_Shuffle机制

Map方法之后&#xff0c;Reduce方法之前的数据处理过程称之为Shuffle。 理解&#xff1a; 1.Map方法得<k,v>数据&#xff0c;进行分区标记后存入环形缓冲区&#xff0c;图中环形缓冲区左边箭头是索引写入&#xff0c;右边箭头是数据写入&#xff0c;当环形缓冲区的容量达…