HDFS-windows下测试hdfs命令

public class HdfsClient1 {
    private FileSystem fs;
    //初始化一个FileSystem
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        //1:获取文件系统
        URI uri = new URI("hdfs://myhadoop105:9820");
        Configuration conf = new Configuration();
        conf.set("dfs.replication","6");
        String user = "atguigu";
        fs = FileSystem.get(uri, conf, user);

    }
    @After
    public void closeResources() throws IOException {
        //3:关闭资源
        fs.close();
    }
    @Test
    public void testMkdir() throws IOException {
        fs.mkdirs(new Path("/hdfs/windowsOperator/operator01"));
    }
    @Test
    public void testPut() throws IOException {
        fs.copyFromLocalFile(false , false , new Path("G:/code/test/day08课堂记录.txt") , new Path("/hdfs/windowsOperator/operator01"));
    }
    @Test
    public void testGet() throws IOException {
        fs.copyToLocalFile(new Path("/hdfs/windowsOperator/operator01"),new Path("G:\\code\\test\\test"));
    }
    @Test
    public void testMove() throws IOException {
        fs.rename(new Path("/hdfs/windowsOperator/day08课堂记录.txt"),new Path("/hdfs/windowsOperator/day08"));
    }
    @Test
    public void testDelete() throws IOException {
        fs.delete(new Path("/hdfs/windowsOperator/operator01"),true);
    }

    @Test
    public void testIsFile() throws IOException {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses) {
            //判断是否是文件
            if (fileStatus.isFile()){
                System.out.println("-:"+fileStatus.getPath().getName());

            }else {
                System.out.println("d:" + fileStatus.getPath().getName());
            }

        }


    }

    @Test
    public void testListFiles() throws IOException {
        // f:要查看的目录路径
        // r:是否递归查看
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
        while (listFiles.hasNext()){
            LocatedFileStatus fileStatus = listFiles.next();
            System.out.println("-------------------------" + fileStatus.getPath() + "--------------------");
            System.out.println(fileStatus.getPermission());
            System.out.println(fileStatus.getOwner());
            System.out.println(fileStatus.getGroup());
            System.out.println(fileStatus.getLen());
            System.out.println(fileStatus.getModificationTime());
            System.out.println(fileStatus.getReplication());
            System.out.println(fileStatus.getBlockLocations());
            System.out.println(fileStatus.getPath().getName());

            //快信息
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            System.out.println(Arrays.toString(blockLocations));

        }


    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值