I am just getting started with Google Data Flow, I have written a simple flow that reads a CSV file from cloud storage. One of the steps involves calling a web service to enrich results. The web service in question performs much better when sending several 100 requests in bulk.
In looking at API I don't see a great way to aggregate 100 elements of a PCollection into a single Par.do Execution. The results would need to be then split to handle the last step of the flow which is writing to a BigQuery table.
Not sure if I need to use windowing is what I want. Most of the windowing examples I see are more geared towards counting over a given time period.
You can buffer elements in a local member variable of your DoFn, and call your web service when the buffer is large enough, as well as in finishBundle. For example:
class CallServiceFn extends DoFn<String, String> {
private List<String> elements = new ArrayList<>();
public void processElement(ProcessContext c) {
elements.add(c.element());
if (elements.size() >= MAX_CALL_SIZE) {
for (String result : callServiceWithData(elements)) {
c.output(result);
}
elements.clear();
}
}
public void finishBundle(Context c) {
for (String result : callServiceWithData(elements)) {
c.output(result);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With