HDFS API 使用②

24 篇文章 6 订阅
6 篇文章 0 订阅

由于为了减少代码的重复性所以我选择写2篇的第一篇是环境搭建 链接为:https://blog.csdn.net/qq_43791724/article/details/102895900 

初始化

    public static String  url="hdfs://hadoop01:8020";
    FileSystem fileSystem =null;
    Configuration configuration=null;

    /**
     * 初始化
     * @throws Exception
     */
    @Before
    public void init() throws Exception {
        configuration=    new Configuration();
        fileSystem = FileSystem.get(new URI(url),configuration,"hadoop");
    }

创建一个文件夹

   /**
     *创建文件夹
     * @throws Exception
     */
    @Test
    public void mkdir() throws Exception {
        fileSystem.mkdirs(new Path("/aa/bb"));
    }

创建一个文件并写入内容

  /**
     * 创建文件
     * @throws Exception
     */
    @Test
    public void create() throws Exception {
//      你要创建的文件路径
        FSDataOutputStream out = fileSystem.create(new Path("/usr/2.txt"));
//       输入字符发串内容
        out.writeUTF("hello word");
//        关闭资源
        out.flush();
        out.close();
    }

查看文件内容

  /**
     * 创建文件
     * @throws Exception
     */
    @Test
    public void create() throws Exception {
        FSDataOutputStream out = fileSystem.create(new Path("/usr/2.txt"));
//       输入字符发串内容
        out.writeUTF("hello word");
//        关闭资源
        out.flush();
        out.close();
    }

修改文件名称 

  /**
     * 修改文件名
     */
    @Test
    public void rename() throws IOException {
      // 第一个路径旧路径 
      // 第二个路径为改为
      fileSystem.rename(new Path("/user"),new Path("/usr"));

    }

拷贝本地文件到HDFS系统

 /**
     * 拷贝本地文件到HDFS系统
     * @throws IOException
     */
    @Test
    public void copyFromLocalFile() throws IOException {
        fileSystem.copyFromLocalFile(new Path("E:\\d12\\hadoop\\hdfs\\hadoop_hdfs02\\src\\2.txt"),
                new Path("/usr/2.txt"));
    }

拷贝HDFS到本地


    /**
     * 下载HDFS文件 下载到本地
     * @throws IOException
     */
    @Test
    public void copyToLocalFile() throws IOException {
       // HDFS 路径
        Path src = new Path("/usr/2.txt");
        // 本地 路径
        Path dst = new Path("E:\\d12\\hadoop\\hdfs\\hadoop_hdfs02\\src\\5.txt");
        fileSystem.copyToLocalFile(false,src,dst,true);
    }

删除HDFS系统山的文件

    @Test
    public void delete() throws IOException {
        fileSystem.delete(new Path("/hadoop-2.7.3.tar.gz"),true);
    }

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大数据老哥

欢迎支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值