airflow.contrib.operators.databricks_operator
¶
Module Contents¶
-
airflow.contrib.operators.databricks_operator.
_deep_string_coerce
(content, json_path='json')[source]¶ -
Coerces content or all values of content if it is a dict to a string. The
-
function will throw if content contains non-string or non-numeric types.
The reason why we have this function is because the
self.json
field must be a dict with only string values. This is becauserender_template
will fail for numerical values.
-
airflow.contrib.operators.databricks_operator.
_handle_databricks_operator_execution
(operator, hook, log, context)[source]¶ -
Handles the Airflow + Databricks lifecycle logic for a Databricks operator
- Parameters
operator – Databricks operator being handled
context – Airflow context
-
class
airflow.contrib.operators.databricks_operator.
DatabricksSubmitRunOperator
(json=None, spark_jar_task=None, notebook_task=None, new_cluster=None, existing_cluster_id=None, libraries=None, run_name=None, timeout_seconds=None, databricks_conn_id='databricks_default', polling_period_seconds=30, databricks_retry_limit=3, databricks_retry_delay=1, do_xcom_push=False, **kwargs)[source]¶ Bases:
airflow.models.BaseOperator
Submits a Spark job run to Databricks using the api/2.0/jobs/runs/submit API endpoint.
There are two ways to instantiate this operator.
In the first way, you can take the JSON payload that you typically use to call the
api/2.0/jobs/runs/submit
endpoint and pass it directly to ourDatabricksSubmitRunOperator
through thejson
parameter. For examplejson = { 'new_cluster': { 'spark_version': '2.1.0-db3-scala2.11', 'num_workers': 2 }, 'notebook_task': { 'notebook_path': '/Users/airflow@example.com/PrepareData', }, } notebook_run = DatabricksSubmitRunOperator(task_id='notebook_run', json=json)
Another way to accomplish the same thing is to use the named parameters of the
DatabricksSubmitRunOperator
directly. Note that there is exactly one named parameter for each top level parameter in theruns/submit
endpoint. In this method, your code would look like this:new_cluster = { 'spark_version': '2.1.0-db3-scala2.11', 'num_workers': 2 } notebook_task = { 'notebook_path': '/Users/airflow@example.com/PrepareData', } notebook_run = DatabricksSubmitRunOperator( task_id='notebook_run', new_cluster=new_cluster, notebook_task=notebook_task)
In the case where both the json parameter AND the named parameters are provided, they will be merged together. If there are conflicts during the merge, the named parameters will take precedence and override the top level
json
keys.- Currently the named parameters that
DatabricksSubmitRunOperator
supports are spark_jar_task
notebook_task
new_cluster
existing_cluster_id
libraries
run_name
timeout_seconds
- Parameters
json (dict) –
A JSON object containing API parameters which will be passed directly to the
api/2.0/jobs/runs/submit
endpoint. The other named parameters (i.e.spark_jar_task
,notebook_task
..) to this operator will be merged with this json dictionary if they are provided. If there are conflicts during the merge, the named parameters will take precedence and override the top level json keys. (templated)See also
For more information about templating see Jinja Templating. https://docs.databricks.com/api/latest/jobs.html#runs-submit
spark_jar_task (dict) –
The main class and parameters for the JAR task. Note that the actual JAR is specified in the
libraries
. EITHERspark_jar_task
ORnotebook_task
should be specified. This field will be templated.notebook_task (dict) –
The notebook path and parameters for the notebook task. EITHER
spark_jar_task
ORnotebook_task
should be specified. This field will be templated.new_cluster (dict) –
Specs for a new cluster on which this task will be run. EITHER
new_cluster
ORexisting_cluster_id
should be specified. This field will be templated.existing_cluster_id (str) – ID for existing cluster on which to run this task. EITHER
new_cluster
ORexisting_cluster_id
should be specified. This field will be templated.libraries (list of dicts) –
Libraries which this run will use. This field will be templated.
run_name (str) – The run name used for this task. By default this will be set to the Airflow
task_id
. Thistask_id
is a required parameter of the superclassBaseOperator
. This field will be templated.timeout_seconds (int32) – The timeout for this run. By default a value of 0 is used which means to have no timeout. This field will be templated.
databricks_conn_id (str) – The name of the Airflow connection to use. By default and in the common case this will be
databricks_default
. To use token based authentication, provide the keytoken
in the extra field for the connection and create the keyhost
and leave thehost
field empty.polling_period_seconds (int) – Controls the rate which we poll for the result of this run. By default the operator will poll every 30 seconds.
databricks_retry_limit (int) – Amount of times retry if the Databricks backend is unreachable. Its value must be greater than or equal to 1.
databricks_retry_delay (float) – Number of seconds to wait between retries (it might be a floating point number).
do_xcom_push (bool) – Whether we should push run_id and run_page_url to xcom.
- Currently the named parameters that
-
class
airflow.contrib.operators.databricks_operator.
DatabricksRunNowOperator
(job_id, json=None, notebook_params=None, python_params=None, spark_submit_params=None, databricks_conn_id='databricks_default', polling_period_seconds=30, databricks_retry_limit=3, databricks_retry_delay=1, do_xcom_push=False, **kwargs)[source]¶ Bases:
airflow.models.BaseOperator
Runs an existing Spark job run to Databricks using the api/2.0/jobs/run-now API endpoint.
There are two ways to instantiate this operator.
In the first way, you can take the JSON payload that you typically use to call the
api/2.0/jobs/run-now
endpoint and pass it directly to ourDatabricksRunNowOperator
through thejson
parameter. For examplejson = { "job_id": 42, "notebook_params": { "dry-run": "true", "oldest-time-to-consider": "1457570074236" } } notebook_run = DatabricksRunNowOperator(task_id='notebook_run', json=json)
Another way to accomplish the same thing is to use the named parameters of the
DatabricksRunNowOperator
directly. Note that there is exactly one named parameter for each top level parameter in therun-now
endpoint. In this method, your code would look like this:job_id=42 notebook_params = { "dry-run": "true", "oldest-time-to-consider": "1457570074236" } python_params = ["douglas adams", "42"] spark_submit_params = ["--class", "org.apache.spark.examples.SparkPi"] notebook_run = DatabricksRunNowOperator( job_id=job_id, notebook_params=notebook_params, python_params=python_params, spark_submit_params=spark_submit_params )
In the case where both the json parameter AND the named parameters are provided, they will be merged together. If there are conflicts during the merge, the named parameters will take precedence and override the top level
json
keys.- Currently the named parameters that
DatabricksRunNowOperator
supports are job_id
json
notebook_params
python_params
spark_submit_params
- Parameters
job_id (str) –
the job_id of the existing Databricks job. This field will be templated.
json (dict) –
A JSON object containing API parameters which will be passed directly to the
api/2.0/jobs/run-now
endpoint. The other named parameters (i.e.notebook_params
,spark_submit_params
..) to this operator will be merged with this json dictionary if they are provided. If there are conflicts during the merge, the named parameters will take precedence and override the top level json keys. (templated)See also
For more information about templating see Jinja Templating. https://docs.databricks.com/api/latest/jobs.html#run-now
notebook_params (dict) –
A dict from keys to values for jobs with notebook task, e.g. “notebook_params”: {“name”: “john doe”, “age”: “35”}. The map is passed to the notebook and will be accessible through the dbutils.widgets.get function. See Widgets for more information. If not specified upon run-now, the triggered run will use the job’s base parameters. notebook_params cannot be specified in conjunction with jar_params. The json representation of this field (i.e. {“notebook_params”:{“name”:”john doe”,”age”:”35”}}) cannot exceed 10,000 bytes. This field will be templated.
A list of parameters for jobs with python tasks, e.g. “python_params”: [“john doe”, “35”]. The parameters will be passed to python file as command line parameters. If specified upon run-now, it would overwrite the parameters specified in job setting. The json representation of this field (i.e. {“python_params”:[“john doe”,”35”]}) cannot exceed 10,000 bytes. This field will be templated.
spark_submit_params (list[str]) –
A list of parameters for jobs with spark submit task, e.g. “spark_submit_params”: [“–class”, “org.apache.spark.examples.SparkPi”]. The parameters will be passed to spark-submit script as command line parameters. If specified upon run-now, it would overwrite the parameters specified in job setting. The json representation of this field cannot exceed 10,000 bytes. This field will be templated.
timeout_seconds (int32) – The timeout for this run. By default a value of 0 is used which means to have no timeout. This field will be templated.
databricks_conn_id (str) – The name of the Airflow connection to use. By default and in the common case this will be
databricks_default
. To use token based authentication, provide the keytoken
in the extra field for the connection and create the keyhost
and leave thehost
field empty.polling_period_seconds (int) – Controls the rate which we poll for the result of this run. By default the operator will poll every 30 seconds.
databricks_retry_limit (int) – Amount of times retry if the Databricks backend is unreachable. Its value must be greater than or equal to 1.
do_xcom_push (bool) – Whether we should push run_id and run_page_url to xcom.
- Currently the named parameters that