基于hadoop HDFS的存储系统(web 网盘)
1. HDFS的优势
1.1 源码注释说很透彻:
Hadoop DFS is a multi-machine system that appears as a single
disk. It's useful because of its fault tolerance and potentially
very large capacity.
解释:HDFS是一个多机系统,对外作为一个整体的磁盘存在。由于它的容错性和大容量,具有很大的可用性
1.2 对外作为一个整体 和 容错性 的原理
1)整体性
NameNode保存元数据,作为整个系统打大管家
2)容错性
数据数据多机备份、数据校验、心跳检测、数据块报告、读写容错
2. 代码实现
这里代码是基于maven、springboot书写。当然方式多种多样,都可以考虑。
2.1 依赖重要的包
hadoop-common包,这是一个对hadoop其余模块的公告支持库。
包含conf、FS、IO和IPC等部分
hadoop-common源码地址如下:
https://github.com/apache/hadoop/tree/trunk/hadoop-common-project
2.1 上传功能
1)接收用户的上传请求
package com.hdlw.controls;
@Controller
public class UploadMap {
...
@RequestMapping(value = "/upload")
@ResponseBody
public ReturnMessage upload(@RequestParam("file") MultipartFile file, HttpServletRequest httpServletRequest) {
...
int result = UploadDownload.upload(file.getInputStream(), path);
...
}
}
2)处理上传存储请求
public class UploadDownload {
...
public static int upload(InputStream inputStream, String path) {
int result = -1;
//借助FileSystem 来联系/操作HDFS系统
//这里针对每个请求实例化一个局部变量来处理
FileSystem fileSystem = checkAndInit();
if (fileSystem == null) {
return result;
}
OutputStream outputStream = null;
try {
outputStream = fileSystem.create(new Path(path), true);
result = IOUtils.copy(inputStream, outputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(inputStream,outputStream);
}
return result;
}
...
}
2.2 下载功能
1)接收用户的下载请求
@GetMapping("/download")
public String downloadFile(HttpServletRequest request, HttpServletResponse response, @RequestParam String filenameName) {
.....
try {
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName=" + filenameName);
int result = UploadDownload.download(response.getOutputStream(), path);
System.out.println(result);
if (result != -1) {
returnMessage.setMsg(constants.operate_ok);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
1)下载请求处理
public static int download(OutputStream outputStream, String path){
int result = -1;
FileSystem fileSystem = checkAndInit();
if (fileSystem == null) {
return result;
}
InputStream inputStream = null;
Path filePath = new Path(path);
try {
if (!fileSystem.exists(filePath)) {
return result;
}
inputStream = fileSystem.open(new Path(path));
result = IOUtils.copy(inputStream, outputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
close(inputStream,outputStream);
}
return result;
}
结语
要实现复杂的功能还是有不少工作和细节需要去做:
权限管理、用户管理、上传、查询、下载、、删除、目录(文件夹)操作、hdfs系统管理