- day02 使用idea新建springboot项目并测试hdfs连接
- 一、新建项目
- 1. 使用spring initializr创建项目
- 2. 选择依赖
- 3. 引入hadoop依赖
- 二、创建仓库并首次提交
- 1. 启用版本控制
- 2. 选择git
- 3. 提交
- 三、编写hdfs测试代码
- 1. 创建HdfsTest.java
- 2. 查看hdfs文件列表
- 3. 测试通过IO流上传文件
- 4. 测试通过IO流下载文件
- 四、创建HDFS配置文件
- 1. 测试自动注入fs进行hdfs上传下载
- 1. 展示路径
- 2. 上传文件
在项目pom.xml中引入依赖
二、创建仓库并首次提交 1. 启用版本控制 2. 选择git 3. 提交 三、编写hdfs测试代码 1. 创建HdfsTest.javaorg.apache.hadoop hadoop-client 3.3.3 org.apache.hadoop hadoop-hdfs 3.3.3 org.apache.hadoop hadoop-common 3.3.3 org.apache.hadoop hadoop-mapreduce-client-core 3.3.3
package com.tgm.bigdata;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class HdfsTest {
}
2. 查看hdfs文件列表
@Test
public void test1() throws Exception {
// 创建配置信息对象
Configuration conf = new Configuration();
// 创建Hadoop的文件系统对象
// Hadoop可以使用多种文件系统:本地文件系统、HDFS、S3.。。。
// uri用来指定使用的文件系统的Schema file:///
URI uri = new URI("hdfs://10.10.10.100:9000");
FileSystem fs = FileSystem.get(uri, conf);
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
System.err.println(fileStatus);
}
}
- 注意添加导入的依赖类选择hadoop带来的
3. 测试通过IO流上传文件测试结果:成功显示目录情况
@Test
public void test2() throws Exception {
// 读取本地文件
InputStream in = new FileInputStream("G:\Desktop\work\bigdata\data\uploadtest.txt");
// 连接HDFS,获取文件系统对象
Configuration conf = new Configuration();
URI uri = new URI("hdfs://10.10.10.100:9000");
FileSystem fs = FileSystem.get(uri, conf);
// 创建对应的输出流
FSDataOutputStream out = fs.create(new Path("/test/upload.txt"));
IOUtils.copy(in, out); // 将输入流中的数据交给输出流即可实现文件的上传
}
4. 测试通过IO流下载文件测试结果:添加成功
@Test
public void test3() throws Exception {
// 输入流:HDFS
Configuration conf = new Configuration();
URI uri = new URI("hdfs://10.10.10.100:9000");
FileSystem fs = FileSystem.get(uri, conf);
// 获取输入流 :
FSDataInputStream in = fs.open(new Path("/test/upload.txt"));
OutputStream out = new FileOutputStream("abc.txt");
// 会被放到此项目的根目录下
// 将输入流中的数据拷贝给输出流
IOUtils.copy(in, out);
}
四、创建HDFS配置文件测试结果:文件下载到项目根目录下,内容一致
在bigdata下新建软件包conf,新建类HDFSConf
package com.tgm.bigdata.conf;
import org.apache.hadoop.fs.FileSystem;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
@Configuration
public class HDFSConf {
@Bean
public org.apache.hadoop.conf.Configuration getConf(){
org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
// 设置HDFS数据块副本数量为1
conf.set("dfs.replication", "1");
return conf;
}
@Bean
public FileSystem getFS(){
FileSystem fs = null;
try {
URI uri = new URI("hdfs://10.10.10.100:9000");
fs = FileSystem.get(uri, getConf(), "root");
} catch (URISyntaxException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return fs;
}
}
1. 测试自动注入fs进行hdfs上传下载
@Autowired
private FileSystem fs;
@Test
public void test4() throws IOException {
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
// fileStatus.getPath() 获取文件的路径名称
System.err.println(fileStatus.getPath());
}
}
// 文件上传:本地路径(源文件路径)---》HDFS路径(目标路径)
@Test
public void test5() throws IOException {
// 源文件路径
Path srcPath = new Path("D:/wc.txt");
// 目标文件路径
Path dstPath = new Path("/a200.txt");
fs.copyFromLocalFile(srcPath, dstPath);
}
1. 展示路径
2. 上传文件
发现:新的方式上传,因为加入了配置,用户名已经变为root,备份数量为1



