Sander Ploegsma created BEAM-5307:
-------------------------------------

             Summary: Allow injection of state in DoFn.FinishBundle
                 Key: BEAM-5307
                 URL: https://issues.apache.org/jira/browse/BEAM-5307
             Project: Beam
          Issue Type: Improvement
          Components: sdk-java-core
            Reporter: Sander Ploegsma
            Assignee: Kenneth Knowles


Example use case: a stateful {{DoFn}} that requires persisting its state to an 
external database. Instead of writing to the external database for each 
element, it is much more efficient to flush the state every once in a while, or 
when cleaning up.

Currently, this is not possible because the {{@StateId}} injection is only 
available in processing functions and timers. 
[This|https://stackoverflow.com/questions/51789776/calculating-deltas-in-apache-beam-using-stateful-processing]
 might be a workaround, but I'm not even sure if it works.

Instead, by allowing the use of {{@StateId}} inside a {{@FinishBundle}} 
function, we can make sure the internal state is persisted in all scenarios:


{code:java}
@FinishBundle
public void flush(FinishBundleContext context, @StateId("myState") 
ValueState<StateObj> state) {
    repository.save(state.read());
}
{code}


 



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to