gpt4 book ai didi

java - 从 FlatFileItemReader LineMapper 访问平面文件的名称

转载 作者:行者123 更新时间:2023-11-29 04:32:51 25 4
gpt4 key购买 nike

我需要能够访问正在读取的当前行所在的平面文件的名称。我们正在移植一个应用程序,目前的一些流程需要此信息。

这是我当前的配置:

private FlatFileItemReader<Invoice> invoiceFlatFileItemReader() throws Exception {
FlatFileItemReader<Invoice> reader = new FlatFileItemReader<>();
reader.setLinesToSkip(1); // header row
reader.setLineMapper(new InvoiceLineMapper());
reader.afterPropertiesSet();

return reader;
}

发票行映射器:

public class InvoiceLineMapper implements LineMapper<Invoice> {

@Override
public Invoice mapLine(String line, int lineNumber) throws Exception {
String[] fields = line.split(",");
ArrayList<String> fieldList = new ArrayList<>();

for (int i = 0; i < fields.length; i++) {
fieldList.add(fields[i].replaceAll("\"", "");
}

Invoice invoice = new Invoice();

invoice.setCustomerNumber(Integer.parseInt(fieldList.get(0));
invoice.setCustomerName(fieldList.get(1));

// set other things and stuff...

//Need to be able to set the file name on the model here
invoice.setFileName(theFileName);
}
}

我通过 MultiResourceItemReader 委托(delegate)给 invoiceFlatFileItemReader():

@Bean
public ItemReader<Invoice> invoiceItemReader() throws Exception {
ResourcePatternResolver resolver = new PathMatchingResourcePatterResolver(this.getClass().getClassLoader());

// currently being loaded from the classpath, eventually will be read from S3...that comes later
Resource[] resources = resolver.getResources("classpath*:**/InvoiceList*.csv*");

MultiResourceItemReader<Invoice> reader = new MultiResourceItemReader<>();
reader.setResources(resources);
reader.setDelegate(invoiceFlatFileItemReader());

return reader;
}

我怎样才能获得资源的名称沿着链传递到我可以在 InvoiceLineMapper 类中访问它的位置并将其添加到模型(稍后通过 RepositoryItemWriter)?

最佳答案

这是我最后做的,只是将 MultiItemResourceReader 传递到链中,以便我可以在行映射器中调用 getCurrentResource():

@Bean
public ItemReader<Invoice> invoiceItemReader() throws Exception {
ResourcePatternResolver resolver = new PathMatchingResourcePatterResolver(this.getClass().getClassLoader());

// currently being loaded from the classpath, eventually will be read from S3...that comes later
Resource[] resources = resolver.getResources("classpath*:**/InvoiceList*.csv*");

MultiResourceItemReader<Invoice> reader = new MultiResourceItemReader<>();
reader.setResources(resources);
reader.setDelegate(invoiceFlatFileItemReader(reader));

return reader;
}

private FlatFileItemReader<Invoice> invoiceFlatFileItemReader(MultiResourceItemReader delegator) throws Exception {
FlatFileItemReader<Invoice> reader = new FlatFileItemReader<>();
reader.setLinesToSkip(1); // header row
reader.setLineMapper(new InvoiceLineMapper(delegator));
reader.afterPropertiesSet();

return reader;
}

然后在 InvoiceLineMapper 中:

public class InvoiceLineMapper implements LineMapper<Invoice> {

private MultiResourceItemReader delegator;

public InvoiceLineMapper(MultiResourceItemReader delegator) {
this.delegator = delegator;
}

@Override
public Invoice mapLine(String line, int lineNumber) throws Exception {
String[] fields = line.split(",");
ArrayList<String> fieldList = new ArrayList<>();

for (int i = 0; i < fields.length; i++) {
fieldList.add(fields[i].replaceAll("\"", "");
}

Invoice invoice = new Invoice();

invoice.setCustomerNumber(Integer.parseInt(fieldList.get(0));
invoice.setCustomerName(fieldList.get(1));

// set other things and stuff...

Resource currentResource = delegator.getCurrentResource();
String[] fileName = currentResource.getFilename().split("/");

invoice.setFileName(filename[filename.length - 1]);

return invoice;
}
}

关于java - 从 FlatFileItemReader LineMapper 访问平面文件的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43097612/

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