英文:
Parse filename while using JsonItemReader and process it with each record
问题
我正在使用spring-batch的多资源JsonItemReader来读取给定目录中的多个文件,这些文件中包含多个json对象。在处理每条记录时,我还需要知道该记录所来自的文件名。是否有任何方法可以提取文件名并在处理器中使用?
英文:
I am using spring-batch multi resource JsonItemReader to read multiple files with multiple json objects in a given directory. While processing each record, I also need the name of the file that particular record is coming from. Is there any way to extract the filename and use it in the processor?
答案1
得分: 1
在Spring Batch中有两个API可以帮助您实现这一目标:
- MultiResourceItemReader#getCurrentResource 提供了当前由委托读取器读取的资源。
- ResourceAware:如果您的项目实现了此接口,它将被注入从中读取的资源。
注意,ResourceAware
项目的一个典型用例是与 MultiResourceItemReader
一起使用,就像在您的情况中一样。
英文:
There are two APIs in Spring Batch that help you achieve that:
- MultiResourceItemReader#getCurrentResource gives you the current resource that is being read by the delegate reader.
- ResourceAware: if your item implements this interface, it will get injected the resource it was read from.
Note that a typical use case of ResourceAware
items is when used with a MultiResourceItemReader
, as in your case.
答案2
得分: 0
我认为你需要使用`CompositeItemReader`,并在`Processor`中(如果尚未创建,请创建)添加一个标志(将字段添加到您的模型类),并将文件名设置为该标志。
@Bean
public JsonItemReader<Trade> jsonItemReader() {
ObjectMapper objectMapper = new ObjectMapper();
// 根据需要配置objectMapper
JacksonJsonObjectReader<Trade> jsonObjectReader = new JacksonJsonObjectReader<>(Trade.class);
jsonObjectReader.setMapper(objectMapper);
return new JsonItemReaderBuilder<Trade>()
.jsonObjectReader(jsonObjectReader)
.resource(new ClassPathResource("trades.json"))
.name("tradeJsonItemReader")
.build();
}
注意:您需要实现MultiResourceItemReader,并根据上述建议区分来自哪个文件的记录。
以下代码仅供参考,不必按原样使用。
@Bean
public MultiResourceItemReader<Customer> multiResourceItemreader() {
MultiResourceItemReader<Customer> reader = new MultiResourceItemReader<>();
reader.setDelegate(customerItemReader());
reader.setResources(inputFiles);
return reader;
}
@Bean
public FlatFileItemReader<Customer> customerItemReader() {
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
tokenizer.setNames(new String[] { "id", "firstName", "lastName", "birthdate" });
DefaultLineMapper<Customer> customerLineMapper = new DefaultLineMapper<>();
customerLineMapper.setLineTokenizer(tokenizer);
customerLineMapper.setFieldSetMapper(new CustomerFieldSetMapper());
customerLineMapper.afterPropertiesSet();
FlatFileItemReader<Customer> reader = new FlatFileItemReader<>();
reader.setLineMapper(customerLineMapper);
return reader;
}
英文:
I think you need to use CompositeItemReader
and in the Processor
(create if you not already have), add one flag (add field to your model class) and set file name to that flag.
@Bean
public JsonItemReader<Trade> jsonItemReader() {
ObjectMapper objectMapper = new ObjectMapper();
// configure the objectMapper as required
JacksonJsonObjectReader<Trade> jsonObjectReader =new JacksonJsonObjectReader<>(Trade.class);
jsonObjectReader.setMapper(objectMapper);
return new JsonItemReaderBuilder<Trade>()
.jsonObjectReader(jsonObjectReader)
.resource(new ClassPathResource("trades.json"))
.name("tradeJsonItemReader")
.build();
}
Note: You need to implement the MultiResourceItemReader and based on above suugestions, you can differentiate your records coming from which file.
Below code is for reference, doesn't necessary mean to use as is.
@Bean
public MultiResourceItemReader<Customer> multiResourceItemreader() {
MultiResourceItemReader<Customer> reader = new MultiResourceItemReader<>();
reader.setDelegate(customerItemReader());
reader.setResources(inputFiles);
return reader;
}
@Bean
public FlatFileItemReader<Customer> customerItemReader() {
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
tokenizer.setNames(new String[] { "id", "firstName", "lastName", "birthdate" });
DefaultLineMapper<Customer> customerLineMapper = new DefaultLineMapper<>();
customerLineMapper.setLineTokenizer(tokenizer);
customerLineMapper.setFieldSetMapper(new CustomerFieldSetMapper());
customerLineMapper.afterPropertiesSet();
FlatFileItemReader<Customer> reader = new FlatFileItemReader<>();
reader.setLineMapper(customerLineMapper);
return reader;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论