1、创建Maven项目
- 创建Maven项目 - HDFSDemo
单击【Finish】按钮
2、添加相关依赖
- 在
pom.xml
文件里添加hadoop
和junit
依赖
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
3、创建日志属性文件
- 在
resources
目录里创建log4j.properties
文件
log4j.rootLogger=stdout, logfile
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/hdfs.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
4、启动集群HDFS服务
- 在主节点上执行命令:
start-dfs.sh
5、在HDFS上创建文件
- 创建
net.hw.hdfs
包,在包里创建CreateFileOnHDFS
类
- 在HDFS上有
/ied01
目录,在该目录里创建hadoop.txt
文件
package net.hw.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.net.URI;
/**
* 功能:在HDFS上创建文件
* 作者:华卫
* 日期:2022年10月08日
*/
public class CreateFileOnHDFS {
public static void main(String[] args) throws Exception {
// 创建配置对象
Configuration conf = new Configuration();
// 定义uri字符串
String uri = "hdfs://master:9000";
// 创建文件系统对象
FileSystem fs = FileSystem.get(new URI(uri), conf);
// 创建路径对象
Path path = new Path(uri + "/ied01/hadoop.txt");
// 创建文件
boolean result = fs.createNewFile(path);
// 判断是否创建成功
if (result) {
System.out.println("文件[" + path + "]创建成功!");
} else {
System.out.println("文件[" + path + "]创建失败!");
}
}
}
运行程序,查看结果
再次运行程序,由于hadoop.txt
已经存在,此时会提示用户创建失败
6、写入HDFS文件
- 在
net.hw.hdfs
包里创建WriteFileOnHDFS
类
(1)将数据直接写入HDFS文件
- 创建
write1()方法
package net.hw.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;
import java.net.URI;
/**
* 功能:写入HDFS文件
* 作者:华卫
* 日期:2022年10月08日
*/
public class WriteFileOnHDFS {
@Test
public void write1() throws Exception {
// 创建配置对象
Configuration conf = new Configuration();
// 定义uri字符串
String uri = "hdfs://master:9000";
// 创建文件系统对象
FileSystem fs = FileSystem.get(new URI(uri), conf);
// 创建路径对象(文件或目录)
Path path = new Path(uri + "/ied01/hello.txt");
// 创建文件输出流
FSDataOutputStream out = fs.create(path);
// 写数据
out.write("hello hadoop world".getBytes());
// 提示用户写文件成功
System.out.println("文件[" + path + "]写入成功!");
// 刷新输出流
out.flush();
// 关闭输出流
out.flush();
// 关闭文件
fs.close();
}
}
运行write1()
测试方法,查看结果
利用HDFS集群WebUI查看hello.txt
文件
(2)将本地文件写入HDFS文件
-
在项目根目录创建一个文本文件
test.txt
创建write2()
方法
@Test
public void write2() throws Exception {
Configuration conf = new Configuration();
String uri = "hdfs://master:9000";
FileSystem fs = FileSystem.get(new URI(uri), conf);
Path path = new Path(uri + "/ied01/test.txt");
FSDataOutputStream out = fs.create(path);
FileInputStream in = new FileInputStream("test.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String nextLine = "";
while ((nextLine = br.readLine()) != null) {