package com.cloud.demo.service.utils;
import com.cloud.demo.dto.BarDTO;
import com.cloud.demo.dto.PieDTO;
import com.cloud.demo.entity.HdfsPath;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
@Service
@Slf4j
public class HadoopUtils {
@Value("${hdfs.core-site}")
private String core;
@Value("${hdfs.hdfs-site}")
private String hdfs;
@Value("${localFile-home}")
private String localPath;
private Configuration configuration = new Configuration();
private FileSystem fileSystem;
public void init() throws IOException {
configuration.addResource(core);
configuration.addResource(hdfs);
fileSystem = FileSystem.get(configuration);
}
/**
* 上传文件
*
* @param path
* @param local
* @return
*/
public boolean upload(String path, String local) {
File file = new File(local);
try {
fileSystem.copyFromLocalFile(new Path(local), new Path(path));
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
log.info("【文件删除{}】,绝对路径:{}", file.delete() ? "成功" : "失败", local);
}
}
/**
* 获取文件类型
*
* @param name
* @return
*/
public String getFileType(String name) {
String type = "";
if (name.lastIndexOf(".") > 0) {
type = name.substring(name.lastIndexOf("."), name.length());
} else
return "other";
String out = "";
if (type.indexOf("doc") > 0 || type.indexOf("docx") > 0)
out = "word";
else if (type.indexOf("xls") > 0 || type.indexOf("xlsx") > 0)
out = "excel";
else if (type.indexOf("ppt") > 0 || type.indexOf("pptx") > 0)
out = "ppt";
else if (type.indexOf("txt") > 0 || type.indexOf("css") > 0 || type.indexOf("html") > 0 || type.indexOf("js") > 0)
out = "txt";
else if (type.indexOf("jpg") > 0 || type.indexOf("jpeg") > 0 || type.indexOf("png") > 0 || type.indexOf("bmp") > 0)
out = "pic";
else if (type.indexOf("zip") > 0 || type.indexOf("rar") > 0 || type.indexOf("jar") > 0)
out = "zip";
else if (type.indexOf("pdf") > 0)
out = "pdf";
else
out = "other";
return out;
}
/**
* 获取当前文件列表
*
* @param path
* @return
*/
public List<HdfsPath> getFilesList(String path) {
List<HdfsPath> list = new ArrayList<>();
path += "/";
System.out.println("PATH:" + path);
try {
FileStatus[] fileStatuses = fileSystem.listStatus(new Path(path));
for (int i = 0; i < fileStatuses.length; i++) {
Path p = fileStatuses[i].getPath();
HdfsPath hdfsPath = new HdfsPath();
hdfsPath.setName(p.getName());
hdfsPath.setPathName(path + p.getName());
hdfsPath.setSize(fileStatuses[i].getLen());
if (fileStatuses[i].isFile()) {
hdfsPath.setTypes(getFileType(p.getName()));
hdfsPath.setCreateTime(fileStatuses[i].getAccessTime());
} else {
hdfsPath.setTypes("dir");
hdfsPath.setCreateTime(fileStatuses[i].getModificationTime());
}
list.add(hdfsPath);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
* 获取路径列表
*
* @param path
* @return
*/
public List<HdfsPath> getPaths(String path) {
String[] split = path.split("/");
List<HdfsPath> hdfsPaths = new ArrayList<>();
List<String> paths = Arrays.stream(split).collect(Collectors.toList());
paths.forEach((v) -> {
if (!v.equals("")) {
String substring = path.substring(0, path.indexOf(v)) + v;
hdfsPaths.add(new HdfsPath(v, substring));
}
});
return hdfsPaths.subList(2, hdfsPaths.size());
}
/**
* 创建文件夹
*
* @param path
* @param name
* @return
*/
public boolean createDir(String path, String name) {
try {
fileSystem.mkdirs(new Path(path + "/" + name));
return true;
} catch (IOException e) {
return false;
}
}
/**
* 删除文件
*
* @param path
* @return
*/
public boolean delete(String path) {
try {
fileSystem.delete(new Path(path), true);
return true;
} catch (IOException e) {
return false;
}
}
/**
* 修改文件名
*
* @param oldName
* @param newName
* @return
*/
public boolean changeName(String oldName, String newName) {
try {
fileSystem.rename(new Path(oldName), new Path(newName));
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 从hdfs上复制文件到本地
*
* @param path
* @return
*/
public String copyFromHDFS(String path) {
String name = path.substring(path.lastIndexOf("/") + 1, path.length());
try {
fileSystem.copyToLocalFile(new Path(path), new Path(localPath + "/" + name));
return localPath + "/" + name;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 清空缓存
*/
public void clearTmp() {
String path = localPath;
File file = new File(path);
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
}
}
/**
* 获取所有文件信息
*
* @param path
* @return
*/
public List<HdfsPath> getFilesData(String path) {
List<HdfsPath> list = new ArrayList<>();
try {
FileStatus[] fileStatuses = fileSystem.listStatus(new Path(path));
for (int i = 0; i < fileStatuses.length; i++) {
Path p = fileStatuses[i].getPath();
HdfsPath hdfsPath = new HdfsPath();
hdfsPath.setName(p.getName());
hdfsPath.setPathName(path + p.getName());
hdfsPath.setSize(fileStatuses[i].getLen());
if (fileStatuses[i].isFile()) {
hdfsPath.setTypes(getFileType(p.getName()));
hdfsPath.setCreateTime(fileStatuses[i].getAccessTime());
list.add(hdfsPath);
} else {
List<HdfsPath> filesData = getFilesData(p.toString());
list.addAll(filesData);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
/**
* 获取饼状图
*
* @param filesData
* @return
*/
public List<PieDTO> getDataPie(List<HdfsPath> filesData) {
List<PieDTO> list = new ArrayList<>();
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于springboot+hadoop的个人私有云盘.zip (63个子文件)
private-cloud-master
mvnw.cmd 6KB
pom.xml 6KB
src
test
java
com
cloud
demo
DemoApplicationTests.java 213B
main
resources
core-site.xml 863B
templates
local.html 13KB
login.html 7KB
cloudStatus.html 13KB
error.html 792B
hdfs-site.xml 1KB
static
sql
user.sql 810B
js
bootstrap.js 68KB
bootstrap.min.js 36KB
local.js 8KB
FileSaver.js 6KB
echarts.min.js 392KB
jquery.min.js 94KB
img
zip.svg 1KB
dir.svg 1KB
pic.svg 1KB
txt.svg 3KB
ppt.svg 1KB
default.jpg 5KB
excel.svg 1KB
other.svg 2KB
word.svg 1003B
pdf.svg 3KB
css
bootstrap-theme.min.css.map 25KB
bootstrap.min.css 118KB
bootstrap-theme.css.map 47KB
local.css 8KB
bootstrap-theme.css 26KB
bootstrap.css.map 380KB
bootstrap.css 143KB
bootstrap-theme.min.css 23KB
bootstrap.min.css.map 529KB
fonts
glyphicons-halflings-regular.svg 106KB
glyphicons-halflings-regular.ttf 44KB
glyphicons-halflings-regular.woff 23KB
glyphicons-halflings-regular.eot 20KB
glyphicons-halflings-regular.woff2 18KB
application.yml 2KB
java
com
cloud
demo
DemoApplication.java 417B
mapper
UserMapper.java 284B
controller
CustomizeErrorController.java 2KB
IndexController.java 2KB
LocalController.java 7KB
CloudController.java 2KB
service
UserService.java 1KB
utils
HadoopUtils.java 9KB
entity
HdfsPath.java 466B
User.java 796B
exception
InterErrorCode.java 155B
ErrorCode.java 701B
dto
BarDTO.java 275B
ResultDTO.java 1KB
PieDTO.java 238B
interceptor
SessionInterceptor.java 1KB
WebConfig.java 688B
.mvn
wrapper
maven-wrapper.properties 218B
maven-wrapper.jar 50KB
MavenWrapperDownloader.java 5KB
mvnw 10KB
derby.log 0B
共 63 条
- 1
资源评论
博士僧小星
- 粉丝: 1922
- 资源: 5884
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功