DataflowCreateJavaJobOperator

Google

Start a Java Cloud Dataflow batch job. The parameters of the operation will be passed to the job.

View on GitHub

Last Updated: Feb. 25, 2023

Access Instructions

Install the Google provider package into your Airflow environment.

Import the module into your DAG file and instantiate it with your desired params.

Parameters

jarRequiredThe reference to a self executing Dataflow jar (templated).
job_nameThe ‘jobName’ to use when executing the Dataflow job (templated). This ends up being set in the pipeline options, so any entry with key 'jobName' in options will be overwritten.
dataflow_default_optionsMap of default job options.
optionsMap of job specific options.The key must be a dictionary. The value can contain different types: If the value is None, the single option - --key (without value) will be added. If the value is False, this option will be skipped If the value is True, the single option - --key (without value) will be added. If the value is list, the many options will be added for each key. If the value is ['A', 'B'] and the key is key then the --key=A --key=B options will be left Other value types will be replaced with the Python textual representation. When defining labels (labels option), you can also provide a dictionary.
project_idOptional, the Google Cloud project ID in which to start a job. If set to None or missing, the default project_id from the Google Cloud connection is used.
locationJob location.
gcp_conn_idThe connection ID to use connecting to Google Cloud.
delegate_toThe account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled.
poll_sleepThe time in seconds to sleep between polling Google Cloud Platform for the dataflow job status while the job is in the JOB_STATE_RUNNING state.
job_classThe name of the dataflow job class to be executed, it is often not the main class configured in the dataflow jar file.
multiple_jobsIf pipeline creates multiple jobs then monitor all jobs
check_if_runningbefore running job, validate that a previous run is not in process if job is running finish with nothing, WaitForRun= wait until job finished and the run job) jar, options, and job_name are templated so you can use variables in them.
cancel_timeoutHow long (in seconds) operator should wait for the pipeline to be successfully cancelled when task is being killed.
wait_until_finished(Optional) If True, wait for the end of pipeline execution before exiting. If False, only submits job. If None, default behavior. The default behavior depends on the type of pipeline: for the streaming pipeline, wait for jobs to start, for the batch pipeline, wait for the jobs to complete. Warning You cannot call PipelineResult.wait_until_finish method in your pipeline code for the operator to work properly. i. e. you must use asynchronous execution. Otherwise, your pipeline will always wait until finished. For more information, look at: Asynchronous execution The process of starting the Dataflow job in Airflow consists of two steps: running a subprocess and reading the stderr/stderr log for the job id. loop waiting for the end of the job ID from the previous step. This loop checks the status of the job. Step two is started just after step one has finished, so if you have wait_until_finished in your pipeline code, step two will not start until the process stops. When this process stops, steps two will run, but it will only execute one iteration as the job will be in a terminal state. If you in your pipeline do not call the wait_for_pipeline method but pass wait_until_finish=True to the operator, the second loop will wait for the job’s terminal state. If you in your pipeline do not call the wait_for_pipeline method, and pass wait_until_finish=False to the operator, the second loop will check once is job not in terminal state and exit the loop.

Documentation

Start a Java Cloud Dataflow batch job. The parameters of the operation will be passed to the job.

This class is deprecated. Please use providers.apache.beam.operators.beam.BeamRunJavaPipelineOperator.

Example:

default_args = {
"owner": "airflow",
"depends_on_past": False,
"start_date": (2016, 8, 1),
"email": ["alex@vanboxel.be"],
"email_on_failure": False,
"email_on_retry": False,
"retries": 1,
"retry_delay": timedelta(minutes=30),
"dataflow_default_options": {
"project": "my-gcp-project",
"zone": "us-central1-f",
"stagingLocation": "gs://bucket/tmp/dataflow/staging/",
},
}
dag = DAG("test-dag", default_args=default_args)
task = DataflowCreateJavaJobOperator(
gcp_conn_id="gcp_default",
task_id="normalize-cal",
jar="{{var.value.gcp_dataflow_base}}pipeline-ingress-cal-normalize-1.0.jar",
options={
"autoscalingAlgorithm": "BASIC",
"maxNumWorkers": "50",
"start": "{{ds}}",
"partitionType": "DAY",
},
dag=dag,
)

See also

For more detail on job submission have a look at the reference: https://cloud.google.com/dataflow/pipelines/specifying-exec-params

See also

For more information on how to use this operator, take a look at the guide: Java SDK pipelines

Note that both dataflow_default_options and options will be merged to specify pipeline execution parameter, and dataflow_default_options is expected to save high-level options, for instances, project and zone information, which apply to all dataflow operators in the DAG.

It’s a good practice to define dataflow_* parameters in the default_args of the dag like the project, zone and staging location.

default_args = {
"dataflow_default_options": {
"zone": "europe-west1-d",
"stagingLocation": "gs://my-staging-bucket/staging/",
}
}

You need to pass the path to your dataflow as a file reference with the jar parameter, the jar needs to be a self executing jar (see documentation here: https://beam.apache.org/documentation/runners/dataflow/#self-executing-jar). Use options to pass on options to your job.

t1 = DataflowCreateJavaJobOperator(
task_id="dataflow_example",
jar="{{var.value.gcp_dataflow_base}}pipeline/build/libs/pipeline-example-1.0.jar",
options={
"autoscalingAlgorithm": "BASIC",
"maxNumWorkers": "50",
"start": "{{ds}}",
"partitionType": "DAY",
"labels": {"foo": "bar"},
},
gcp_conn_id="airflow-conn-id",
dag=my_dag,
)

Was this page helpful?