Commit 1983b61
Add SMBCollection: unified fluent API for Sort-Merge Bucket operations
This introduces SMBCollection, a new fluent API that unifies and improves all SMB operations in Scio.
## Key Improvements
### 1. Unified API
Traditional SMB operations are fragmented across disjoint methods solving specific sub-problems:
- `sortMergeJoin` - read and join to SCollection
- `sortMergeTransform` - read, transform, and write back to SMB
- `sortMergeGroupByKey` - read single source to SCollection
- `sortMergeCoGroup` - read multiple sources to SCollection
SMBCollection provides a single, composable API for all SMB workflows.
### 2. Familiar SCollection-like Ergonomics
Uses familiar functional operations (`map`, `filter`, `flatMap`) instead of imperative callbacks:
**Before (Traditional API):**
```scala
sc.sortMergeTransform(classOf[Integer], usersRead)
.to(output)
.via { case (key, users, outputCollector) =>
users.foreach { user =>
val transformed = transformUser(user)
outputCollector.accept(transformed) // ❌ Imperative callback
}
}
```
**After (SMBCollection):**
```scala
SMBCollection.read(classOf[Integer], usersRead)
.flatMap(users => users.map(transformUser)) // ✅ Functional style
.saveAsSortedBucket(output)
```
### 3. Better Interoperability
Seamlessly convert between SMB and SCollection:
```scala
val base = SMBCollection.cogroup2(classOf[Integer], usersRead, accountsRead)
.map { case (_, (users, accounts)) => expensiveJoin(users, accounts) }
// SMB outputs (stay bucketed)
base.map(_.summary).saveAsSortedBucket(summaryOutput)
base.map(_.details).saveAsSortedBucket(detailsOutput)
// SCollection output (for non-SMB operations)
val sc = base.toDeferredSCollection().get
sc.filter(_.needsProcessing).saveAsTextFile(textOutput)
sc.run() // All outputs execute in one pass!
```
### 4. Zero-Shuffle Multi-Output (Massive Performance Gains)
Create multiple SMB outputs from the same computation with zero shuffles.
**Before (Traditional - SCollection fanout):**
```scala
// Reads once, joins once, BUT shuffles 3 times
val joined = sc.sortMergeJoin(classOf[Integer], usersRead, accountsRead)
.map { case (userId, (user, account)) =>
expensiveJoin(user, account) // Runs once ✓
}
// ❌ Each saveAsSortedBucket does a GroupByKey shuffle!
joined.map(_.summary).saveAsSortedBucket(summaryOutput) // Shuffle 1
joined.map(_.details).saveAsSortedBucket(detailsOutput) // Shuffle 2
joined.filter(_.isHighValue).saveAsSortedBucket(highValueOutput) // Shuffle 3
```
**After (SMBCollection - zero shuffles):**
```scala
// Reads once, joins once, zero shuffles!
val base = SMBCollection.cogroup2(classOf[Integer], usersRead, accountsRead)
.map { case (_, (users, accounts)) =>
expensiveJoin(users, accounts) // Runs ONCE
}
// ✅ Fan out to multiple SMB outputs - data already bucketed!
base.map(_.summary).saveAsSortedBucket(summaryOutput)
base.map(_.details).saveAsSortedBucket(detailsOutput)
base.filter(_.isHighValue).saveAsSortedBucket(highValueOutput)
sc.run() // Single pass execution
```
**Performance Impact:**
| Scenario | Traditional (SCollection fanout) | SMBCollection Multi-Output | Cost Reduction |
|----------|----------------------------------|----------------------------|----------------|
| 1TB → 3 SMB outputs | 1TB read + ~3TB shuffle | 1TB read, 0 shuffle | **~4× savings** |
| 2TB join → 5 outputs | 2TB read + ~10TB shuffle | 2TB read, 0 shuffle | **~6× savings** |
| 500GB → 10 outputs | 500GB read + ~5TB shuffle | 500GB read, 0 shuffle | **~11× savings** |
## Complete Example
See `SortMergeBucketMultiOutputExample` in scio-examples for a full working example showing how to create multiple derived datasets (summary, details, high-value users) from a single expensive user-account join with zero shuffles.
## API Design
- Type signature: `SMBCollection[K1, K2, V]` - tracks keys for type safety, methods work with V directly
- `read()` returns `Iterable[V]` without key wrapper
- `cogroup2()` returns `(K, (Iterable[L], Iterable[R]))`
- Standard transformations: `map`, `filter`, `flatMap` (not `mapValues`/`flatMapValues`)
- Side inputs: clean `(SideInputContext, V)` signature
- Auto-execution: outputs execute via `sc.onClose()` hook
## Limitations
- Currently supports up to 4-way cogroups (`cogroup2`, `cogroup3`, `cogroup4`)
- For 5-22 way cogroups, use traditional `sortMergeCoGroup`
- Note: This is not a systemic limitation - easily extensible by adding `cogroup5` through `cogroup22` methods
## Documentation
Updated documentation includes:
- Complete fluent API guide with multi-output examples
- API comparison table (fluent vs traditional)
- Performance impact analysis
- Migration examples
- When to use which API
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <[email protected]>1 parent c234ba6 commit 1983b61
File tree
26 files changed
+6816
-19
lines changed- scio-examples/src/main/scala/com/spotify/scio/examples/extra
- scio-smb/src
- main
- java/org/apache/beam/sdk/extensions/smb
- scala/com/spotify/scio/smb
- syntax
- test
- java/org/apache/beam/sdk/extensions/smb
- scala
- com/spotify/scio/smb
- org/apache/beam/sdk/extensions/smb
- site/src/main/paradox
- extras
26 files changed
+6816
-19
lines changedLines changed: 101 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
280 | 280 | | |
281 | 281 | | |
282 | 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 | + | |
Lines changed: 25 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| |||
44 | 46 | | |
45 | 47 | | |
46 | 48 | | |
47 | | - | |
| 49 | + | |
| 50 | + | |
48 | 51 | | |
49 | 52 | | |
50 | 53 | | |
| |||
55 | 58 | | |
56 | 59 | | |
57 | 60 | | |
58 | | - | |
| 61 | + | |
59 | 62 | | |
60 | 63 | | |
61 | 64 | | |
| |||
64 | 67 | | |
65 | 68 | | |
66 | 69 | | |
67 | | - | |
| 70 | + | |
68 | 71 | | |
69 | 72 | | |
70 | 73 | | |
| |||
76 | 79 | | |
77 | 80 | | |
78 | 81 | | |
79 | | - | |
| 82 | + | |
80 | 83 | | |
81 | 84 | | |
82 | 85 | | |
| |||
91 | 94 | | |
92 | 95 | | |
93 | 96 | | |
94 | | - | |
| 97 | + | |
95 | 98 | | |
96 | 99 | | |
97 | 100 | | |
| |||
110 | 113 | | |
111 | 114 | | |
112 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
113 | 133 | | |
114 | 134 | | |
115 | 135 | | |
| |||
0 commit comments