在春季批次中通过列顺序解析csv

时间:2015-04-13 22:06:06

标签: java spring spring-batch

我的格式为<...

Kitten URL,Kitten Name,# of Reviews,Rating,Categories,,,,,,,,,,,,,
www.happykitten.com,happykittem.com,111746,7.8,Clothes & Fashion,Fashion Accessories,Ladies wear,Menswear,,,,,,,,,,
animedkitten.co.uk,Animed Kitten,33918,9.6,Pets,,,,,,,,,,,,,

因此,第一列为Kitten URL,Kitten Name,# of Reviews,Rating,其余列为可能的类别,列为额外属性。

我尝试使用Spring批处理,因此我指定了用于表示此CSV的哑对象。我遇到的第一个问题是(使用the example from Spring documentation我不知道如何在标题中解析带有空格的CSV。是否可以像这样使用Spring批处理?我可以注释每个在Hibernate中使用csv列的标题?

我的愚蠢对象将会像......

public class ImportDataObject {
    private String kittenUrl;
    private String kittenName;
    private int numOfReviews;
    public String getKittenUrl() {
        return kittenUrl;
    }
    public void setKittenUrl(String kittenUrl) {
        this.kittenUrl = kittenUrl;
    }
    public String getKittenName() {
        return kittenName;
    }
    public void setKittenName(String kittenName) {
        this.kittenName = kittenName;
    }
    public int getNumOfReviews() {
        return numOfReviews;
    }
    public void setNumOfReviews(int numOfReviews) {
        this.numOfReviews = numOfReviews;
    }

}

我只想读取前两列,附加一些字符串,然后保留其余的CSV。

我还考虑了之后使用这些多个逗号的最佳方法。不幸的是,这就是我获取数据的方式,而且我不能改变这些数据。

1 个答案:

答案 0 :(得分:3)

您可以为对象实施FieldSetMapper,然后将其设置为DefaultLineMapper。您对FieldSetMapper的实现可以处理职位,您只能解析前几个职位并将其设置为您的bean。

以下是基于您发布的网址代码的建议:

reader.setLineMapper(new DefaultLineMapper<ImportDataObject>());
            setFieldSetMapper(new ImportDataObjectFieldSetMapper());
            }});
            setLinesToSkip(1); //skip header since read int will throw exception and I assume you do not need header info
        }});

然后更改为POJO对象以保存类别列表:

public class ImportDataObject {
    private String kittenUrl;
    private String kittenName;
    private int numOfReviews;
    private int rating; //add getters and setters
    private List<String> categories; //add getters and setters
    public String getKittenUrl() {
        return kittenUrl;
    }
    public void setKittenUrl(String kittenUrl) {
        this.kittenUrl = kittenUrl;
    }
    public String getKittenName() {
        return kittenName;
    }
    public void setKittenName(String kittenName) {
        this.kittenName = kittenName;
    }
    public int getNumOfReviews() {
        return numOfReviews;
    }
    public void setNumOfReviews(int numOfReviews) {
        this.numOfReviews = numOfReviews;
    }

}

这是FieldSetMapper

public class ImportDataObjectFieldSetMapper implements FieldSetMapper<ImportDataObject> {

    @Override
    public ImportDataObject mapFieldSet(final FieldSet fieldSet) throws BindException {
        final ImportDataObject importDataObject = new ImportDataObject();

        importDataObject.setKittenUrl(fieldSet.readString(0));
        importDataObject.setKittenName(fieldSet.readString(1));
        importDataObject.setNumOfReviews(fieldSet.readInt(2));
        importDataObject.setRating(fieldSet.readInt(3));

        importDataObject.setCategories(new ArrayList<String>());
        for (int i = 4; i < fieldSet.getFieldCount(); i++) {
            importDataObject.getCategories().add(fieldSet.readString(i));
        }


        return importDataObject;
    }
}