像这样创建一个 MultiResourceItemReader,
@Autowired
private AmazonS3 s3;
@Autowired
private ResourceLoader resourceLoader;
public MultiResourceItemReader<String> fileItemReader() throws Exception {
List<Resource> resourceList = new ArrayList<>();
String s3ResponseFilePath = "s3://bucket/path/"; //put your s3 path here
//TODO: warn: this functn can only return max 1000 objects
s3objects = s3.listObjects("bucket", s3ResponseFilePath).getObjectSummaries();
for(S3ObjectSummary it:s3objects)
resourceList.add(resourceLoader.getResource( "s3://" + s3Config.getBucket() + "/" + it.getKey()));
Resource[] resources = resourceList.toArray(new Resource[resourceList.size()]);
MultiResourceItemReader<String> reader = new MultiResourceItemReader<>();
reader.setResources(resources);
reader.setDelegate(flatFileItemReader());
return reader;
}
这个读者需要一个委托和lineMapper,你可以像这样实现它,
private FlatFileItemReader<String> flatFileItemReader() throws Exception {
FlatFileItemReader<String> reader = new FlatFileItemReader<>();
JsonLineMapper lineMapper = new JsonLineMapper();
reader.setLineMapper(lineMapper);
reader.afterPropertiesSet();
return reader;
}
public class JsonLineMapper implements LineMapper<String> {
private ObjectMapper mapper = new ObjectMapper();
@Override
public String mapLine(String s, int i) throws Exception {
return s;
}
}