前置条件:windows上成功搭建Hadoop环境
一、首先,创建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>com.hadoop</groupId>
<artifactId>mapreduce-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.7.7</version>
</dependency>
</dependencies>
</project>
HdfsTest.java文件如下:
package com.hadoop.hdfs;
import com.hadoop.utils.Tools;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
public class HdfsTest {
public static void main(String[] args) {
FileSystem fileSystem = null;
try {
fileSystem = FileSystem.get(Tools.configuration);
// 创建新目录
fileSystem.mkdirs(new Path("/hadoop"));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fileSystem.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Tools.java文件如下:
package com.hadoop.utils;
import org.apache.hadoop.conf.Configuration;
public class Tools {
public static final Configuration configuration = new Configuration();
static {
// 与服务器的地址有关
configuration.set("fs.defaultFS", "hdfs://localhost:9000");
}
}
log4j.properties文件是从hadoop根目录/etc/hadoop 目录下拷贝过来的。
运行项目,通过localhost:50070查看,相关目录已成功创建,如下图:
参考博客:https://blog.csdn.net/chenzhongwei99/article/details/72473021