Hadoop之Java客户端操作HDFS

public class HdfsUtil {

    private FileSystem fs = null;

    @Before
    public void init() throws Exception {
        //读取classpath下的core-site.xml配置文件
        Configuration conf = new Configuration();
        //手动封装配置中需要读取的信息
        conf.set("fs.defaultFS", "hdfs://192.168.2.100:9000");
        //根据配置信息获取一个具体的客户端实例对象
        fs = FileSystem.get(new URI("hdfs://192.168.2.100:9000"), conf, "hadoop");
    }

    @Test
    //通过流上传文件
    public void uploadByStream() throws IOException {
        Path path = new Path("hdfs://192.168.2.100:9000/user/hadoop/mark.txt");
        FSDataOutputStream dataOut = fs.create(path);
        FileInputStream dataIn = new FileInputStream("c://mark.txt");
        IOUtils.copy(dataIn, dataOut);
    }

    @Test
    //通过封装好方法上传文件
    public void uploadByMethods() throws IOException {
        fs.copyFromLocalFile(new Path("c://mark.txt"), new Path("hdfs://192.168.2.100:9000/user/hadoop/mark.txt"));
    }


    @Test
    //下载文件
    public void download() throws IOException {
        fs.copyToLocalFile(new Path("hdfs://192.168.2.100:9000/user/hadoop/mark.txt"),new Path("d://JAVA"));
    }


    @Test
    //查看文件
    public void listFiles() throws IOException {
        RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);
        while (files.hasNext()){
            LocatedFileStatus file = files.next();
            Path path = file.getPath();
            System.out.println(path.getName());
        }
        System.out.println("------------------");
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses){
            String name = fileStatus.getPath().getName();
            System.out.println(name);
        }
    }



    @Test
    //创建文件夹
    public void mkdir() throws IOException {
        fs.mkdirs(new Path("/user/mark"));
    }


    @Test
    //删除文件或文件下
    public void rm() throws IOException {
        fs.delete(new Path("/user/mark"),true);
    }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值