java操作hdfs
1.添加maven依赖,即在pom.xml文件李添加依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>HdfsOperate</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>3.2.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>2.0.5</version> </dependency> </dependencies> </project>
2.添加log4j配置
Log4j.rootLogger=INFO,stdout Log4j.appender.stdout=org.apache.log4j.ConsoleAppender Log4j.appender.stdout.Layout=org.apache.log4j.PatternLayout Log4j.appender.stdout.Layout.ConversionPattern=%d %p [%C] - %m%n Log4j.appender.Logfile=org.apache.Log4j.FileAppender Log4j.appender.Logfile.File=target/log.log Log4j.appender.Logfile.Layout=org.apache.log4j.PatternLayout Log4j.appender.Logfile.Layout.ConversionPattern=%d %p [%c] -%m%n
3.编写测试案例
package com.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class HdfsOperateTest {
private FileSystem fs;
@Before
public void init() throws URISyntaxException, IOException, InterruptedException {
URI uri = new URI("hdfs://hadoop01:8020");
Configuration entries = new Configuration();
String user = "root";
fs = FileSystem.get(uri, entries, user);
}
@Test
public void testMkdir() throws IOException {
fs.mkdirs(new Path("/test"));
}
@After
public void clean() throws IOException {
fs.close();
}
}
4.运行代码,检查结果

5.项目结构


浙公网安备 33010602011771号