HDFS Java 客户端开发(开发环境:Windows)

HDFS是一个分布式文件系统,既然是文件系统,就可以对其中的文件进行操作,比如说新建文件夹、上传文件,重命名文件,下载文件,删除文件、列举所有文件等操作。

HDFSClient.java

package com.looc.client;

import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Before;
import org.junit.Test;

public class HDFSClient {

    private FileSystem fileSystem = null;

    @Before
    public void getFileSystem() throws IOException {
        // 配置参数的封装对象
        // 构造函数对xxx-site.xml文件进行解析,真实项目中需要将xxx-site.xml文件加载到工程中。
        Configuration configuration = new Configuration();
        configuration.set("fs.defaultFS", "hdfs://looc:9000");
        // 获取文件系统的客户端实例对象,根据configuration中的相关参数决定。
        fileSystem = FileSystem.get(configuration);
    }

    /**
     * 创建文件夹
     * 
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void testMkdir() throws IllegalArgumentException, IOException {
        fileSystem.mkdirs(new Path("/a/b/c"));
    }

    /**
     * 上传文件
     * 
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void testUpload() throws IllegalArgumentException, IOException {
        fileSystem.copyFromLocalFile(new Path("E:\\MapReduce\\WordCount.txt"), new Path("/a/b/c"));
    }

    /**
     * 重命名文件
     * 
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void testRename() throws IllegalArgumentException, IOException {
        fileSystem.rename(new Path("/a/b/c/WordCount.txt"), new Path("/a/b/c/WordCount.txt.rename"));
    }

    /**
     * 下载文件
     * 
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void testDownload() throws IllegalArgumentException, IOException {
        fileSystem.copyToLocalFile(new Path("/a/b/c/WordCount.txt.rename"), new Path("E:\\MapReduce"));
    }

    /**
     * 删除文件
     * 
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void testRmfile() throws IllegalArgumentException, IOException {
        boolean result = fileSystem.delete(new Path("/a/b/c/WordCount.txt.rename"), true);
        System.out.println(result ? "DELETE IS SUCCESSFULLY!" : "DELETE IS FAILD!");
    }

    /**
     * 列举所有文件
     * 
     * @throws FileNotFoundException
     * @throws IllegalArgumentException
     * @throws IOException
     */
    @Test
    public void testListFile() throws FileNotFoundException, IllegalArgumentException, IOException {
        RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), true);
        while (listFiles.hasNext()) {
            System.out.println(listFiles.next().getPath().getName());
        }
    }

}

Over

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值