使用 Apache Airflow 使用 PySpark 代码执行 Databricks Notebook

Execute a Databricks Notebook with PySpark code using Apache Airflow

我正在使用 Airflow、Databricks 和 PySpark。我想知道当我想通过 Airflow 执行 Databricks Notebook 时是否可以添加更多参数。

我在 Python 中的下一个代码名为 MyETL:

def main(**kwargs):
      spark.sql("CREATE TABLE {0} {1}".format(table, columns))
      print("Running my ETL!")

    if __name__== "__main__":
      main(arg1, arg2)

我想定义其他任务参数 运行 一个具有更多参数的 Databricks notebook,我想添加方法的名称,以及这些方法的参数。例如,当我想在 Airflow 的 DAG 中注册任务时:

   notebook_task_params = {
        'new_cluster': new_cluster,
        'notebook_task': {
            'notebook_path': '/Users/airflow@example.com/MyETL',
            'method_name': 'main',
            'params':'[{'table':'A'},{'columns':['a', 'b']}]'
        },
    }

我不知道这是否可能,因为我没有找到类似的例子。

# Example of using the JSON parameter to initialize the operator.
notebook_task = DatabricksSubmitRunOperator(
    task_id='notebook_task',
    dag=dag,
    json=notebook_task_params)

换句话说,我想使用 Airflow 执行带有参数的笔记本。我的问题是我该怎么做?

您也可以将 method_name 添加为 params,然后在笔记本上解析您的逻辑。

但是,这里更常见的模式是确保该方法已安装在您的集群上。

params = '[{'table':'A'},{'columns':['a', 'b']}]'

然后在数据块上的笔记本中:

table = getArgument("table", "DefaultValue")
columns = getArgument("columns", "DefaultValue")

result = method(table, columns)

如果您可以在您的笔记本作业中看到您的参数(上图),您也会知道这些参数是否可以通过 getArgument() 访问 运行。