Unverified Commit 8af14c9d authored by silentxingtian's avatar silentxingtian Committed by GitHub

[Feature] [Resources] New resource center added, synchronization of HDFS information (#3137)

Co-authored-by: 's avatarliuw529 <liuw529@chinaunicom.cn> Co-authored-by: 's avatarZackYoung <zackyoungh@163.com>
parent cfcaf43a
...@@ -24,16 +24,20 @@ import org.dinky.data.model.ResourcesVO; ...@@ -24,16 +24,20 @@ import org.dinky.data.model.ResourcesVO;
import org.dinky.resource.BaseResourceManager; import org.dinky.resource.BaseResourceManager;
import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.Path;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
public class HdfsResourceManager implements BaseResourceManager { public class HdfsResourceManager implements BaseResourceManager {
FileSystem hdfs; FileSystem hdfs;
...@@ -87,7 +91,43 @@ public class HdfsResourceManager implements BaseResourceManager { ...@@ -87,7 +91,43 @@ public class HdfsResourceManager implements BaseResourceManager {
@Override @Override
public List<ResourcesVO> getFullDirectoryStructure(int rootId) { public List<ResourcesVO> getFullDirectoryStructure(int rootId) {
throw new RuntimeException("Sync HDFS Not implemented!"); String basePath = new Path(getBasePath()).toUri().getPath();
checkHdfsFile(basePath);
List<FileStatus> filePathsList = new ArrayList<>();
listAllHdfsFilePaths(basePath, filePathsList);
List<ResourcesVO> resList = new ArrayList<>();
for (FileStatus file : filePathsList) {
Path parentPath = file.getPath().getParent();
if (parentPath == null) {
continue;
}
int parentId = 1;
String parentSimplePath = parentPath.toUri().getPath();
// Determine whether it is the root directory
if (!parentSimplePath.equals(basePath)) {
String path = parentSimplePath.replace(basePath, "");
parentId = path.isEmpty() ? rootId : path.hashCode();
}
String self = StrUtil.replaceFirst(file.getPath().toUri().getPath(), basePath, "");
ResourcesVO resources = ResourcesVO.builder()
.id(self.hashCode())
.pid(parentId)
.fullName(self)
.fileName(file.getPath().getName())
.isDirectory(file.isDirectory())
.type(0)
.size(file.getBlockSize())
.build();
resList.add(resources);
}
return resList;
} }
@Override @Override
...@@ -109,4 +149,36 @@ public class HdfsResourceManager implements BaseResourceManager { ...@@ -109,4 +149,36 @@ public class HdfsResourceManager implements BaseResourceManager {
public synchronized void setHdfs(FileSystem hdfs) { public synchronized void setHdfs(FileSystem hdfs) {
this.hdfs = hdfs; this.hdfs = hdfs;
} }
public void checkHdfsFile(String path) {
try {
getHdfs().exists(new Path(path));
} catch (IOException e) {
throw BusException.valueOf("hdfs.dir.or.file.not.exist", e);
}
}
public FileStatus[] listHdfsFilePaths(String path) {
try {
return getHdfs().listStatus(new Path(path));
} catch (IOException e) {
throw BusException.valueOf("file.path.visit.failed", e);
}
}
public void listAllHdfsFilePaths(String path, List<FileStatus> fileStatusList) {
FileStatus[] paths = listHdfsFilePaths(path);
if (ArrayUtil.isEmpty(paths)) {
return;
}
for (FileStatus file : paths) {
fileStatusList.add(file);
if (file.isDirectory()) {
listAllHdfsFilePaths(file.getPath().toString(), fileStatusList);
}
}
}
} }
...@@ -38,6 +38,7 @@ import java.util.stream.Stream; ...@@ -38,6 +38,7 @@ import java.util.stream.Stream;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
...@@ -89,7 +90,8 @@ public class LocalResourceManager implements BaseResourceManager { ...@@ -89,7 +90,8 @@ public class LocalResourceManager implements BaseResourceManager {
@Override @Override
public List<ResourcesVO> getFullDirectoryStructure(int rootId) { public List<ResourcesVO> getFullDirectoryStructure(int rootId) {
String basePath = getBasePath(); String basePath = FileUtil.file(getBasePath()).getPath();
try (Stream<Path> paths = Files.walk(Paths.get(basePath))) { try (Stream<Path> paths = Files.walk(Paths.get(basePath))) {
return paths.map(path -> { return paths.map(path -> {
if (path.compareTo(Paths.get(basePath)) == 0) { if (path.compareTo(Paths.get(basePath)) == 0) {
...@@ -97,16 +99,18 @@ public class LocalResourceManager implements BaseResourceManager { ...@@ -97,16 +99,18 @@ public class LocalResourceManager implements BaseResourceManager {
return null; return null;
} }
Path parentPath = path.getParent(); Path parentPath = path.getParent();
String parent = ""; String self = StrUtil.replaceFirst(path.toString(), basePath, "");
if (parentPath != null) { int parentId = 1;
parent = parentPath.toString().replace(basePath, "");
// Determine whether it is the root directory
if (!parentPath.toUri().getPath().equals(basePath)) {
String parent = parentPath.toString().replace(basePath, "");
parentId = parent.isEmpty() ? rootId : parent.hashCode();
} }
String self = path.toString().replace(basePath, "");
int pid = parent.isEmpty() ? rootId : parent.hashCode();
File file = new File(path.toString()); File file = new File(path.toString());
return ResourcesVO.builder() return ResourcesVO.builder()
.id(self.hashCode()) .id(self.hashCode())
.pid(pid) .pid(parentId)
.fullName(self) .fullName(self)
.fileName(file.getName()) .fileName(file.getName())
.isDirectory(file.isDirectory()) .isDirectory(file.isDirectory())
......
...@@ -38,6 +38,7 @@ import com.amazonaws.services.s3.model.S3ObjectSummary; ...@@ -38,6 +38,7 @@ import com.amazonaws.services.s3.model.S3ObjectSummary;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
public class OssResourceManager implements BaseResourceManager { public class OssResourceManager implements BaseResourceManager {
OssTemplate ossTemplate; OssTemplate ossTemplate;
...@@ -87,6 +88,11 @@ public class OssResourceManager implements BaseResourceManager { ...@@ -87,6 +88,11 @@ public class OssResourceManager implements BaseResourceManager {
@Override @Override
public List<ResourcesVO> getFullDirectoryStructure(int rootId) { public List<ResourcesVO> getFullDirectoryStructure(int rootId) {
String basePath = getBasePath(); String basePath = getBasePath();
if (StrUtil.isNotBlank(basePath)) {
if (basePath.charAt(0) == '/') {
basePath = basePath.substring(1);
}
}
List<S3ObjectSummary> listBucketObjects = List<S3ObjectSummary> listBucketObjects =
getOssTemplate().listBucketObjects(getOssTemplate().getBucketName(), basePath); getOssTemplate().listBucketObjects(getOssTemplate().getBucketName(), basePath);
......
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
<properties> <properties>
<assertj.version>3.21.0</assertj.version> <assertj.version>3.21.0</assertj.version>
<aws-java-sdk-s3.version>1.12.589</aws-java-sdk-s3.version> <aws-java-sdk-s3.version>1.12.645</aws-java-sdk-s3.version>
<build-helper-maven-plugin.version>1.7</build-helper-maven-plugin.version> <build-helper-maven-plugin.version>1.7</build-helper-maven-plugin.version>
<clickhouse.version>0.2.6</clickhouse.version> <clickhouse.version>0.2.6</clickhouse.version>
<commons-email>1.5</commons-email> <commons-email>1.5</commons-email>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment