spring-batch-rs 0.3.4

A toolkit for building enterprise-grade batch applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
---
title: Getting Started
description: Learn how to build your first batch processing application with Spring Batch RS
sidebar:
  order: 2
---

import { Tabs, TabItem, Card, CardGrid, Code } from '@astrojs/starlight/components';

Welcome to Spring Batch RS! This guide will walk you through creating your first batch processing application in Rust.

## Prerequisites

Before you begin, ensure you have:

- **Rust 1.70+** installed ([Install Rust](https://rustup.rs/))
- Basic familiarity with Rust programming
- A text editor or IDE (VS Code with rust-analyzer recommended)

## Quick Start

### Step 1: Create a New Project

```bash
cargo new my-batch-app
cd my-batch-app
```

### Step 2: Add Dependencies

Add Spring Batch RS to your `Cargo.toml`:

```toml title="Cargo.toml"
[dependencies]
spring-batch-rs = { version = "0.3", features = ["csv", "json"] }
serde = { version = "1.0", features = ["derive"] }
```

### Step 3: Write Your First Batch Job

Create a simple CSV to JSON converter:

```rust title="src/main.rs"
use spring_batch_rs::{
    core::{job::JobBuilder, step::StepBuilder, item::PassThroughProcessor},
    item::{csv::CsvItemReaderBuilder, json::JsonItemWriterBuilder},
    BatchError,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize, Serialize)]
struct Product {
    id: u32,
    name: String,
    price: f64,
    category: String,
}

fn main() -> Result<(), BatchError> {
    // Sample CSV data
    let csv_data = r#"id,name,price,category
1,Laptop,999.99,Electronics
2,Coffee Mug,12.99,Kitchen
3,Notebook,5.99,Office
4,Wireless Mouse,29.99,Electronics"#;

    // Create CSV reader
    let reader = CsvItemReaderBuilder::<Product>::new()
        .has_headers(true)
        .from_reader(csv_data.as_bytes());

    // Create JSON writer
    let writer = JsonItemWriterBuilder::<Product>::new()
        .pretty_formatter(true)
        .from_path("products.json");

    // Create processor (pass-through in this case)
    let processor = PassThroughProcessor::<Product>::new();

    // Build the step
    let step = StepBuilder::new("csv-to-json-step")
        .chunk(10)
        .reader(&reader)
        .processor(&processor)
        .writer(&writer)
        .build();

    // Build and run the job
    let job = JobBuilder::new()
        .start(&step)
        .build();

    // Execute the job
    let result = job.run()?;

    println!("✅ Job completed successfully!");
    println!("📊 Processed {} steps", result.get_step_executions().len());

    Ok(())
}
```

### Step 4: Run Your Job

```bash
cargo run
```

You should see:

```
✅ Job completed successfully!
📊 Processed 1 steps
```

And a `products.json` file with your converted data!

## Understanding the Core Concepts

### The Job

A **Job** is the top-level container for your entire batch process. It's composed of one or more **Steps** that execute sequentially.

```rust
let job = JobBuilder::new()
    .start(&step1)      // First step
    .next(&step2)       // Second step (optional)
    .next(&step3)       // Third step (optional)
    .build();
```

### The Step

A **Step** represents an independent phase of processing. There are two types:

<Tabs>
  <TabItem label="Chunk-Oriented" icon="document">
    Process large datasets in configurable chunks using the read-process-write pattern:

    ```rust
    let step = StepBuilder::new("process-data")
        .chunk(100)              // Process 100 items at a time
        .reader(&reader)         // Read data source
        .processor(&processor)   // Transform items
        .writer(&writer)         // Write results
        .build();
    ```
  </TabItem>

  <TabItem label="Tasklet" icon="setting">
    Execute a single task or operation:

    ```rust
    let step = StepBuilder::new("cleanup")
        .tasklet(&cleanup_tasklet)
        .build();
    ```
  </TabItem>
</Tabs>

### ItemReader

An **ItemReader** retrieves input data one item at a time from various sources.

<Tabs>
  <TabItem label="CSV" icon="document">
    ```rust
    use spring_batch_rs::item::csv::CsvItemReaderBuilder;

    let reader = CsvItemReaderBuilder::<Product>::new()
        .has_headers(true)
        .delimiter(b',')
        .from_path("products.csv")?;
    ```
  </TabItem>

  <TabItem label="JSON" icon="seti:json">
    ```rust
    use spring_batch_rs::item::json::JsonItemReaderBuilder;

    let reader = JsonItemReaderBuilder::<Product>::new()
        .from_path("products.json")?;
    ```
  </TabItem>

  <TabItem label="Database" icon="seti:db">
    ```rust
    use spring_batch_rs::item::orm::OrmItemReaderBuilder;
    use sea_orm::{Database, EntityTrait};

    let db = Database::connect("sqlite::memory:").await?;
    let query = ProductEntity::find();

    let reader = OrmItemReaderBuilder::new()
        .connection(&db)
        .query(query)
        .page_size(100)
        .build();
    ```
  </TabItem>
</Tabs>

### ItemProcessor

An **ItemProcessor** applies business logic to transform or filter items.

```rust
use spring_batch_rs::core::item::ItemProcessor;

struct PriceDiscountProcessor {
    discount_rate: f64,
}

impl ItemProcessor<Product, Product> for PriceDiscountProcessor {
    fn process(&self, item: Product) -> ItemProcessorResult<Product> {
        let mut product = item;

        // Apply discount
        product.price *= (1.0 - self.discount_rate);

        // Filter out items below minimum price
        if product.price < 5.0 {
            return Ok(None);  // Skip this item
        }

        Ok(Some(product))
    }
}

// Usage
let processor = PriceDiscountProcessor { discount_rate: 0.15 };
```

:::tip[Filtering Items]
Return `Ok(None)` from your processor to skip an item without counting it as an error.
:::

### ItemWriter

An **ItemWriter** outputs processed items to various destinations.

<Tabs>
  <TabItem label="JSON" icon="seti:json">
    ```rust
    use spring_batch_rs::item::json::JsonItemWriterBuilder;

    // Replace MyType with your actual output type
    let writer = JsonItemWriterBuilder::<MyType>::new()
        .pretty_formatter(true)
        .from_path("output.json");
    ```
  </TabItem>

  <TabItem label="CSV" icon="document">
    ```rust
    use spring_batch_rs::item::csv::CsvItemWriterBuilder;

    let writer = CsvItemWriterBuilder::new()
        .has_headers(true)
        .delimiter(b',')
        .from_path("output.csv")?;
    ```
  </TabItem>

  <TabItem label="Database" icon="seti:db">
    ```rust
    use spring_batch_rs::item::orm::OrmItemWriterBuilder;

    let writer = OrmItemWriterBuilder::new()
        .connection(&db)
        .build();
    ```
  </TabItem>
</Tabs>

## Complete Example: Data Processing Pipeline

Let's build a more sophisticated batch job that:
1. Reads products from CSV
2. Applies business rules and transformations
3. Filters invalid items
4. Writes to JSON

```rust title="src/main.rs"
use spring_batch_rs::{
    core::{job::JobBuilder, step::StepBuilder, item::ItemProcessor},
    item::{csv::CsvItemReaderBuilder, json::JsonItemWriterBuilder},
    BatchError,
};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Clone)]
struct RawProduct {
    id: u32,
    name: String,
    price: f64,
    category: String,
    stock: i32,
}

#[derive(Serialize)]
struct ProcessedProduct {
    id: u32,
    name: String,
    final_price: f64,
    category: String,
    stock_status: String,
    price_tier: String,
}

struct ProductProcessor;

impl ItemProcessor<RawProduct, ProcessedProduct> for ProductProcessor {
    fn process(&self, item: RawProduct) -> ItemProcessorResult<ProcessedProduct> {
        // Validate price
        if item.price <= 0.0 {
            return Ok(None); // Skip invalid items
        }

        // Apply category-specific discount
        let discount = match item.category.as_str() {
            "Electronics" => 0.15,
            "Kitchen" => 0.10,
            "Office" => 0.05,
            _ => 0.0,
        };

        let final_price = item.price * (1.0 - discount);

        // Determine stock status
        let stock_status = if item.stock == 0 {
            "Out of Stock"
        } else if item.stock < 10 {
            "Low Stock"
        } else {
            "In Stock"
        }.to_string();

        // Classify price tier
        let price_tier = if final_price < 20.0 {
            "Budget"
        } else if final_price < 100.0 {
            "Mid-Range"
        } else {
            "Premium"
        }.to_string();

        Ok(Some(ProcessedProduct {
            id: item.id,
            name: item.name,
            final_price,
            category: item.category,
            stock_status,
            price_tier,
        }))
    }
}

fn main() -> Result<(), BatchError> {
    // Create input file with sample data
    let csv_data = r#"id,name,price,category,stock
1,Laptop,999.99,Electronics,25
2,Coffee Mug,12.99,Kitchen,150
3,Notebook,5.99,Office,8
4,Wireless Mouse,29.99,Electronics,0
5,Desk Lamp,45.00,Office,50"#;

    let reader = CsvItemReaderBuilder::<RawProduct>::new()
        .has_headers(true)
        .from_reader(csv_data.as_bytes());

    let writer = JsonItemWriterBuilder::<ProcessedProduct>::new()
        .pretty_formatter(true)
        .from_path("processed_products.json");

    let processor = ProductProcessor;

    let step = StepBuilder::new("process-products")
        .chunk(10)
        .reader(&reader)
        .processor(&processor)
        .writer(&writer)
        .build();

    let job = JobBuilder::new()
        .start(&step)
        .build();

    job.run()?;

    println!("✅ Processing complete! Check processed_products.json");

    Ok(())
}
```

## Error Handling & Fault Tolerance

Spring Batch RS provides robust error handling with configurable skip limits:

```rust
let step = StepBuilder::new("fault-tolerant-step")
    .chunk(100)
    .reader(&reader)
    .processor(&processor)
    .writer(&writer)
    .skip_limit(10)  // Allow up to 10 errors before failing
    .build();
```

### Best Practices

<CardGrid>
  <Card title="Start Small" icon="rocket">
    Begin with small chunk sizes (10-100) and adjust based on your data and memory constraints
  </Card>
  <Card title="Validate Early" icon="approve-check">
    Perform validation in your processor to catch errors before writing
  </Card>
  <Card title="Use Skip Limits Wisely" icon="warning">
    Set appropriate skip limits based on acceptable data quality thresholds
  </Card>
  <Card title="Log Errors" icon="information">
    Implement custom error logging to track skipped items for later review
  </Card>
</CardGrid>

## Next Steps

Now that you understand the basics, explore more advanced features:

<CardGrid>
  <Card title="Processing Models" icon="document">
    Learn about chunk-oriented vs tasklet processing patterns

    [Read More →](/processing-models/)
  </Card>
  <Card title="Item Readers & Writers" icon="seti:db">
    Explore all available data sources and destinations

    [View Options →](/item-readers-writers/overview/)
  </Card>
  <Card title="Tasklets" icon="setting">
    Use tasklets for file operations, FTP transfers, and custom tasks

    [Learn About Tasklets →](/tasklets/overview/)
  </Card>
  <Card title="Examples" icon="open-book">
    Browse comprehensive examples for every feature

    [See Examples →](/examples/)
  </Card>
</CardGrid>

## Need Help?

- 📖 [API Documentation](https://docs.rs/spring-batch-rs) - Complete API reference
- 💬 [Discord](https://discord.gg/9FNhawNsG6) - Chat with the community
- 🐛 [GitHub Issues](https://github.com/sboussekeyt/spring-batch-rs/issues) - Report bugs or request features
- 💡 [Discussions](https://github.com/sboussekeyt/spring-batch-rs/discussions) - Ask questions

Happy batch processing! 🚀