CSV Processing
CSV (Comma-Separated Values) is one of the most common data formats for batch processing. Spring Batch RS provides powerful tools for reading and writing CSV files with full support for headers, custom delimiters, and data transformation.
Quick Start
Section titled “Quick Start”use spring_batch_rs::item::csv::{CsvItemReaderBuilder, CsvItemWriterBuilder};
// Read CSV with headerslet reader = CsvItemReaderBuilder::<Product>::new() .has_headers(true) .from_path("products.csv");
// Write CSV with headerslet writer = CsvItemWriterBuilder::<Product>::new() .has_headers(true) .from_path("output.csv");Features
Section titled “Features”- Header handling: Automatic header detection and generation
- Custom delimiters: Support for comma, semicolon, tab, and any custom delimiter
- Data transformation: Apply processors to transform data during processing
- Fault tolerance: Skip invalid records with configurable limits
- Type safety: Automatic deserialization into Rust structs using serde
Complete Example
Section titled “Complete Example”The csv_processing example demonstrates:
- Basic CSV to CSV: Copy CSV data with pass-through processing
- CSV to JSON with transformation: Apply discounts during conversion
- Custom delimiters: Process semicolon-separated files
- Fault tolerance: Handle malformed records gracefully
Run the Example
Section titled “Run the Example”cargo run --example csv_processing --features csv,jsonAPI Reference
Section titled “API Reference”CsvItemReaderBuilder
Section titled “CsvItemReaderBuilder”| Method | Description |
|---|---|
has_headers(bool) | Enable/disable header row parsing (default: false) |
delimiter(u8) | Set field delimiter (default: ,) |
from_reader(R) | Create reader from any Read source |
from_path(P) | Create reader from file path |
CsvItemWriterBuilder
Section titled “CsvItemWriterBuilder”| Method | Description |
|---|---|
has_headers(bool) | Include header row in output (default: false) |
delimiter(u8) | Set field delimiter (default: ,) |
from_writer(W) | Create writer for any Write destination |
from_path(P) | Create writer to file path |
Common Patterns
Section titled “Common Patterns”Reading with Custom Delimiter
Section titled “Reading with Custom Delimiter”// European CSV format with semicolonslet reader = CsvItemReaderBuilder::<Record>::new() .has_headers(true) .delimiter(b';') .from_path("data.csv");Writing Without Headers
Section titled “Writing Without Headers”// Raw data export without header rowlet writer = CsvItemWriterBuilder::<Record>::new() .has_headers(false) .from_path("export.csv");Error Handling with Skip Limit
Section titled “Error Handling with Skip Limit”let step = StepBuilder::new("csv-step") .chunk::<Input, Output>(100) .reader(&reader) .processor(&processor) .writer(&writer) .skip_limit(10) // Allow up to 10 parsing errors .build();See Also
Section titled “See Also”- JSON Processing - Convert CSV to JSON
- Database Processing - Import CSV to database
- Advanced Patterns - Multi-step ETL with CSV