BeanIO 如何 skip/filter @Group in fixedLength

BeanIO how to skip/filter @Group in fixedLength

我正在尝试读取固定长度的文件。 我想提取文档列表,但 filtered 仅使用字段值为“BB”的 RecordHeader (RH)。 所以跳过 Document with recordHeader equals:

RH20210607AA   

这是我的class文档

的示例
@Group
public class Document{

    @Record(order = 1, minOccurs=1, maxOccurs=1)
    private RH recordHeader;
    @Record(order = 2, minOccurs=1, maxOccurs = -1, type=RD.class, collection = List.class)
    private List<RD> recordDetails;
}

这是我的 class RH

的例子
@Record
public class RH{

    @Field(ordinal = 0, required = true, length = 2, align = Align.LEFT, rid = true, literal = "RH")
    private String recordType;

    @Field(ordinal = 1, required = true, length=8, format = "yyyyMMdd")
    private LocalDate documentDate;

    @Field(ordinal = 2, required = true, length = 2, padding = ' ', align = Align.LEFT)
    private String documentCode;
}

这是固定长度文件的示例:

fake
Unknown record
Unknown record
RH20210607AA    
RDitem1
RDitem2
Unknown record
RH20210607BB    
RDitem2
RDitem3
Unknown record
Unknown record
Unknown record
RH20210607BB    
RDitem1
RDitem4
RDitem5

感谢帮助

您可以在 RH class 上使用多个字段来标识您想要的记录。你可以这样做:

@Record
public class RH {

  @Field(ordinal = 0, required = true, length = 2, align = Align.LEFT, rid = true, literal = "RH")
  private String recordType;

  @Field(ordinal = 1, required = true, length = 8, format = "yyyyMMdd")
  private Date documentDate;

  @Field(ordinal = 2, required = true, length = 2, padding = ' ', align = Align.LEFT, rid = true, literal = "BB")
  private String documentCode;

}

注意 documentCode 字段的 @Field 注释上的额外属性 --> rid = true, literal = "BB"

如果您需要包含更多 RH 条记录或将其用作排除过滤器,您可以使用正则表达式代替 literal = "BB"

您可能已经这样做了,您还需要配置 StreamFactory 以忽略无法识别的记录:

final StreamBuilder builder = new StreamBuilder("streamName")
    .readOnly()
    .format("fixedlength")
    .parser(new FixedLengthParserBuilder())
    .ignoreUnidentifiedRecords()             // <-- important
    .addGroup(...)
    .addRecord(RD.class));
factory.define(builder);