-
Notifications
You must be signed in to change notification settings - Fork 524
Add SMBCollection: unified fluent API for Sort-Merge Bucket operations #5848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
spkrka
wants to merge
2
commits into
spotify:main
Choose a base branch
from
spkrka:krka/smb-fluent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This introduces SMBCollection, a new fluent API that unifies and improves all SMB operations in Scio.
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.
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)
```
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!
```
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** |
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.
- 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
- 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
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]>
1983b61 to
5aad414
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #5848 +/- ##
==========================================
+ Coverage 61.49% 62.71% +1.21%
==========================================
Files 317 324 +7
Lines 11650 12218 +568
Branches 845 885 +40
==========================================
+ Hits 7164 7662 +498
- Misses 4486 4556 +70 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
4851f57 to
2fd00b9
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 SCollectionsortMergeTransform- read, transform, and write back to SMBsortMergeGroupByKey- read single source to SCollectionsortMergeCoGroup- read multiple sources to SCollectionSMBCollection 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):
After (SMBCollection):
3. Better Interoperability
Seamlessly convert between SMB and SCollection:
4. Zero-Shuffle Multi-Output (Massive Performance Gains)
Create multiple SMB outputs from the same computation with zero shuffles.
Before (Traditional - SCollection fanout):
After (SMBCollection - zero shuffles):
Performance Impact:
Complete Example
See
SortMergeBucketMultiOutputExamplein 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
SMBCollection[K1, K2, V]- tracks keys for type safety, methods work with V directlyread()returnsIterable[V]without key wrappercogroup2()returns(K, (Iterable[L], Iterable[R]))map,filter,flatMap(notmapValues/flatMapValues)(SideInputContext, V)signaturesc.onClose()hookLimitations
cogroup2,cogroup3,cogroup4)sortMergeCoGroupcogroup5throughcogroup22methodsDocumentation
Updated documentation includes:
🤖 Generated with Claude Code
Co-Authored-By: Claude Sonnet 4.5 [email protected]