Path: blob/master/site/en-snapshot/datasets/beam_datasets.md
25115 views
Generating big datasets with Apache Beam
Some datasets are too big to be processed on a single machine. tfds
supports generating data across many machines by using Apache Beam.
This doc has two sections:
For user who want to generate an existing Beam dataset
For developers who want to create a new Beam dataset
Generating a Beam dataset
Below are different examples of generating a Beam dataset, both on the cloud or locally.
Warning: When generating the dataset with the tfds build
CLI, make sure to specify the dataset config you want to generate or it will default to generate all existing configs. For example, for wikipedia, use tfds build wikipedia/20200301.en
instead of tfds build wikipedia
.
On Google Cloud Dataflow
To run the pipeline using Google Cloud Dataflow and take advantage of distributed computation, first follow the Quickstart instructions.
Once your environment is set up, you can run the tfds build
CLI using a data directory on GCS and specifying the required options for the --beam_pipeline_options
flag.
To make it easier to launch the script, it's helpful to define the following variables using the actual values for your GCP/GCS setup and the dataset you want to generate:
You will then need to create a file to tell Dataflow to install tfds
on the workers:
If you're using tfds-nightly
, make sure to echo from tfds-nightly
in case the dataset has been updated since the last release.
Finally, you can launch the job using the command below:
Locally
To run your script locally using the default Apache Beam runner (it must fit all data in memory), the command is the same as for other datasets:
Warning: Beam datasets can be huge (terabytes or larger) and take a significant amount of resources to be generated (can take weeks on a local computer). It is recommended to generate the datasets using a distributed environment. Have a look at the Apache Beam Documentation for a list of supported runtimes.
With Apache Flink
To run the pipeline using Apache Flink you can read the official documentation. Make sure your Beam is compliant with Flink Version Compatibility
To make it easier to launch the script, it's helpful to define the following variables using the actual values for your Flink setup and the dataset you want to generate:
To run on an embedded Flink cluster, you can launch the job using the command below:
With a custom script
To generate the dataset on Beam, the API is the same as for other datasets. You can customize the beam.Pipeline
using the beam_options
(and beam_runner
) arguments of DownloadConfig
.
Implementing a Beam dataset
Prerequisites
In order to write Apache Beam datasets, you should be familiar with the following concepts:
Be familiar with the
tfds
dataset creation guide as most of the content still applies for Beam datasets.Get an introduction to Apache Beam with the Beam programming guide.
If you want to generate your dataset using Cloud Dataflow, read the Google Cloud Documentation and the Apache Beam dependency guide.
Instructions
If you are familiar with the dataset creation guide, adding a Beam dataset only requires to modify the _generate_examples
function. The function should returns a beam object, rather than a generator:
Non-beam dataset:
Beam dataset:
All the rest can be 100% identical, including tests.
Some additional considerations:
Use
tfds.core.lazy_imports
to import Apache Beam. By using a lazy dependency, users can still read the dataset after it has been generated without having to install Beam.Be careful with Python closures. When running the pipeline, the
beam.Map
andbeam.DoFn
functions are serialized usingpickle
and sent to all workers. Do not use mutable objects inside abeam.PTransform
if the state has to be shared across workers.Due to the way
tfds.core.DatasetBuilder
is serialized with pickle, mutatingtfds.core.DatasetBuilder
during data creation will be ignored on the workers (e.g. it's not possible to setself.info.metadata['offset'] = 123
in_split_generators
and access it from the workers likebeam.Map(lambda x: x + self.info.metadata['offset'])
)If you need to share some pipeline steps between the splits, you can add add an extra
pipeline: beam.Pipeline
kwarg to_split_generator
and control the full generation pipeline. See_generate_examples
documentation oftfds.core.GeneratorBasedBuilder
.
Example
Here is an example of a Beam dataset.
Running your pipeline
To run the pipeline, have a look at the above section.
Note: Like for non-beam datasets, do not forget to register download checksums with --register_checksums
(only the first time to register the downloads).
Pipeline using TFDS as input
If you want to create a beam pipeline which takes a TFDS dataset as source, you can use the tfds.beam.ReadFromTFDS
:
It will process each shard of the dataset in parallel.
Note: This require the dataset to be already generated. To generate datasets using beam, see the other sections.