Hadoop学习——hdfs客户端操作及hdfs的API 操作

首先要在ubuntu上运行hadoop集群环境
1.在hfds传建目录

    /**
     * 在hfds传建目录
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public  void testMkdirs() throws URISyntaxException, IOException, InterruptedException {
        Configuration conf = new Configuration();
        // 1 获取hdfs客户端对象
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf, "hadoop");

        // 2 在hdfs上创建路径
        fs.mkdirs(new Path("/0529/dashen/banzhang"));

        // 3 关闭资源
        fs.close();

        System.out.println("over");
    }

从本地将文件上传到hdfs

 /**
     * 1 文件上传
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void testCopyFromLocalFile() throws URISyntaxException, IOException, InterruptedException {
        // 1 获取hdfs客户端对象
        Configuration conf = new Configuration();
        conf.set("dfs.replication", "2");// 权限
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf, "hadoop");

        // 2 执行上传API
        fs.copyFromLocalFile(new Path("/home/hadoop/MyTmp/test.txt"),new Path("/CopyFromLocalFile2.txt"));

        // 3 关闭资源
        fs.close();

        System.out.println("over");
    }

2 从hdfs将文件下载到本地

/**
     * 2 文件下载
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void testCopyToLocal() throws URISyntaxException, IOException, InterruptedException {
        // 1 获取hdfs客户端对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf, "hadoop");

        // 2 执行下载操作
//        fs.copyToLocalFile(new Path(), new Path());
        fs.copyToLocalFile(false,new Path("/CopyFromLocalFile.txt"),new Path("/home/hadoop/MyTmp/CopyToLocal.txt"),true);

        //3.close()
        fs.close();

        System.out.println("voer");
    }

将hdfs 的文件进行删除

 /**
     * 3 文件删除
     * @throws IOException
     * @throws URISyntaxException
     * @throws InterruptedException
     */
    @Test
    public void testDelete() throws IOException, URISyntaxException, InterruptedException {
        // 1 获取hdfs客户端对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf, "hadoop");

        // 2. delete file
        fs.delete(new Path("/0529"),true);

        //3.close()
        fs.close();

        System.out.println("over");
    }

对hdfs 内的文件进行更名

/**
     * 4 文件更名
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void testRename() throws URISyntaxException, IOException, InterruptedException {
        //1. 获取hdfs客户端对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf, "hadoop");

        //2 rename
        fs.rename(new Path("/sanguo"),new Path("/三国 "));

        //3. close()
        fs.close();

        System.out.println("over");
    }

文件详情查看

 /**
     * 5 文件详情查看
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void testListFiles() throws URISyntaxException, IOException, InterruptedException {
        //1. 获取hdfs客户端对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf, "hadoop");

        // 2 查看文件详情
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);

        while(listFiles.hasNext()){
            LocatedFileStatus fileStatus = listFiles.next();

            // 查看文件名称、权限、长度、块信息
            System.out.println(fileStatus.getPath().getName());// 文件名称
            System.out.println(fileStatus.getPermission());// 文件权限
            System.out.println(fileStatus.getLen());// 文件长度

            BlockLocation[] blockLocations = fileStatus.getBlockLocations();

            for (BlockLocation blockLocation : blockLocations) {

                String[] hosts = blockLocation.getHosts();

                for (String host : hosts) {
                    System.out.println(host);
                }
            }

            System.out.println("------分割线--------");
        }

        //3 close()
        fs.close();
    }

判断是文件还是文件夹

/**
     * 6 判断是文件还是文件夹
     * @throws URISyntaxException
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void testListStatus() throws URISyntaxException, IOException, InterruptedException {
        //1. 获取hdfs客户端对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf, "hadoop");

        // 2 判断操作
        FileStatus[] listStatus = fs.listStatus(new Path("/"));

        for (FileStatus fileStatus : listStatus) {

            if (fileStatus.isFile()) {
                // 文件
                System.out.println("f:"+fileStatus.getPath().getName());
            }else{
                // 文件夹
                System.out.println("d:"+fileStatus.getPath().getName());
            }
        }
        // 3 关闭资源
        fs.close();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值