Summary: Execute a SQL query against an external database with batched iteration, suitable for large datasets that cannot fit in memory at once.
URI Format
pfx-sql:selectIterator?sql=SELECT * FROM myTable&dataSource=myDb&batchSize=5000
Parameters
|
Parameter |
Type |
Default |
Required |
Description |
|---|---|---|---|---|
|
|
String |
|
No |
Name of the Spring DataSource bean |
|
|
String |
|
Yes |
SQL query to execute |
|
|
SQLDialect |
|
No |
SQL dialect |
|
|
Integer |
|
No |
Number of rows to fetch per batch |
|
|
Boolean |
|
No |
Enable batch mode for processing |
How It Works
-
The query is executed against the database.
-
Instead of loading all rows into memory, results are fetched in batches of
batchSizerows. -
The body is set to an iterator that yields one batch at a time.
-
Use a Camel
<split>to process each batch individually.
This approach keeps memory usage constant regardless of total result set size.
Examples
Batched Select with Split
<route id="batchedSelectFromDb">
<from uri="timer://dbFetch?repeatCount=1"/>
<to uri="pfx-sql:selectIterator?sql=SELECT * FROM products&dataSource=myDb&batchSize=5000"/>
<split>
<simple>${body}</simple>
<to uri="pfx-api:loaddata?objectType=P&mapper=productMapper"/>
</split>
</route>
Large Dataset with Custom Batch Size
<route id="largeDataset">
<from uri="timer://dbFetch?repeatCount=1"/>
<to uri="pfx-sql:selectIterator?sql=SELECT * FROM transactions&dataSource=myDb&batchSize=10000&dialect=ORACLE"/>
<split>
<simple>${body}</simple>
<to uri="pfx-api:loaddata?objectType=T&mapper=txMapper"/>
</split>
</route>
Batched Select from PostgreSQL
<route id="pgBatched">
<from uri="timer://dbFetch?repeatCount=1"/>
<to uri="pfx-sql:selectIterator?sql=SELECT * FROM large_table&dataSource=pgDb&batchSize=2000&dialect=POSTGRESQL"/>
<split>
<simple>${body}</simple>
<to uri="pfx-api:loaddata?objectType=P&mapper=pgMapper"/>
</split>
</route>
Common Pitfalls
|
Problem |
Cause |
Fix |
|---|---|---|
|
Only one batch processed |
Missing |
Always wrap downstream processing in a |
|
Slow performance |
|
Increase |
|
Memory issues despite iterator |
|
Reduce |
|
Results not split correctly |
Wrong split expression |
Use |
See Also
-
pfx-sql Component — Parent reference
-
pfx-sql:select — Simple select (loads all rows at once)