[英]Processing a Large CSV String from calling Web service API
我調用了一個休息服務,它在第一次調用中返回一個對象的 ID。 除了要讀取的對象的 ID 之外,它還為我提供了以字節為單位的長度。
但是,返回的 CSV 數據最長可達 200,000 行。 由於在 Java 程序中將其余調用中的所有 200,000 行讀取到內存中,這會導致內存不足異常。
返回的Map
是 spring 模板返回的 CSV 結構。 問題是當等效的CSV文件超過200,000行時, LinkedList
無法容納它,因此出現內存不足異常。
有沒有其他方法可以操作RestTemplate
API 將數據處理為可以逐漸讀取的Stream
?
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
builder.scheme("https).pathSegment("/file-object").host(hostName);
// Call the third-party Rest API
ResponseEntity<?> responseEntity =
restTemplate.exchange(
builder.queryParam("fileId", "fjr666eH").build().toUri(),
HttpMethod.GET,
new HttpEntity(httpAuthorizationHeaders("UID","XFCode:66w000")),
Object.class
);
// CSV file is returned by Spring as a linked list. But since data is
// large I get an OOM exception
LinkedHashMap<String, Object> map =
(LinkedHashMap<String, Object>) responseEntity.getBody();
我能夠使用 Spring Rest API 提供的 Streaming 功能。 我使用了 SimpleClientHttpRequestFactory、BufferedReader 並且能夠設置一次讀取的塊大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.