What is the best way to import data from few csv files in Spring Batch? I mean one csv file responds to one table in database. I created one batch configuration class for each table and every table has its own job and step. Is there any solution to do this in more elegant way?
-
if you do not need sophisticated mapping and/or to apply business rules, you can look into the import capabilities of your database, e.g. for mysql dev.mysql.com/doc/refman/5.7/en/load-data.htmlMichael Pralow– Michael Pralow2016-07-18 06:36:29 +00:00Commented Jul 18, 2016 at 6:36
1 Answer
There's a variety of ways you could tackle the problem, but the simplest job would look something like:
FlatFileItemWriterreader with aDelmitedLineTokenizerandBeanWrapperFieldSetMapperto read the file- Processor if you need to do any additional validation/filtering/transformation
JDBCBatchItemWriterto insert/update the target table
Here's an example that includes more information around specific dependencies, config, etc. The example uses context file config rather than annotation-based, but it should be sufficient to show you the way.
A more complex solution might be a single job with a partitioned step that scans the input folder for files and, leveraging reference table/schema information, creates a reader/writer step for each file that it finds.
You also may want to consider what to do with the files once you're done... Delete them? Compress them?