feat(web): 增加HDFS文件管理器

在页面直接查看HDFS文件和目录,还可以查看和下载
This commit is contained in:
v-zhangjc9
2024-04-28 16:28:48 +08:00
parent 9913943a27
commit 572a1dab9f
11 changed files with 581 additions and 34 deletions

View File

@@ -1,6 +1,10 @@
package com.lanyuanxiaoyao.service.hudi.controller;
import com.lanyuanxiaoyao.service.configuration.entity.hudi.HPath;
import com.lanyuanxiaoyao.service.hudi.service.HdfsService;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.collections.api.list.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
@@ -42,4 +46,24 @@ public class HdfsController {
public Boolean existsPath(@RequestParam("hdfs") String hdfs) {
return hdfsService.existsPath(hdfs);
}
@GetMapping("get")
public HPath get(@RequestParam("root")String root) throws IOException {
return hdfsService.get(root);
}
@GetMapping("list")
public ImmutableList<HPath> list(@RequestParam("root")String root) throws IOException {
return hdfsService.list(root);
}
@GetMapping("read")
public String read(@RequestParam("root")String root) throws IOException {
return hdfsService.read(root);
}
@GetMapping("download")
public void download(@RequestParam("root")String root, HttpServletResponse response) throws IOException {
hdfsService.download(root, response.getOutputStream());
}
}

View File

@@ -1,13 +1,21 @@
package com.lanyuanxiaoyao.service.hudi.service;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import com.lanyuanxiaoyao.service.common.entity.TableMeta;
import com.lanyuanxiaoyao.service.configuration.entity.hudi.HPath;
import com.lanyuanxiaoyao.service.forest.service.InfoService;
import java.io.IOException;
import java.io.OutputStream;
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.apache.hudi.common.table.HoodieTableMetaClient;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
@@ -58,10 +66,81 @@ public class HdfsService {
@Cacheable(value = "exists-path", sync = true)
public Boolean existsPath(String hdfs) {
try(FileSystem fileSystem = FileSystem.get(new Configuration())) {
try (FileSystem fileSystem = FileSystem.get(new Configuration())) {
return fileSystem.exists(new Path(hdfs));
} catch (IOException ignored) {
}
return false;
}
@Cacheable(value = "get-hpath", sync = true)
public HPath get(String root) throws IOException {
if (!existsPath(root)) {
throw new RuntimeException("File not found");
}
try (FileSystem fileSystem = FileSystem.get(new Configuration())) {
FileStatus status = fileSystem.getFileStatus(new Path(root));
return new HPath(
status.getPath().getName(),
status.getPath().toString(),
status.isFile(),
status.isDirectory(),
status.getLen(),
status.getGroup(),
status.getOwner(),
status.getPermission().toString(),
(int) status.getReplication(),
status.getModificationTime()
);
}
}
@Cacheable(value = "list-hpath", sync = true)
public ImmutableList<HPath> list(String root) throws IOException {
if (!existsPath(root)) {
return Lists.immutable.empty();
}
try (FileSystem fileSystem = FileSystem.get(new Configuration())) {
MutableList<HPath> files = Lists.mutable.empty();
for (FileStatus status : fileSystem.listStatus(new Path(root))) {
files.add(new HPath(
status.getPath().getName(),
status.getPath().toString(),
status.isFile(),
status.isDirectory(),
status.getLen(),
status.getGroup(),
status.getOwner(),
status.getPermission().toString(),
(int) status.getReplication(),
status.getModificationTime()
));
}
return files.toImmutable();
}
}
@Cacheable(value = "read-hpath", sync = true)
public String read(String root) throws IOException {
if (!existsPath(root)) {
return "";
}
try (FileSystem fileSystem = FileSystem.get(new Configuration())) {
try (FSDataInputStream stream = fileSystem.open(new Path(root))) {
return IoUtil.readUtf8(stream);
}
}
}
@SuppressWarnings("SpringCacheableMethodCallsInspection")
public void download(String root, OutputStream outputStream) throws IOException {
if (!existsPath(root)) {
return;
}
try (FileSystem fileSystem = FileSystem.get(new Configuration())) {
try (FSDataInputStream stream = fileSystem.open(new Path(root))) {
IoUtil.copy(stream, outputStream);
}
}
}
}