feat(hudi-query): 新增写hdfs文件接口

This commit is contained in:
v-zhangjc9
2024-05-08 10:02:33 +08:00
parent 5c9089419f
commit b2e3a58d27
8 changed files with 122 additions and 92 deletions

View File

@@ -8,6 +8,8 @@ import org.eclipse.collections.api.list.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@@ -48,27 +50,36 @@ public class HdfsController {
}
@GetMapping("get")
public HPath get(@RequestParam("root")String root) throws IOException {
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 {
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 {
public String read(@RequestParam("root") String root) throws IOException {
return hdfsService.read(root);
}
@PostMapping("write")
public void write(
@RequestParam("root") String root,
@RequestParam(value = "overwrite", defaultValue = "false") Boolean overwrite,
@RequestBody String text
) throws IOException {
hdfsService.write(root, text, overwrite);
}
@GetMapping("download")
public void download(@RequestParam("root")String root, HttpServletResponse response) throws IOException {
public void download(@RequestParam("root") String root, HttpServletResponse response) throws IOException {
hdfsService.download(root, response.getOutputStream());
}
@GetMapping("size")
public Long size(@RequestParam("root")String root) throws IOException {
public Long size(@RequestParam("root") String root) throws IOException {
return hdfsService.size(root);
}
}

View File

@@ -9,6 +9,7 @@ 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.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
@@ -25,6 +26,7 @@ import org.springframework.stereotype.Service;
* @author lanyuanxiaoyao
* @date 2024-01-24
*/
@SuppressWarnings("SpringCacheableMethodCallsInspection")
@Service
public class HdfsService {
private static final Logger logger = LoggerFactory.getLogger(HdfsService.class);
@@ -132,6 +134,14 @@ public class HdfsService {
}
}
public void write(String root, String text, Boolean overwrite) throws IOException {
try (FileSystem fileSystem = FileSystem.get(new Configuration())) {
try (FSDataOutputStream stream = fileSystem.create(new Path(root), overwrite)) {
stream.writeBytes(text);
}
}
}
@SuppressWarnings("SpringCacheableMethodCallsInspection")
public void download(String root, OutputStream outputStream) throws IOException {
if (!existsPath(root)) {