如何使用 spring-batch 和 MultiResourceItemReader 读取文件夹中的所有文件?
How to read all files in a folder with spring-batch and MultiResourceItemReader?
我想配置 spring-batch
以按顺序读取特定文件夹中的所有 csv
文件。
以下内容不起作用,因为代表将尝试打开名为 *.csv
的文件,这当然是无效的。我必须在这里更改什么?
@Bean
public ItemReader<String> reader() {
MultiResourceItemReader<String> reader = new MultiResourceItemReader<>();
reader.setResources(new Resource[] {new FileSystemResource("/myfolder/*.csv")});
reader.setDelegate(new FlatFileItemReader<>(..));
return reader;
}
等价的xml配置应该写成下面这样,怎么改写成java only config?
<bean id="reader" class="org.springframework.batch.item.file.MultiResourceItemReader">
<property name="resources" value="/mypfolder/*.csv"/>
<property name="delegate" ref="flatFileItemReader"/>
</bean>
我认为你应该使用 PathMatchingResourcePatternResolver
。
像这样使用 PathMatchingResourcePatternResolver。
@Bean
public ItemReader<String> reader() {
Resource[] resources = null;
ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
try {
resources = patternResolver.getResources("/myfolder/*.csv");
} catch (IOException e) {
e.printStackTrace();
}
MultiResourceItemReader<String> reader = new MultiResourceItemReader<>();
reader.setResources(resources);
reader.setDelegate(new FlatFileItemReader<>(..));
return reader;
}
从绝对文件路径读取多个文件:
@Bean
public MultiResourceItemReader<POJO> multiResourceItemReader() {
MultiResourceItemReader<POJO> resourceItemReader = new MultiResourceItemReader<POJO>();
ClassLoader cl = this.getClass().getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
Resource[] resources = resolver.getResources("file:" + filePath );
resourceItemReader.setResources(resources);
resourceItemReader.setDelegate(reader());
return resourceItemReader;
}
@Bean
public FlatFileItemReader<POJO> reader() {
FlatFileItemReader<POJO> reader = new FlatFileItemReader<POJO>();
reader.setLineMapper(new DefaultLineMapper() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[]{"id1", "POJO"});
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<POJO>() {{
setTargetType(POJO.class);
}});
}});
return reader;
}
或者可以使用@Value 注解让 ResourceArrayPropertyEditor 将字符串转换为 Resource[]。
@Bean
@StepScope
public ItemReader<String> reader(
@Value("file:/myfolder/*.csv") Resource[] resources
) {
MultiResourceItemReader<String> reader = new MultiResourceItemReader<>();
reader.setResources(resources);
reader.setDelegate(new FlatFileItemReader<>(..));
return reader;
}
我想配置 spring-batch
以按顺序读取特定文件夹中的所有 csv
文件。
以下内容不起作用,因为代表将尝试打开名为 *.csv
的文件,这当然是无效的。我必须在这里更改什么?
@Bean
public ItemReader<String> reader() {
MultiResourceItemReader<String> reader = new MultiResourceItemReader<>();
reader.setResources(new Resource[] {new FileSystemResource("/myfolder/*.csv")});
reader.setDelegate(new FlatFileItemReader<>(..));
return reader;
}
等价的xml配置应该写成下面这样,怎么改写成java only config?
<bean id="reader" class="org.springframework.batch.item.file.MultiResourceItemReader">
<property name="resources" value="/mypfolder/*.csv"/>
<property name="delegate" ref="flatFileItemReader"/>
</bean>
我认为你应该使用 PathMatchingResourcePatternResolver
。
像这样使用 PathMatchingResourcePatternResolver。
@Bean
public ItemReader<String> reader() {
Resource[] resources = null;
ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
try {
resources = patternResolver.getResources("/myfolder/*.csv");
} catch (IOException e) {
e.printStackTrace();
}
MultiResourceItemReader<String> reader = new MultiResourceItemReader<>();
reader.setResources(resources);
reader.setDelegate(new FlatFileItemReader<>(..));
return reader;
}
从绝对文件路径读取多个文件:
@Bean
public MultiResourceItemReader<POJO> multiResourceItemReader() {
MultiResourceItemReader<POJO> resourceItemReader = new MultiResourceItemReader<POJO>();
ClassLoader cl = this.getClass().getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
Resource[] resources = resolver.getResources("file:" + filePath );
resourceItemReader.setResources(resources);
resourceItemReader.setDelegate(reader());
return resourceItemReader;
}
@Bean
public FlatFileItemReader<POJO> reader() {
FlatFileItemReader<POJO> reader = new FlatFileItemReader<POJO>();
reader.setLineMapper(new DefaultLineMapper() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[]{"id1", "POJO"});
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<POJO>() {{
setTargetType(POJO.class);
}});
}});
return reader;
}
或者可以使用@Value 注解让 ResourceArrayPropertyEditor 将字符串转换为 Resource[]。
@Bean
@StepScope
public ItemReader<String> reader(
@Value("file:/myfolder/*.csv") Resource[] resources
) {
MultiResourceItemReader<String> reader = new MultiResourceItemReader<>();
reader.setResources(resources);
reader.setDelegate(new FlatFileItemReader<>(..));
return reader;
}