我已经创建了3个文件,创建了3个本地文件。但是读取的所有信息都以相同的方式进入每个文件。我试图设置一些逻辑,以便只有被调用的项才能进入它们的正确文件。例如:创建filea、fileb、filec。当读者点击mongodb时,它应该解析buisness列并找到3种不同的类型:typea、typeb和typec。一旦读取器找到了不同的类型,它应该将其添加到相应的文件中,而不是将所有内容打印到每个创建的文件中。filea应该只有typea的信息,除此之外不应该有任何其他信息。有办法吗?下面是我的代码。
项目阅读器:
@Bean
public MongoItemReader<PaymentAudit> mongoreader() {
LOG.debug("Mongo-Reader");
@SuppressWarnings("unchecked")
MongoItemReader<PaymentAudit> mongoreader = new MongoItemReader();
mongoreader.setTemplate(mongoTemplate);
mongoreader.setQuery("{}");
mongoreader.setTargetType(PaymentAudit.class);
mongoreader.setSort(new HashMap<String, Sort.Direction>() {
{
put("_id", Direction.ASC);
}
});
return mongoreader;
}
文件项编写器:
@StepScope
@Bean
public FlatFileItemWriter<PaymentAudit> writer() {
LOG.debug("Mongo-writer");
String exportFilePath="C:\\filewriter\\retail.txt";
FlatFileItemWriter<PaymentAudit> flatFile = new
FlatFileItemWriterBuilder<PaymentAudit>()
.name("")
.resource(new FileSystemResource(exportFilePath))
.lineAggregator(createPaymentPortalLineAggregator())
.build();
String exportFileHeader = "TypeA";
StringHeaderWriter headerWriter = new
StringHeaderWriter(exportFileHeader);
flatFile.setHeaderCallback(headerWriter);
return flatFile;
}
@Bean
public FlatFileItemWriter<PaymentAudit> writer2() {
LOG.debug("flatFileItemWriter");
String exportFilePath="C:\\filewriter\\hcc.txt";
FlatFileItemWriter<PaymentAudit> flatFile = new
FlatFileItemWriterBuilder<PaymentAudit>()
.name("")
.resource(new FileSystemResource(exportFilePath))
.lineAggregator(createPaymentPortalLineAggregator())
.build();
String exportFileHeader = "TypeB";
StringHeaderWriter headerWriter = new
StringHeaderWriter(exportFileHeader);
flatFile.setHeaderCallback(headerWriter);
return flatFile;
}
@Bean
public FlatFileItemWriter<PaymentAudit> writer3() {
LOG.debug("Mongo-writer");
String exportFilePath="C:\\filewriter\\srx.txt";
FlatFileItemWriter<PaymentAudit> flatFile = new
FlatFileItemWriterBuilder<PaymentAudit>()
.name("")
.resource(new FileSystemResource(exportFilePath))
.lineAggregator(createPaymentPortalLineAggregator())
.build();
String exportFileHeader = "TypeC";
StringHeaderWriter headerWriter = new
StringHeaderWriter(exportFileHeader);
flatFile.setHeaderCallback(headerWriter);
return flatFile;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public CompositeItemWriter<PaymentAudit> compositeItemWriter(){
CompositeItemWriter writer = new CompositeItemWriter();
private LineAggregator<PaymentAudit> createPaymentPortalLineAggregator() {
DelimitedLineAggregator<PaymentAudit> lineAggregator = new
DelimitedLineAggregator<>();
lineAggregator.setDelimiter("|");
FieldExtractor<PaymentAudit> fieldExtractor =
createPaymentPortalFieldExtractor();
lineAggregator.setFieldExtractor(fieldExtractor);
return lineAggregator;
}
private FieldExtractor<PaymentAudit> createPaymentPortalFieldExtractor() {
BeanWrapperFieldExtractor<PaymentAudit> extractor = new
BeanWrapperFieldExtractor<>();
extractor.setNames(new String[] { "TypeA, TypeB, TypeC"});
return extractor;
}
1条答案
按热度按时间bf1o4zei1#
你需要使用
ClassifierCompositeItemWriter
以便对项目进行分类并将每种类型写入相应的文件中。下面是一个您可以尝试的快速示例:此示例读取一些
Person
项目,并用名称foo*
至foos.txt
还有那些有名字的bar*
至bars.txt
.希望这有帮助。