Aller au contenu

Overview

Ce contenu n’est pas encore disponible dans votre langue.

Spring Batch RS provides a rich set of item readers and writers for various data sources and formats. All readers and writers use the builder pattern for easy, type-safe configuration.

Enable only the features you need to keep your dependencies minimal:

Feature Description Status
csv CSV file reading and writing ✅ Stable
json JSON file reading and writing ✅ Stable
xml XML file reading and writing ✅ Stable
mongodb MongoDB database integration ✅ Stable
rdbc-postgres PostgreSQL via RDBC ✅ Stable
rdbc-mysql MySQL/MariaDB via RDBC ✅ Stable
rdbc-sqlite SQLite via RDBC ✅ Stable
orm SeaORM integration ✅ Stable
fake Mock data generation ✅ Stable
logger Debug logging writer ✅ Stable

CSV

Read and write CSV files with configurable delimiters, headers, and quoting

Learn More →

JSON

Handle JSON arrays and objects with pretty printing support

Learn More →

XML

Process XML documents with custom root and item elements

Learn More →

RDBC (Generic SQL)

Direct database access for PostgreSQL, MySQL, and SQLite using SQLx

Learn More →

Fake Data Generator

Generate realistic mock data for testing

Learn More →

use spring_batch_rs::item::csv::CsvItemReaderBuilder;
use serde::Deserialize;
#[derive(Deserialize)]
struct Product {
id: u32,
name: String,
price: f64,
}
let reader = CsvItemReaderBuilder::<Product>::new()
.has_headers(true)
.delimiter(b',')
.from_path("products.csv")?;
use spring_batch_rs::item::json::JsonItemWriterBuilder;
let writer = JsonItemWriterBuilder::<MyType>::new()
.pretty_formatter(true)
.from_path("output.json")?;
use spring_batch_rs::item::rdbc::RdbcItemReaderBuilder;
use sqlx::{PgPool, FromRow};
#[derive(Debug, Clone, FromRow)]
struct Product {
id: i64,
name: String,
price: f64,
}
let pool = PgPool::connect("postgresql://user:pass@localhost/db").await?;
let reader = RdbcItemReaderBuilder::<Product>::new()
.postgres(pool)
.query("SELECT id, name, price FROM products ORDER BY id")
.with_page_size(1000)
.build_postgres();
use spring_batch_rs::item::rdbc::RdbcItemWriterBuilder;
let writer = RdbcItemWriterBuilder::<Product>::new()
.postgres(&pool)
.table("products")
.column("id", |p: &Product| p.id.into())
.column("name", |p: &Product| p.name.as_str().into())
.column("price", |p: &Product| p.price.into())
.build_postgres();

Writes are sequential by default. .with_concurrency(N) allows up to N chunk INSERTs in flight at once on PostgreSQL and MySQL — opt-in, because it drops write ordering and delays write errors by up to N chunks. SQLite ignores it and logs a warning. See the Performance Tuning Guide.

You can create custom implementations by implementing the respective traits:

use spring_batch_rs::core::item::ItemReader;
use spring_batch_rs::BatchError;
struct MyCustomReader {
data: Vec<String>,
index: usize,
}
impl ItemReader<String> for MyCustomReader {
fn read(&mut self) -> ItemReaderResult<String> {
if self.index < self.data.len() {
let item = self.data[self.index].clone();
self.index += 1;
Ok(Some(item))
} else {
Ok(None) // No more items
}
}
}

Choose appropriate chunk sizes based on your data and memory:

// Small chunks (10-50): Memory-constrained environments
.chunk(25)
// Medium chunks (100-500): Balanced performance
.chunk(250)
// Large chunks (1000+): High-throughput scenarios
.chunk(1000)

For database readers, configure page sizes to optimize memory:

let reader = RdbcItemReaderBuilder::<Record>::new()
.postgres(pool)
.query("SELECT * FROM records ORDER BY id")
.with_page_size(500) // Fetch 500 records at a time
.build_postgres();

File-based readers and writers use buffered I/O by default for optimal performance.

Explore detailed documentation for each reader and writer type: