Source code for airflow.providers.google.cloud.example_dags.example_automl_tables
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License."""Example Airflow DAG that uses Google AutoML services."""importosfromcopyimportdeepcopyfromdatetimeimportdatetimefromtypingimportDict,Listfromairflowimportmodelsfromairflow.providers.google.cloud.hooks.automlimportCloudAutoMLHookfromairflow.providers.google.cloud.operators.automlimport(AutoMLBatchPredictOperator,AutoMLCreateDatasetOperator,AutoMLDeleteDatasetOperator,AutoMLDeleteModelOperator,AutoMLDeployModelOperator,AutoMLGetModelOperator,AutoMLImportDataOperator,AutoMLListDatasetOperator,AutoMLPredictOperator,AutoMLTablesListColumnSpecsOperator,AutoMLTablesListTableSpecsOperator,AutoMLTablesUpdateDatasetOperator,AutoMLTrainModelOperator,)
[docs]defget_target_column_spec(columns_specs:List[Dict],column_name:str)->str:""" Using column name returns spec of the column. """forcolumnincolumns_specs:ifcolumn["display_name"]==column_name:returnextract_object_id(column)raiseException(f"Unknown target column: {column_name}")
# Example DAG to create dataset, train model_id and deploy it.withmodels.DAG("example_create_and_deploy",schedule_interval='@once',# Override to match your needsstart_date=START_DATE,catchup=False,user_defined_macros={"get_target_column_spec":get_target_column_spec,"target":TARGET,"extract_object_id":extract_object_id,},tags=['example'],)ascreate_deploy_dag:# [START howto_operator_automl_create_dataset]
)# [END howto_operator_get_model]# [START howto_operator_deploy_model]deploy_model_task=AutoMLDeployModelOperator(task_id="deploy_model_task",model_id=MODEL_ID,location=GCP_AUTOML_LOCATION,project_id=GCP_PROJECT_ID,)# [END howto_operator_deploy_model]withmodels.DAG("example_gcp_predict",schedule_interval='@once',# Override to match your needsstart_date=START_DATE,catchup=False,tags=["example"],)aspredict_dag:# [START howto_operator_prediction]
[docs]predict_task=AutoMLPredictOperator(task_id="predict_task",model_id=MODEL_ID,payload={},# Add your own payload, the used model_id must be deployedlocation=GCP_AUTOML_LOCATION,project_id=GCP_PROJECT_ID,
)# [END howto_operator_prediction]# [START howto_operator_batch_prediction]batch_predict_task=AutoMLBatchPredictOperator(task_id="batch_predict_task",model_id=MODEL_ID,input_config={},# Add your configoutput_config={},# Add your configlocation=GCP_AUTOML_LOCATION,project_id=GCP_PROJECT_ID,)# [END howto_operator_batch_prediction]