数据统计

This commit is contained in:
2025-10-17 16:44:46 +08:00
parent 5bb77c2a51
commit 7f84de7d2b
9 changed files with 191 additions and 49 deletions

View File

@@ -0,0 +1,27 @@
package com.sdm.data.controller;
import com.sdm.data.service.IModelService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 模型训练控制器
*/
@RestController
@RequestMapping("/modelTraning")
public class ModelTraningController {
@Autowired
private IModelService modelService;
/**
* 调用python脚本处理导入数据
*/
@GetMapping("/handleLoadData")
@Operation(summary = "调用python脚本处理导入数据", description = "调用python脚本处理导入数据")
public void handleLoadData(Integer fileId, String localFilePath, String pythonScriptPath, String paramJsonPath) {
modelService.handleLoadData(fileId, localFilePath, pythonScriptPath,paramJsonPath );
}
}

View File

@@ -0,0 +1,11 @@
package com.sdm.data.service;
/**
* 模型训练服务接口
*/
public interface IModelService {
/**
* 调用python脚本处理导入数据
*/
void handleLoadData(Integer fileId, String localFilePath, String pythonScriptPath, String paramJsonPath);
}

View File

@@ -0,0 +1,81 @@
package com.sdm.data.service.impl;
import com.sdm.data.service.IDataFileService;
import com.sdm.data.service.IModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.*;
import java.io.FileOutputStream;
import java.io.OutputStream;
@Service
@Slf4j
public class ModelServiceImpl implements IModelService {
@Autowired
IDataFileService dataFileService;
@Override
public void handleLoadData(Integer fileId, String localFilePath, String pythonScriptPath,String paramJsonPath) {
try {
// 从MinIO下载文件到本地路径
downloadFromMinIO(fileId,localFilePath);
callPythonScript(pythonScriptPath, paramJsonPath);
} catch (Exception e) {
log.error("调用python脚本处理导入数据", e);
throw new RuntimeException(e);
}
}
private void downloadFromMinIO(Integer fileId, String localFilePath) {
try {
InputStream in = dataFileService.getMinioInputStream(fileId);
OutputStream out = new FileOutputStream(localFilePath);
// 将输入流写入本地文件
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} catch (IOException e) {
log.error("下载文件到本地失败", e);
throw new RuntimeException(e);
}
}
public void callPythonScript(String pythonScriptPath, String paramJsonPath) throws IOException, InterruptedException {
try {
// 构建执行 Python 脚本的命令
ProcessBuilder processBuilder = new ProcessBuilder(
"python3",
pythonScriptPath,
paramJsonPath
);
// 重定向进程的输入、输出流,便于查看执行日志
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
// 读取 Python 脚本的输出
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
// 等待 Python 脚本执行完成,并获取退出码
int exitCode = process.waitFor();
if (exitCode == 0) {
System.out.println("Python 脚本执行成功");
} else {
System.out.println("Python 脚本执行失败,退出码: " + exitCode);
}
} catch (Exception e) {
log.error("调用Python脚本失败", e);
throw new RuntimeException(e);
}
}
}