S++

千线一眼

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

hadoop入门(9):hdfs的java编程-创建目录

前言

api文档
为了方便,我先在test下新建

文件编写

创建文件

编写文件

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

import java.io.IOException;

public class HdfsOperate {
    /**
     * 创建目录
     */
    @Test
    public void mkDirOnHdfs() throws IOException {
        // configuration
        Configuration configuration = new Configuration();
        // 设置namenode
        configuration.set("fs.defaultFS","hdfs://node001:8020");
        // filesystem
        FileSystem fileSystem = FileSystem.get(configuration);
        // 通过filesystem对象创建目录
        boolean ans = fileSystem.mkdirs(new Path("/sjj/test"));
        System.out.println("创建结果:"+ans);
        // 释放资源
        fileSystem.close();
    }
}
或者这样写
    @Test
    public void mkDirOnHdfs_2() throws IOException, URISyntaxException, InterruptedException {
        // configuration
        Configuration configuration = new Configuration();
        // filesystem
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://node001:8020"), configuration, "sjj");
        // 通过filesystem对象创建目录
        boolean ans = fileSystem.mkdirs(new Path("/sjj/test_2"));
        System.out.println("创建结果:"+ans);
        // 释放资源
        fileSystem.close();
    }

运行文件

运行文件之前确保你的hadoop集群是打开的
查看

创建目录时指定权限

    // 创建目录时指定权限
    @Test
    public void mkDirOnHdfs_3() throws IOException, URISyntaxException, InterruptedException {
        // configuration
        Configuration configuration = new Configuration();
        // filesystem对象、设置namenode
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://node001:8020"), configuration, "sjj");
        // 设置权限(当前用户拥有读写权限,当前用户组其它用户拥有读权限,其它用户组用户拥有读权限)
        FsPermission fsPermission = new FsPermission(FsAction.READ_WRITE, FsAction.READ, FsAction.READ);
        // 通过filesystem对象创建目录
        boolean ans = fileSystem.mkdirs(new Path("/sjj/test_3"),fsPermission);
        System.out.println("创建结果:"+ans);
        // 释放资源
        fileSystem.close();
    }

posted on   S++  阅读(697)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示