gpt4 book ai didi

java - 使用spring集成dsl逐行读取文件

转载 作者:行者123 更新时间:2023-11-30 05:30:45 27 4
gpt4 key购买 nike

我正在尝试读取文件,但我需要使用 Spring Integration DSL 逐行分割它。我需要添加到我的集成流程中才能使其正常工作

消息来源

 @Bean
public MessageSource<File> sourceDirectory() {
FileReadingMessageSource messageSource = new FileReadingMessageSource();
messageSource.setDirectory(new File(fileSource));
return messageSource;
}

过滤

@Bean
public GenericSelector<File> onlyCSVs() {
return (file) -> file.getName().endsWith(".csv");
}

文件转换器

 @Bean
public FileToStringTransformer fileToStringTransformer() {
return new FileToStringTransformer();
}

集成流程

@Bean
public StandardIntegrationFlow integrationFlow() {
return IntegrationFlows
.from(sourceDirectory(), configurer -> configurer.poller(Pollers.fixedDelay(10000)))
.channel("fileInputChannel")

.filter(onlyCSVs())
.transform(fileToStringTransformer())
.handle(m -> System.out.println((String.valueOf(Math.random()) + m.getPayload())))
.get();
}

最佳答案

在 filetoString 转换器之后,我会添加另一个自定义转换器,它接受字符串并创建一个像这样的数组。

String.split("[\\r\\n]+")

它已经删除了空行,然后,我会在流程中添加一个 .split() ,以便它为每一行创建一条消息, .split() 已经可以与迭代器一起使用,以防将数组转换为列出 list ,就完成了。

它是这样的:

@Bean
public StandardIntegrationFlow integrationFlow() {
return IntegrationFlows
.from(sourceDirectory(), configurer -> configurer.poller(Pollers.fixedDelay(10000)))
.channel("fileInputChannel")

.filter(onlyCSVs())
.transform(fileToStringTransformer())
.transform(GenericMessage.class, message -> message.getPayload().split("[\\r\\n]+"))
.split()
.handle(m -> System.out.println((String.valueOf(Math.random()) + m.getPayload())))
.get();
}

关于java - 使用spring集成dsl逐行读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57612164/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com