大数据开发系列(四)----HDFS的增删改查代码

package com.xiaoye.hdfsTest;

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

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

public class DFSClientTest {
   //1、创建文件夹
    @Test
    public  void  mkdirFile() throws URISyntaxException, IOException, InterruptedException {
        URI uri = new URI("hdfs://xiaoye11:8020");
        //创建对象
        Configuration configuration = new Configuration();
        String  user="root";

        //获取客户端对象
        FileSystem  fs= FileSystem.get(uri, configuration,user);
        //创建文件夹
        fs.mkdirs(new Path("/xiaoyeText"));
        //关闭资源
        fs.close();
        System.out.println("文件夹创建成功");
    }

    //2、复制上传文件
    @Test
    public void  copyFromLocalFileTest() throws URISyntaxException, IOException, InterruptedException{
        URI uri = new URI("hdfs://xiaoye11:8020");
        //创建对象
        Configuration configuration = new Configuration();
        String  user="root";

        //获取客户端对象
        FileSystem  fs= FileSystem.get(uri, configuration,user);
        Path src = new Path("F://xiaoye_Test//long.txt");
        Path dest = new Path("/xiaoyeText/xx.txt");
        fs.copyFromLocalFile(src,dest);
         //关闭资源
        fs.close();
        System.out.println("上传成功");
    }

   //3、下载文件到本地
    @Test
    public void copyToLocalTest()  throws URISyntaxException, IOException, InterruptedException{
        URI uri = new URI("hdfs://xiaoye11:8020");
        //创建对象
        Configuration configuration = new Configuration();
        String  user="root";
        //获取客户端对象
        FileSystem  fs= FileSystem.get(uri, configuration,user);
        Path src = new Path("F://xiaoye_Test");
        Path dest = new Path("/xiaoyeText/xiao111.txt");
        fs.copyToLocalFile(false,dest,src,true);
        //关闭资源
        fs.close();
        System.out.println("下载成功");
    }



    //4、删除hadoop上的文件
    @Test
    public void deleteFileTest()  throws URISyntaxException, IOException, InterruptedException{
        URI uri = new URI("hdfs://xiaoye11:8020");
        //创建对象
        Configuration configuration = new Configuration();
        String  user="root";

        //获取客户端对象
        FileSystem  fs= FileSystem.get(uri, configuration,user);

        Path dest = new Path("/xiaoyeText");

        fs.delete(dest,true);
        //关闭资源
        fs.close();
        System.out.println("删除成功");
    }


    //5、新建文件
    @Test
    public void createFileTest()  throws URISyntaxException, IOException, InterruptedException{
        URI uri = new URI("hdfs://xiaoye11:8020");
        //创建对象
        Configuration configuration = new Configuration();
        String  user="root";

        //获取客户端对象
        FileSystem  fs= FileSystem.get(uri, configuration,user);

        Path dest = new Path("/xiaoxiao.txt");

        fs.create(dest);
        //关闭资源
        fs.close();
        System.out.println("新建文件成功");
        System.out.println("********************hello,hadoop************************");
    }


    //6、判断是文件还是文件夹
    @Test
    public void judgeFileType()  throws URISyntaxException, IOException, InterruptedException{
        URI uri = new URI("hdfs://xiaoye11:8020");
        //创建对象
        Configuration configuration = new Configuration();
        String  user="root";

        //获取客户端对象
        FileSystem  fs= FileSystem.get(uri, configuration,user);

        Path dest = new Path("/");

        FileStatus[]   listStatus=fs.listStatus(dest);

        for (FileStatus  fileType:listStatus){

            if (fileType.isFile()) {
                System.out.println("文件:" + fileType.getPath().getName());
            }else {
                System.out.println("文件夹:" + fileType.getPath().getName());
            }
        }
        //关闭资源
        fs.close();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值