Search code examples
spring-bootspring-integration

How To Get a Specific Column in a CSV File


I'm using Spring Integration and was wondering how I can pull in a CSV file via an inbound file adapter and get a specific column from the file. The CSV file structure is shown below.

Header1, Header2, Header3
Value,   Value,   Value
...       ...      ...
...       ...      ...

What I need is to pull in all the values from the second column (Header2) and read each value.

Code below.

@Bean
@SuppressWarnings("unchecked")
public IntegrationFlow fileIntegrationTesting() {
    
    Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    

    
    
    return IntegrationFlows
            .from(Files.inboundAdapter(new File(inputFilePath))
                    .filter(getFileFilters())
                    .autoCreateDirectory(true) ,
                    c -> c.poller(Pollers.fixedRate(1000))
                    )
            .channel("fileInputChannel")
            .transform(Files.toStringTransformer())
            .split(s -> s.applySequence(true).delimiters(","))
            .aggregate(a -> a.releaseStrategy(g -> g.size() >= 10)
                            .expireGroupsUponCompletion(true)
                    )
            
            .handle((p, h) -> gson.toJson(new RequestPayload((Collection<String>) p)))
            .enrichHeaders(eh -> eh.async(false)
                    .header("accept", "application/json")
                    .header("contentType", "application/json")
                    )
            .handle(Http.outboundGateway("URL")
                        .httpMethod(HttpMethod.POST)
                        .expectedResponseType(String.class)
                        .extractPayload(true)
                    )
            .get();
            
}

private FileListFilter<File> getFileFilters(){
     ChainFileListFilter<File> cflf = new ChainFileListFilter<>();
     cflf.addFilter(new LastModifiedFileListFilter(30));
     cflf.addFilter(new AcceptOnceFileListFilter<>());
     cflf.addFilter(new SimplePatternFileListFilter(fileExtention));
     return cflf;
}

Solution

  • I think you should be using a FileSplitter, not a FileToStringTransformer which will load the whole file into a single String.

    Then, if you are discarding all the other columns, it would be most efficient to use .handle(myColumnSelector).

    With

    @Component
    public class MyColumnSelector {
    
        @ServiceActivator
        String getCol(String in) {
            return StringUtils.delimitedListToStringArray(in)[1];
        }
    
    }
    

    (Adding some protection for badly formed rows).