Source code for airflow.providers.google.cloud.operators.datastore
## 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 Datastore operators."""fromtypingimportTYPE_CHECKING,Any,Dict,List,Optional,Sequence,Unionfromairflow.exceptionsimportAirflowExceptionfromairflow.modelsimportBaseOperatorfromairflow.providers.google.cloud.hooks.datastoreimportDatastoreHookfromairflow.providers.google.cloud.hooks.gcsimportGCSHookfromairflow.providers.google.cloud.links.datastoreimport(CloudDatastoreEntitiesLink,CloudDatastoreImportExportLink,)fromairflow.providers.google.common.links.storageimportStorageLinkifTYPE_CHECKING:fromairflow.utils.contextimportContext
[docs]classCloudDatastoreExportEntitiesOperator(BaseOperator):""" Export entities from Google Cloud Datastore to Cloud Storage .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudDatastoreExportEntitiesOperator` .. seealso:: https://cloud.google.com/datastore/docs/export-import-entities :param bucket: name of the cloud storage bucket to backup data :param namespace: optional namespace path in the specified Cloud Storage bucket to backup data. If this namespace does not exist in GCS, it will be created. :param datastore_conn_id: the name of the Datastore connection id to use :param cloud_storage_conn_id: the name of the cloud storage connection id to force-write backup :param delegate_to: The account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled. :param entity_filter: description of what data from the project is included in the export, refer to https://cloud.google.com/datastore/docs/reference/rest/Shared.Types/EntityFilter :param labels: client-assigned labels for cloud storage :param polling_interval_in_seconds: number of seconds to wait before polling for execution status again :param overwrite_existing: if the storage bucket + namespace is not empty, it will be emptied prior to exports. This enables overwriting existing backups. :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')->dict:self.log.info('Exporting data to Cloud Storage bucket %s',self.bucket)ifself.overwrite_existingandself.namespace:gcs_hook=GCSHook(self.cloud_storage_conn_id,impersonation_chain=self.impersonation_chain)objects=gcs_hook.list(self.bucket,prefix=self.namespace)forobjinobjects:gcs_hook.delete(self.bucket,obj)ds_hook=DatastoreHook(gcp_conn_id=self.datastore_conn_id,delegate_to=self.delegate_to,impersonation_chain=self.impersonation_chain,)result=ds_hook.export_to_storage_bucket(bucket=self.bucket,namespace=self.namespace,entity_filter=self.entity_filter,labels=self.labels,project_id=self.project_id,)operation_name=result['name']result=ds_hook.poll_operation_until_done(operation_name,self.polling_interval_in_seconds)state=result['metadata']['common']['state']ifstate!='SUCCESSFUL':raiseAirflowException(f'Operation failed: result={result}')StorageLink.persist(context=context,task_instance=self,uri=f"{self.bucket}/{result['response']['outputUrl'].split('/')[3]}",project_id=self.project_idords_hook.project_id,)returnresult
[docs]classCloudDatastoreImportEntitiesOperator(BaseOperator):""" Import entities from Cloud Storage to Google Cloud Datastore .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudDatastoreImportEntitiesOperator` .. seealso:: https://cloud.google.com/datastore/docs/export-import-entities :param bucket: container in Cloud Storage to store data :param file: path of the backup metadata file in the specified Cloud Storage bucket. It should have the extension .overall_export_metadata :param namespace: optional namespace of the backup metadata file in the specified Cloud Storage bucket. :param entity_filter: description of what data from the project is included in the export, refer to https://cloud.google.com/datastore/docs/reference/rest/Shared.Types/EntityFilter :param labels: client-assigned labels for cloud storage :param datastore_conn_id: the name of the connection id to use :param delegate_to: The account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled. :param polling_interval_in_seconds: number of seconds to wait before polling for execution status again :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'):self.log.info('Importing data from Cloud Storage bucket %s',self.bucket)ds_hook=DatastoreHook(self.datastore_conn_id,self.delegate_to,impersonation_chain=self.impersonation_chain,)result=ds_hook.import_from_storage_bucket(bucket=self.bucket,file=self.file,namespace=self.namespace,entity_filter=self.entity_filter,labels=self.labels,project_id=self.project_id,)operation_name=result['name']result=ds_hook.poll_operation_until_done(operation_name,self.polling_interval_in_seconds)state=result['metadata']['common']['state']ifstate!='SUCCESSFUL':raiseAirflowException(f'Operation failed: result={result}')CloudDatastoreImportExportLink.persist(context=context,task_instance=self)returnresult
[docs]classCloudDatastoreAllocateIdsOperator(BaseOperator):""" Allocate IDs for incomplete keys. Return list of keys. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudDatastoreAllocateIdsOperator` .. seealso:: https://cloud.google.com/datastore/docs/reference/rest/v1/projects/allocateIds :param partial_keys: a list of partial keys. :param project_id: Google Cloud project ID against which to make the request. :param delegate_to: The account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled. :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]classCloudDatastoreBeginTransactionOperator(BaseOperator):""" Begins a new transaction. Returns a transaction handle. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudDatastoreBeginTransactionOperator` .. seealso:: https://cloud.google.com/datastore/docs/reference/rest/v1/projects/beginTransaction :param transaction_options: Options for a new transaction. :param project_id: Google Cloud project ID against which to make the request. :param delegate_to: The account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled. :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]classCloudDatastoreCommitOperator(BaseOperator):""" Commit a transaction, optionally creating, deleting or modifying some entities. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudDatastoreCommitOperator` .. seealso:: https://cloud.google.com/datastore/docs/reference/rest/v1/projects/commit :param body: the body of the commit request. :param project_id: Google Cloud project ID against which to make the request. :param delegate_to: The account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled. :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]classCloudDatastoreRollbackOperator(BaseOperator):""" Roll back a transaction. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudDatastoreRollbackOperator` .. seealso:: https://cloud.google.com/datastore/docs/reference/rest/v1/projects/rollback :param transaction: the transaction to roll back. :param project_id: Google Cloud project ID against which to make the request. :param delegate_to: The account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled. :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]classCloudDatastoreRunQueryOperator(BaseOperator):""" Run a query for entities. Returns the batch of query results. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudDatastoreRunQueryOperator` .. seealso:: https://cloud.google.com/datastore/docs/reference/rest/v1/projects/runQuery :param body: the body of the query request. :param project_id: Google Cloud project ID against which to make the request. :param delegate_to: The account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled. :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]classCloudDatastoreGetOperationOperator(BaseOperator):""" Gets the latest state of a long-running operation. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudDatastoreGetOperationOperator` .. seealso:: https://cloud.google.com/datastore/docs/reference/data/rest/v1/projects.operations/get :param name: the name of the operation resource. :param delegate_to: The account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled. :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]classCloudDatastoreDeleteOperationOperator(BaseOperator):""" Deletes the long-running operation. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudDatastoreDeleteOperationOperator` .. seealso:: https://cloud.google.com/datastore/docs/reference/data/rest/v1/projects.operations/delete :param name: the name of the operation resource. :param delegate_to: The account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled. :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). """