To add to Eugene's excellent answer as well as the watchfornewfiles options there are a couple of other choices;
There are several options available to solve this requirement dependent on your latency requirements. As of SDK 2.9.0:
Option 1: Continuous read mode:
Java:
FileIO , TextIO and several other IO sources support continuous reading of the source for new files.
FileIO class supports the ability to watch a single file pattern continuously.
This example matches a single filepattern repeatedly every 30 seconds, continuously returns new matched files as an unbounded PCollection and stops if no new files appear for 1 hour.
PCollection<Metadata> matches = p.apply(FileIO.match()
.filepattern("...")
.continuously(
Duration.standardSeconds(30), afterTimeSinceNewOutput(Duration.standardHours(1))));
TextIO class supports streaming new file matching using the watchForNewFiles property.
PCollection<String> lines = p.apply(TextIO.read()
.from("/local/path/to/files/*")
.watchForNewFiles(
// Check for new files every minute
Duration.standardMinutes(1),
// Stop watching the filepattern if no new files appear within an hour
afterTimeSinceNewOutput(Duration.standardHours(1))));
It is important to note that the file list is not retained across restarts of the pipeline. To deal with that scenario, you can move the files either through a process downstream of the pipeline or as part of the pipeline itself. Another option would be to store processed file names in an external file and de-dupe the lists at the next transform.
Python:
The continuously option is not available as of SDK 2.9.0 for python.
Option 2: Stream processing triggered from external source
You can have a Beam pipeline running in stream mode, which has an unbounded source, for example PubSub. When new files arrive you can use an external to Beam process to detect the file arrival and then send a PubSub message which has a URI as payload to the file. In a DoFn which is preceded by the PubSub source you can then use that URI to process the file.
Java :
Use an Unbounded Source IO ( PubSubIO, KafakIO, etc...)
Python:
Use an UnBounded Source IO ( PubSubIO, etc...)
Option 3: Batch mode processing triggered from external source
This approach, introduces latency over Option 1 & 2 as the pipeline needs to startup before processing can begin. Here you can have a triggering event from your source file system to schedule or immediately start a Dataflow process. This option is best suited for low frequency large file size updates.