您的位置 首页 > 数码极客

deletefile,deletefile 错误码5拒绝访问?

1、背景

在上一节中,我们简单学习了在命令行上如何操作Hdfs shell api,此处我们通过java程序来操作一下。

2、环境准备

  1. 需要在本地环境变量中 配置 HADOOP_HOME 或在程序启动的时候通过命令行指定的值,值为HADOOP的home目录地址。可通过org.a方法验证。
  2. 我们的HADOOP最好是自己在本地系统进行重新编译,不然可能运行部分java api会出现问题。

3、环境搭建

3.1 引入jar包

<dependencyManagement> <dependencies> <dependency> <groupId>org.junit</groupId> <artifactId>junit-bom</artifactId> <version>5.7.1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.a;/groupId> <artifactId>hadoop-client</artifactId> <version>3.3.4</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.a;/groupId> <artifactId>log4j-api</artifactId> <version>2.14.1</version> </dependency> <dependency> <groupId>org.a;/groupId> <artifactId>log4j-core</artifactId> <version>2.14.1</version> </dependency> </dependencies>

3.2 引入log4j.properties配置文件

log4j.a = org.a log4j.a.Target = Sy log4j.a.layout = org.a log4j.a.layout.ConversionPattern = [%-5p] %d{ HH:mm:ss,SSS} [%t]:%m%n log4j.rootLogger = debug,console

引入这个配置是为了,当hadoop报错时,更好的排查问题

3.3 初始化Hadoop Api

@TestInstance) class hdfsApiTest { private FileSystem fileSystem; private static final Logger log = LoggerFac); @BeforeAll public void setUp() throws IOException, InterruptedException { // 1、将 HADOOP_HOME 设置到环境变量中 Configuration configuration = new Configuration(); // 2、此处的地址是 NameNode 的地址 URI uri = URI.create("hdfs://192.168.121.140:8020"); // 3、设置用户 String user = "hadoopdeploy"; // 此处如果不设置第三个参数,指的是客户端的身份,默认获取的是当前用户,不过当前用户不一定有权限,需要指定一个有权限的用户 FileSystem = FileSy(uri, configuration, user); } @AfterAll public void tearDown() throws IOException { if (null != fileSystem) { (); } } }

此处我们需要注意的是,需要设置客户端操作的 用户,默认情况下获取的是当前登录用户,否则很有可能会出现如下错误

解决办法:
1、修改目录的访问权限。
2、修改客户端的用户,比如此处修改成hadoopdeploy

4、java api操作

4.1 创建目录

@Test @DisplayName("创建hdfs目录") public void testMkdir() throws IOException { Path path = new Path("/bigdata/hadoop/hdfs"); if (path)) { log.info("目录 /bigdata/hadoop/hdfs 已经存在,不在创建"); return; } boolean success = (path); log.info("创建目录 /bigdata/hadoop/hdfs 成功:[{}?]", success); }

4.2 上传文件

@Test @DisplayName("上传文件") void uploadFile() throws IOException { /** * delSrc: 文件上传后,是否删除源文件 true:删除 false:不删除 * overwrite: 如果目标文件存在是否重写 true:重写 false:不重写 * 第三个参数:需要上传的文件 * 第四个参数:目标文件 */ (false, true, new Path("/Users/huan/code/IdeaProjects/me/spring-cloud-parent/hadoop/hdfs-api/src/test/java/com/huan/hadoo;), new Path("/bigdata/hadoop/hdfs")); }

4.3 列出目录下有哪些文件

@Test @DisplayName("列出目录下有哪些文件") void testListFile() throws IOException { Remoteiterator<LocatedFileStatus> iterator = (new Path("/bigdata"), true); while ()) { LocatedFileStatus locatedFileStatus = i(); Path path = loca(); if ()) { log.info("获取到文件: {}", ()); } } }

4.4 下载文件

@Test @DisplayName("下载文件") void testDownloadFile() throws IOException { (false, new Path("/bigdata/hadoop/hd;), new Path("/Users/huan;), true); }

4.5 删除文件

@Test @DisplayName("删除文件") public void testDeleteFile() throws IOException { (new Path("/bigdata/hadoop/hd;), false); }

4.6 检测文件是否存在

@Test @DisplayName("检查文件是否存在") public void testFileExists() throws IOException { Path path = new Path("/bigdata/hadoop/hd;); boolean exists = (path); log.info("/bigdata/hadoop/hd 存在:[{}]", exists); }

5、完整代码

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“deletefile,deletefile,错误码5拒绝访问,deletefile,错误码5怎样解决,deletefile,错误码5”边界阅读