As of January 1, 2020 this library no longer supports Python 2 on the latest released version. Library versions released prior to that date will continue to be available. For more information please visit Python 2 support on Google Cloud.

Source code for

# Copyright 2015 Google LLC
# Licensed 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
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

"""Classes for copy jobs."""

from typing import Optional

from import EncryptionConfiguration
from import _helpers
from import TableReference

from import _AsyncJob
from import _JobConfig
from import _JobReference

[docs]class OperationType: """Different operation types supported in table copy job. """ OPERATION_TYPE_UNSPECIFIED = "OPERATION_TYPE_UNSPECIFIED" """Unspecified operation type.""" COPY = "COPY" """The source and destination table have the same table type.""" SNAPSHOT = "SNAPSHOT" """The source table type is TABLE and the destination table type is SNAPSHOT.""" RESTORE = "RESTORE" """The source table type is SNAPSHOT and the destination table type is TABLE."""
[docs]class CopyJobConfig(_JobConfig): """Configuration options for copy jobs. All properties in this class are optional. Values which are :data:`None` -> server defaults. Set properties on the constructed configuration by using the property name as the name of a keyword argument. """
[docs] def __init__(self, **kwargs): super(CopyJobConfig, self).__init__("copy", **kwargs)
@property def create_disposition(self): """ Specifies behavior for creating tables. See """ return self._get_sub_prop("createDisposition") @create_disposition.setter def create_disposition(self, value): self._set_sub_prop("createDisposition", value) @property def write_disposition(self): """ Action that occurs if the destination table already exists. See """ return self._get_sub_prop("writeDisposition") @write_disposition.setter def write_disposition(self, value): self._set_sub_prop("writeDisposition", value) @property def destination_encryption_configuration(self): """ Custom encryption configuration for the destination table. Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None` if using default encryption. See """ prop = self._get_sub_prop("destinationEncryptionConfiguration") if prop is not None: prop = EncryptionConfiguration.from_api_repr(prop) return prop @destination_encryption_configuration.setter def destination_encryption_configuration(self, value): api_repr = value if value is not None: api_repr = value.to_api_repr() self._set_sub_prop("destinationEncryptionConfiguration", api_repr) @property def operation_type(self) -> str: """The operation to perform with this copy job. See """ return self._get_sub_prop( "operationType", OperationType.OPERATION_TYPE_UNSPECIFIED ) @operation_type.setter def operation_type(self, value: Optional[str]): if value is None: value = OperationType.OPERATION_TYPE_UNSPECIFIED self._set_sub_prop("operationType", value)
[docs]class CopyJob(_AsyncJob): """Asynchronous job: copy data into a table from other tables. Args: job_id (str): the job's ID, within the project belonging to ``client``. sources (List[]): Table from which data is to be loaded. destination ( Table into which data is to be loaded. client ( A client which holds credentials and project configuration for the dataset (which requires a project). job_config (Optional[]): Extra configuration options for the copy job. """ _JOB_TYPE = "copy"
[docs] def __init__(self, job_id, sources, destination, client, job_config=None): super(CopyJob, self).__init__(job_id, client) if not job_config: job_config = CopyJobConfig() self._configuration = job_config self._properties["configuration"] = job_config._properties if destination: _helpers._set_sub_prop( self._properties, ["configuration", "copy", "destinationTable"], destination.to_api_repr(), ) if sources: source_resources = [source.to_api_repr() for source in sources] _helpers._set_sub_prop( self._properties, ["configuration", "copy", "sourceTables"], source_resources, )
@property def destination(self): """ Table into which data is to be loaded. """ return TableReference.from_api_repr( _helpers._get_sub_prop( self._properties, ["configuration", "copy", "destinationTable"] ) ) @property def sources(self): """List[]): Table(s) from which data is to be loaded. """ source_configs = _helpers._get_sub_prop( self._properties, ["configuration", "copy", "sourceTables"] ) if source_configs is None: single = _helpers._get_sub_prop( self._properties, ["configuration", "copy", "sourceTable"] ) if single is None: raise KeyError("Resource missing 'sourceTables' / 'sourceTable'") source_configs = [single] sources = [] for source_config in source_configs: table_ref = TableReference.from_api_repr(source_config) sources.append(table_ref) return sources @property def create_disposition(self): """See :attr:``. """ return self._configuration.create_disposition @property def write_disposition(self): """See :attr:``. """ return self._configuration.write_disposition @property def destination_encryption_configuration(self): """ Custom encryption configuration for the destination table. Custom encryption configuration (e.g., Cloud KMS keys) or :data:`None` if using default encryption. See :attr:``. """ return self._configuration.destination_encryption_configuration
[docs] def to_api_repr(self): """Generate a resource for :meth:`_begin`.""" # Exclude statistics, if set. return { "jobReference": self._properties["jobReference"], "configuration": self._properties["configuration"], }
[docs] @classmethod def from_api_repr(cls, resource, client): """Factory: construct a job given its API representation .. note:: This method assumes that the project found in the resource matches the client's project. Args: resource (Dict): dataset job representation returned from the API client ( Client which holds credentials and project configuration for the dataset. Returns: Job parsed from ``resource``. """ cls._check_resource_config(resource) job_ref = _JobReference._from_api_repr(resource["jobReference"]) job = cls(job_ref, None, None, client=client) job._set_properties(resource) return job