|
19 | 19 |
|
20 | 20 |
|
21 | 21 | class EmrPySparkResource(PySparkResourceDefinition): |
| 22 | + '''See the docstring on `emr_pyspark_resource` for instructions on using this resource. |
| 23 | + ''' |
| 24 | + |
22 | 25 | def __init__(self, config): |
23 | 26 | self.config = config |
24 | 27 | self.emr_job_runner = EmrJobRunner(region=self.config['region_name']) |
@@ -230,6 +233,45 @@ def running_on_emr(self): |
230 | 233 | } |
231 | 234 | ) |
232 | 235 | def emr_pyspark_resource(init_context): |
| 236 | + '''EMR Pyspark Resource. |
| 237 | +
|
| 238 | + The EMR pyspark resource provides the capability to run pyspark code transparently, without any |
| 239 | + code changes, across both your local machine and EMR. This is as straightforward as the |
| 240 | + following: |
| 241 | +
|
| 242 | + .. code-block:: python |
| 243 | +
|
| 244 | + from dagster import ModeDefinition, pipeline |
| 245 | + from dagster_aws.emr import emr_pyspark_resource |
| 246 | + from dagster_pyspark import pyspark_resource, pyspark_solid |
| 247 | +
|
| 248 | + @pyspark_solid |
| 249 | + def example_solid(context): |
| 250 | + list_p = [('John', 19), ('Jennifer', 29), ('Adam', 35), ('Henry', 50)] |
| 251 | + rdd = context.resources.pyspark.spark_context.parallelize(list_p) |
| 252 | + res = rdd.take(2) |
| 253 | + for name, age in res: |
| 254 | + context.log.info('%s: %d' % (name, age)) |
| 255 | +
|
| 256 | + @pipeline( |
| 257 | + mode_defs=[ |
| 258 | + ModeDefinition('prod', resource_defs={'pyspark': emr_pyspark_resource}), |
| 259 | + ModeDefinition('local', resource_defs={'pyspark': pyspark_resource}), |
| 260 | + ] |
| 261 | + ) |
| 262 | + def example_pipe(): |
| 263 | + example_solid() |
| 264 | +
|
| 265 | + When running locally, this will use the standard pyspark resource, which will bring up Spark |
| 266 | + locally and execute your code. In "prod" mode, for each pyspark solid, the EMR resource will: |
| 267 | +
|
| 268 | + 1. Sync your code to a zip file on S3, including a code-generated `main.py` to kickstart |
| 269 | + execution; |
| 270 | + 2. If a requirements.txt file is present (or specified via resource configuration), Python |
| 271 | + requirements will be installed on EMR prior to job execution; |
| 272 | + 3. An EMR job will be constructed for the solid and execution invoked. Job logs will be |
| 273 | + retrieved and logged if configured via `wait_for_logs`. |
| 274 | + ''' |
233 | 275 | emr_pyspark = EmrPySparkResource(init_context.resource_config) |
234 | 276 | try: |
235 | 277 | yield emr_pyspark |
|
0 commit comments