This post is part of a series delving into the details of the JSR-352 (Java Batch) specification. Each post examines a very specific part of the specification and looks at how it works and how you might use it in a real batch application.To start at the beginning, follow the link to the first post.The next post in the series is
here.
This series is also available as a podcast on iTunes, Google Play, Stitcher, or use the link to the RSS feed.
-----
Well, with all those ways to read a CSV file that we talked about last time, surely there must be matching implementations to write a CSV file (and stop calling me Shirley…).
Why yes there are. BatchEE provides a JSefaCsvWriter and JBeret provides both a CsvItemWriter and a JacksonCsvItemWriter.
The JSefaCsvWriter is built on a more basic JSefaWriter. The JacksonCsvItemWriter is built on a JacksonCsvItemReaderWriter, and the CsvItemWriter is built on the CsvItemReaderWriter. Those last two are shared with the Jackson and Csv Readers we talked about last time.
Which means that these all work pretty much the same as the readers did. Each one makes use of the underlying library to do the conversion into a CSV format. And in each case it is going to map the attributes of the Object passed into the writer into columns of data.
I thought the header mechanism was interesting. You can, of course, just write CSV data without a first line of column headers. But it is generally more helpful to have a header. The CsvItemWriter allows you to specify a ‘header’ batch property which it will dutifully write after it opens the output file. So you’d have to know in the JSL what you want it to be.
Looking at the JacksonCsvItemWriter I didn’t see a way to get a header at all, but maybe I missed it.
The JSefaCsvWriter has a property indicating whether or not to include a header and allows you to specify the header string, like CsvItemWriter. But it can also look at annotations in the Object being mapped for @CsvField and can use the attribute name as the column header. That’s pretty slick.
I should probably point out here that I don’t have any actual practical experience with any of these. I’m just casually browsing through the code and commenting. If you’re really interested in using any of these you should definitely do a little digging. I’m sure there are online resources where you can get less casual information and answers to questions. I just found all this stuff to be pretty interesting and worth a casual stroll through it..