简体   繁体   English

将春季批处理中的不良行排除到ItemWriter

[英]excluding bad lines in spring batch to ItemWriter

Cob Date;Supplier Code;Customer Code;
20180123;ABCLTD;43434;
20180123;BCD;4645;
;;;;;;;;
;;;;;;;

How can I avoid passing lines with ;;;;;; 我如何避免用;;;;;; to the writer? 给作家?

My mapper class looks something like this: 我的映射器类看起来像这样:

public class Mapper implements FieldSetMapper<Model> {


    @Override
    public PosRow mapFieldSet(FieldSet fieldSet) {

        if (fieldSet == null) {
            return null;
        }

        Model pos = new Model();

        try {

            if (!StringUtils.isEmpty(fieldSet.readString("cobDate"))) {
                pos.setCobDate(new Date());
            }

            } catch(...) {}

            }
            return pos;
    }

I check for fieldset==null and then check for each delimited fields and set the values to model. 我检查fieldset==null ,然后检查每个定界字段并将值设置为model。 Next, in my processor looks like this: 接下来,在我的处理器中看起来像这样:

public class ModelItemProcessor implements ItemProcessor<Model, AnotherModel> {

    private Set<AnotherModel> anotherModelSet= new HashSet<>();

    @Override
    public AnotherModel process(final Model posRow) throws Exception {

        if (posRow == null) {

        }

        if (!posRow.isValid()) {
            return null;
        }


       AnotherModel scp = new AnotherModel();

      //sett values

        if (anotherModelSet.contains(scp)) return null;

        anotherModelSet.add(scp);

        return scp;
    }

but still in my write() method i get items that are of size 0. 但仍然在我的write()方法中,我得到了大小为0的项目。

Make sure that your processor returns null when a record is invalid. 确保记录无效时处理器返回null。 In your code, you're catching exceptions in the mapFieldSet() call, but you're returning an empty PosRow record. 在您的代码中,您在mapFieldSet()调用中捕获了异常,但返回的是空的PosRow记录。 Instead, try something like: 相反,请尝试如下操作:

    try {

        if (!StringUtils.isEmpty(fieldSet.readString("cobDate"))) {
            pos.setCobDate(new Date());
        }

        } 
        catch(...) {
            return null;
        }

        }
        return pos;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM