简体   繁体   English

通过调用 Web 服务 API 处理大型 CSV 字符串

[英]Processing a Large CSV String from calling Web service API

I call a rest service which returns an ID of an object in the first call.我调用了一个休息服务,它在第一次调用中返回一个对象的 ID。 It gives me the length in bytes in addition to the ID of the object to read.除了要读取的对象的 ID 之外,它还为我提供了以字节为单位的长度。

However, the CSV data returned could be up to 200,000 lines long.但是,返回的 CSV 数据最长可达 200,000 行。 This causes an out of memory exception due to reading all 200,000 thousand lines from a rest call into memory in a Java program.由于在 Java 程序中将其余调用中的所有 200,000 行读取到内存中,这会导致内存不足异常。

The Map returned is a CSV structure that the spring template returns.返回的Map是 spring 模板返回的 CSV 结构。 The problem is that when the equivalent CSV file is over 200,000 lines, the LinkedList can't hold it, so I get an out of memory exception.问题是当等效的CSV文件超过200,000行时, LinkedList无法容纳它,因此出现内存不足异常。

Is there any other way to manipulate the RestTemplate API to process the data as a Stream that can be read gradually?有没有其他方法可以操作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();

I was able to use a Streaming feature provided by Spring Rest API.我能够使用 Spring Rest API 提供的 Streaming 功能。 I used the SimpleClientHttpRequestFactory, BufferedReader and was able to set the chunk size to read at a time.我使用了 SimpleClientHttpRequestFactory、BufferedReader 并且能够设置一次读取的块大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM