我正在学习 spring 批处理,我对阅读器、映射器和编写器有基本的了解。现在我当前的需求是从文件中读取每一行,压缩数据并写入基于千兆空间的内存网格。
我知道线映射器是 itam 阅读器的强制性属性。我没有使用创建的映射器,我需要做的就是读取该行并将其发送给编写器以在网格中写入。那么我怎样才能跳过线映射器或者我怎样才能阅读普通线。目前我已经做了这样的事情,这似乎不是理想的解决方案。
public class ShrFileMapper implements FieldSetMapper<SpaceDocument> {
@Override
public SpaceDocument mapFieldSet(FieldSet fieldSet) throws BindException {
String positionId = fieldSet.readString(0);
StringBuffer line = new StringBuffer();
for (String fieldValue : fieldSet.getValues()) {
line.append("\t").append(fieldSet);
}
// logic for compression
SpaceDocument spaceDocument = new SpaceDocument("shr.doc");
spaceDocument.setProperty("id", positionId);
spaceDocument.setProperty("payload", compressedString);
return spaceDocument;
}
}
最佳答案
假设您使用的是 FlatFileItemReader,则需要提供资源和 LineMapper。由于您不想将输入行转换为不需要 LineTokenizer 的任何其他内容,因此您只想传递原始输入。有关更多信息,您可以查看官方文档:
http://docs.spring.io/spring-batch/reference/html/readersAndWriters.html#flatFileItemReader
Spring 已经提供了这样的功能。
请查看 PassThroughLineMapper https://github.com/spring-projects/spring-batch/blob/master/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/mapping/PassThroughLineMapper.java
public class PassThroughLineMapper implements LineMapper<String>{
@Override
public String mapLine(String line, int lineNumber) throws Exception {
return line;
}
}
这门课正是你所需要的!
关于java - 如何在没有任何映射器的情况下使用 Spring Batch 读取完整的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38715090/