user3140364
user3140364

Reputation: 1

Import data from few csv files to database in Spring Batch

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?

Upvotes: 0

Views: 994

Answers (1)

Dean Clark
Dean Clark

Reputation: 3868

There's a variety of ways you could tackle the problem, but the simplest job would look something like:

  • FlatFileItemWriter reader with a DelmitedLineTokenizer and BeanWrapperFieldSetMapper to read the file
  • Processor if you need to do any additional validation/filtering/transformation
  • JDBCBatchItemWriter to 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?

Upvotes: 1

Related Questions