解析文件名时使用JsonItemReader,并且对每条记录进行处理。

huangapple go评论84阅读模式
英文:

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可以帮助您实现这一目标:

注意,ResourceAware 项目的一个典型用例是与 MultiResourceItemReader 一起使用,就像在您的情况中一样。

英文:

There are two APIs in Spring Batch that help you achieve that:

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&lt;Trade&gt; jsonItemReader() {
ObjectMapper objectMapper = new ObjectMapper();
// configure the objectMapper as required
JacksonJsonObjectReader&lt;Trade&gt; jsonObjectReader =new JacksonJsonObjectReader&lt;&gt;(Trade.class);
jsonObjectReader.setMapper(objectMapper);
return new JsonItemReaderBuilder&lt;Trade&gt;()
.jsonObjectReader(jsonObjectReader)
.resource(new ClassPathResource(&quot;trades.json&quot;))
.name(&quot;tradeJsonItemReader&quot;)
.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&lt;Customer&gt; multiResourceItemreader() {
MultiResourceItemReader&lt;Customer&gt; reader = new MultiResourceItemReader&lt;&gt;();
reader.setDelegate(customerItemReader());
reader.setResources(inputFiles);
return reader;
}
@Bean
public FlatFileItemReader&lt;Customer&gt; customerItemReader() {
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
tokenizer.setNames(new String[] { &quot;id&quot;, &quot;firstName&quot;, &quot;lastName&quot;, &quot;birthdate&quot; });
DefaultLineMapper&lt;Customer&gt; customerLineMapper = new DefaultLineMapper&lt;&gt;();
customerLineMapper.setLineTokenizer(tokenizer);
customerLineMapper.setFieldSetMapper(new CustomerFieldSetMapper());
customerLineMapper.afterPropertiesSet();
FlatFileItemReader&lt;Customer&gt; reader = new FlatFileItemReader&lt;&gt;();
reader.setLineMapper(customerLineMapper);
return reader;
}

huangapple
  • 本文由 发表于 2020年4月8日 05:22:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61089621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定