Google Cloud Composer and Google Cloud SQL

后端 未结 6 1322
情深已故
情深已故 2020-12-19 03:36

What ways do we have available to connect to a Google Cloud SQL (MySQL) instance from the newly introduced Google Cloud Composer? The intention is to get data from a Cloud S

6条回答
  •  忘掉有多难
    2020-12-19 03:45

    Now we can connect to Cloud SQL without creating a cloud proxy ourselves. The operator will create it automatically. The code look like this:

    from airflow.models import DAG
    from airflow.contrib.operators.gcp_sql_operator import CloudSqlInstanceExportOperator
    
    export_body = {
        'exportContext': {
            'fileType': 'CSV',
            'uri': EXPORT_URI,
            'databases': [DB_NAME],
            'csvExportOptions': {
                'selectQuery': SQL
            }
        }
    }
    
    default_dag_args = {}
    
    with DAG(
            'postgres_test',
            schedule_interval='@once',
            default_args=default_dag_args) as dag:
    
        sql_export_task = CloudSqlInstanceExportOperator(
            project_id=GCP_PROJECT_ID,
            body=export_body,
            instance=INSTANCE_NAME,
            task_id='sql_export_task'
        )
    

提交回复
热议问题