-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding an ElementCollector to create batches to process elements
- Loading branch information
Sebastian Just
authored and
Sebastian Just
committed
Apr 2, 2016
1 parent
7f834a2
commit 98c17bf
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.sungard.dataflow; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.google.cloud.dataflow.sdk.transforms.Aggregator; | ||
import com.google.cloud.dataflow.sdk.transforms.DoFn; | ||
import com.google.cloud.dataflow.sdk.transforms.Sum; | ||
|
||
public class ElementCollector<T> extends DoFn<T, List<T>> { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public static interface ElementCollectorOptions { | ||
int getBatchSize(); | ||
void setBatchSize(int batchSize); | ||
} | ||
|
||
private final Aggregator<Integer, Integer> collectedElements; | ||
private final Aggregator<Integer, Integer> flushes; | ||
|
||
private final int batchSize; | ||
private ArrayList<T> buffer; | ||
|
||
public ElementCollector(ElementCollectorOptions options) { | ||
this.batchSize = options.getBatchSize(); | ||
|
||
this.collectedElements = createAggregator("Collected elements", new Sum.SumIntegerFn()); | ||
this.flushes = createAggregator("Buffer flushes", new Sum.SumIntegerFn()); | ||
} | ||
|
||
@Override | ||
public void startBundle(Context context) { | ||
buffer = new ArrayList<>(batchSize); | ||
} | ||
|
||
@Override | ||
public void processElement(ProcessContext context) { | ||
collectedElements.addValue(1); | ||
buffer.add(context.element()); | ||
|
||
if (buffer.size() >= batchSize) { | ||
flushBuffer(context); | ||
} | ||
} | ||
|
||
@Override | ||
public void finishBundle(Context context) { | ||
flushBuffer(context); | ||
} | ||
|
||
private void flushBuffer(Context context) { | ||
flushes.addValue(1); | ||
context.output(buffer); | ||
buffer.clear(); | ||
} | ||
} |