Source code for airflow.providers.google.cloud.operators.vertex_ai.dataset
## 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."""This module contains Google Vertex AI operators."""from__future__importannotationsfromtypingimportTYPE_CHECKING,Sequencefromgoogle.api_core.exceptionsimportNotFoundfromgoogle.api_core.gapic_v1.methodimportDEFAULT,_MethodDefaultfromgoogle.cloud.aiplatform_v1.typesimportDataset,ExportDataConfig,ImportDataConfigfromairflow.providers.google.cloud.hooks.vertex_ai.datasetimportDatasetHookfromairflow.providers.google.cloud.links.vertex_aiimportVertexAIDatasetLink,VertexAIDatasetListLinkfromairflow.providers.google.cloud.operators.cloud_baseimportGoogleCloudBaseOperatorifTYPE_CHECKING:fromgoogle.api_core.retryimportRetryfromgoogle.protobuf.field_mask_pb2importFieldMaskfromairflow.utils.contextimportContext
[docs]classCreateDatasetOperator(GoogleCloudBaseOperator):""" Creates a Dataset. :param project_id: Required. The ID of the Google Cloud project the cluster belongs to. :param region: Required. The Cloud Dataproc region in which to handle the request. :param dataset: Required. The Dataset to create. This corresponds to the ``dataset`` field on the ``request`` instance; if ``request`` is provided, this should not be set. :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. :param gcp_conn_id: The connection ID to use connecting to Google Cloud. :param impersonation_chain: Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account (templated). """
[docs]defexecute(self,context:Context):hook=DatasetHook(gcp_conn_id=self.gcp_conn_id,impersonation_chain=self.impersonation_chain,)self.log.info("Creating dataset")operation=hook.create_dataset(project_id=self.project_id,region=self.region,dataset=self.dataset,retry=self.retry,timeout=self.timeout,metadata=self.metadata,)result=hook.wait_for_operation(timeout=self.timeout,operation=operation)dataset=Dataset.to_dict(result)dataset_id=hook.extract_dataset_id(dataset)self.log.info("Dataset was created. Dataset id: %s",dataset_id)self.xcom_push(context,key="dataset_id",value=dataset_id)VertexAIDatasetLink.persist(context=context,task_instance=self,dataset_id=dataset_id)returndataset
[docs]classGetDatasetOperator(GoogleCloudBaseOperator):""" Get a Dataset. :param project_id: Required. The ID of the Google Cloud project the cluster belongs to. :param region: Required. The Cloud Dataproc region in which to handle the request. :param dataset_id: Required. The ID of the Dataset to get. :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. :param gcp_conn_id: The connection ID to use connecting to Google Cloud. :param impersonation_chain: Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account (templated). """
[docs]defexecute(self,context:Context):hook=DatasetHook(gcp_conn_id=self.gcp_conn_id,impersonation_chain=self.impersonation_chain,)try:self.log.info("Get dataset: %s",self.dataset_id)dataset_obj=hook.get_dataset(project_id=self.project_id,region=self.region,dataset=self.dataset_id,read_mask=self.read_mask,retry=self.retry,timeout=self.timeout,metadata=self.metadata,)VertexAIDatasetLink.persist(context=context,task_instance=self,dataset_id=self.dataset_id)self.log.info("Dataset was gotten.")returnDataset.to_dict(dataset_obj)exceptNotFound:self.log.info("The Dataset ID %s does not exist.",self.dataset_id)
[docs]classDeleteDatasetOperator(GoogleCloudBaseOperator):""" Deletes a Dataset. :param project_id: Required. The ID of the Google Cloud project the cluster belongs to. :param region: Required. The Cloud Dataproc region in which to handle the request. :param dataset_id: Required. The ID of the Dataset to delete. :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. :param gcp_conn_id: The connection ID to use connecting to Google Cloud. :param impersonation_chain: Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account (templated). """
[docs]defexecute(self,context:Context):hook=DatasetHook(gcp_conn_id=self.gcp_conn_id,impersonation_chain=self.impersonation_chain,)try:self.log.info("Deleting dataset: %s",self.dataset_id)operation=hook.delete_dataset(project_id=self.project_id,region=self.region,dataset=self.dataset_id,retry=self.retry,timeout=self.timeout,metadata=self.metadata,)hook.wait_for_operation(timeout=self.timeout,operation=operation)self.log.info("Dataset was deleted.")exceptNotFound:self.log.info("The Dataset ID %s does not exist.",self.dataset_id)
[docs]classExportDataOperator(GoogleCloudBaseOperator):""" Exports data from a Dataset. :param project_id: Required. The ID of the Google Cloud project the cluster belongs to. :param region: Required. The Cloud Dataproc region in which to handle the request. :param dataset_id: Required. The ID of the Dataset to delete. :param export_config: Required. The desired output location. :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. :param gcp_conn_id: The connection ID to use connecting to Google Cloud. :param impersonation_chain: Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account (templated). """
[docs]defexecute(self,context:Context):hook=DatasetHook(gcp_conn_id=self.gcp_conn_id,impersonation_chain=self.impersonation_chain,)self.log.info("Exporting data: %s",self.dataset_id)operation=hook.export_data(project_id=self.project_id,region=self.region,dataset=self.dataset_id,export_config=self.export_config,retry=self.retry,timeout=self.timeout,metadata=self.metadata,)hook.wait_for_operation(timeout=self.timeout,operation=operation)self.log.info("Export was done successfully")
[docs]classImportDataOperator(GoogleCloudBaseOperator):""" Imports data into a Dataset. :param project_id: Required. The ID of the Google Cloud project the cluster belongs to. :param region: Required. The Cloud Dataproc region in which to handle the request. :param dataset_id: Required. The ID of the Dataset to delete. :param import_configs: Required. The desired input locations. The contents of all input locations will be imported in one batch. :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. :param gcp_conn_id: The connection ID to use connecting to Google Cloud. :param impersonation_chain: Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account (templated). """
[docs]defexecute(self,context:Context):hook=DatasetHook(gcp_conn_id=self.gcp_conn_id,impersonation_chain=self.impersonation_chain,)self.log.info("Importing data: %s",self.dataset_id)operation=hook.import_data(project_id=self.project_id,region=self.region,dataset=self.dataset_id,import_configs=self.import_configs,retry=self.retry,timeout=self.timeout,metadata=self.metadata,)hook.wait_for_operation(timeout=self.timeout,operation=operation)self.log.info("Import was done successfully")
[docs]classListDatasetsOperator(GoogleCloudBaseOperator):""" Lists Datasets in a Location. :param project_id: Required. The ID of the Google Cloud project that the service belongs to. :param region: Required. The ID of the Google Cloud region that the service belongs to. :param filter: The standard list filter. :param page_size: The standard list page size. :param page_token: The standard list page token. :param read_mask: Mask specifying which fields to read. :param order_by: A comma-separated list of fields to order by, sorted in ascending order. Use "desc" after a field name for descending. :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. :param gcp_conn_id: The connection ID to use connecting to Google Cloud. :param impersonation_chain: Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account (templated). """
[docs]classUpdateDatasetOperator(GoogleCloudBaseOperator):""" Updates a Dataset. :param project_id: Required. The ID of the Google Cloud project that the service belongs to. :param region: Required. The ID of the Google Cloud region that the service belongs to. :param dataset_id: Required. The ID of the Dataset to update. :param dataset: Required. The Dataset which replaces the resource on the server. :param update_mask: Required. The update mask applies to the resource. :param retry: Designation of what errors, if any, should be retried. :param timeout: The timeout for this request. :param metadata: Strings which should be sent along with the request as metadata. :param gcp_conn_id: The connection ID to use connecting to Google Cloud. :param impersonation_chain: Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account (templated). """
[docs]defexecute(self,context:Context):hook=DatasetHook(gcp_conn_id=self.gcp_conn_id,impersonation_chain=self.impersonation_chain,)self.log.info("Updating dataset: %s",self.dataset_id)result=hook.update_dataset(project_id=self.project_id,region=self.region,dataset_id=self.dataset_id,dataset=self.dataset,update_mask=self.update_mask,retry=self.retry,timeout=self.timeout,metadata=self.metadata,)self.log.info("Dataset was updated")returnDataset.to_dict(result)