【发布时间】:2021-07-10 14:11:15
【问题描述】:
我需要处理来自 Rest 网络服务的数据。以下基本示例是:
import org.springframework.batch.item.ItemReader;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
import java.util.List;
class RESTDataReader implements ItemReader<DataDTO> {
private final String apiUrl;
private final RestTemplate restTemplate;
private int nextDataIndex;
private List<DataDTO> data;
RESTDataReader(String apiUrl, RestTemplate restTemplate) {
this.apiUrl = apiUrl;
this.restTemplate = restTemplate;
nextDataIndex = 0;
}
@Override
public DataDTO read() throws Exception {
if (dataIsNotInitialized()) {
data = fetchDataFromAPI();
}
DataDTO nextData = null;
if (nextDataIndex < data.size()) {
nextData = data.get(nextDataIndex);
nextDataIndex++;
}
else {
nextDataIndex= 0;
data = null;
}
return nextData;
}
private boolean dataIsNotInitialized() {
return this.data == null;
}
private List<DataDTO> fetchDataFromAPI() {
ResponseEntity<DataDTO[]> response = restTemplate.getForEntity(apiUrl,
DataDTO[].class
);
DataDTO[] data= response.getBody();
return Arrays.asList(data);
}
}
但是,我的 fetchDataFromAPI 方法是使用时隙调用的,它可以获得超过 2000 万个对象。
例如:如果我在 01012020 和 01012021 之间调用它,我将获得 8000 万个数据。
PS:网络服务通过一天的分页工作,即如果我想检索 01/09/2020 和 07/09/2020 之间的数据,我必须调用它几次(01/09-02 之间) /09 然后在 02/09-03/09 之间,依此类推,直到 06/09-07/09)
如果数据量很大,我的问题是堆空间内存。
我必须为每个月创建一个步骤以避免在我的 BatchConfiguration 中出现此问题(12 个步骤)。第一步将在 01/01/2020 和 01/02/2020 等之间调用 Web 服务
有没有一种解决方案可以在进入处理器之前仅一步读取所有这些数据量??
提前致谢
【问题讨论】:
-
the web service works by pagination of a single day: 这个webservice是否提供当天分页,即小于一天的粒度。例如,对于一天,是否可以一次按 100 个项目或 1000 个项目的页面查询数据? -
不,只是按天计算,我必须在参数中精确设置第一天和第二天
标签: spring-boot spring-batch jobs spring-batch-job-monitoring