@@ -126,7 +126,7 @@ class Compression(_EnumApiResourceProperty):
126
126
NONE = 'NONE'
127
127
128
128
129
- class CreateDisposition (_EnumProperty ):
129
+ class CreateDisposition (_EnumApiResourceProperty ):
130
130
"""Pseudo-enum for ``create_disposition`` properties."""
131
131
CREATE_IF_NEEDED = 'CREATE_IF_NEEDED'
132
132
CREATE_NEVER = 'CREATE_NEVER'
@@ -159,7 +159,7 @@ class SourceFormat(_EnumProperty):
159
159
AVRO = 'AVRO'
160
160
161
161
162
- class WriteDisposition (_EnumProperty ):
162
+ class WriteDisposition (_EnumApiResourceProperty ):
163
163
"""Pseudo-enum for ``write_disposition`` properties."""
164
164
WRITE_APPEND = 'WRITE_APPEND'
165
165
WRITE_TRUNCATE = 'WRITE_TRUNCATE'
@@ -688,7 +688,8 @@ def output_rows(self):
688
688
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.load.autodetect
689
689
"""
690
690
691
- create_disposition = CreateDisposition ('create_disposition' )
691
+ create_disposition = CreateDisposition ('create_disposition' ,
692
+ 'createDisposition' )
692
693
"""See
693
694
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.load.createDisposition
694
695
"""
@@ -733,7 +734,8 @@ def output_rows(self):
733
734
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.load.sourceFormat
734
735
"""
735
736
736
- write_disposition = WriteDisposition ('write_disposition' )
737
+ write_disposition = WriteDisposition ('write_disposition' ,
738
+ 'writeDisposition' )
737
739
"""See
738
740
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.load.writeDisposition
739
741
"""
@@ -853,13 +855,51 @@ def from_api_repr(cls, resource, client):
853
855
return job
854
856
855
857
856
- class _CopyConfiguration (object ):
857
- """User-settable configuration options for copy jobs.
858
+ class CopyJobConfig (object ):
859
+ """Configuration options for copy jobs.
858
860
859
- Values which are ``None`` -> server defaults.
861
+ All properties in this class are optional. Values which are ``None`` ->
862
+ server defaults.
860
863
"""
861
- _create_disposition = None
862
- _write_disposition = None
864
+
865
+ def __init__ (self ):
866
+ self ._properties = {}
867
+
868
+ create_disposition = CreateDisposition ('create_disposition' ,
869
+ 'createDisposition' )
870
+ """See
871
+ https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.copy.createDisposition
872
+ """
873
+
874
+ write_disposition = WriteDisposition ('write_disposition' ,
875
+ 'writeDisposition' )
876
+ """See
877
+ https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.copy.writeDisposition
878
+ """
879
+
880
+ def to_api_repr (self ):
881
+ """Build an API representation of the copy job config.
882
+
883
+ :rtype: dict
884
+ :returns: A dictionary in the format used by the BigQuery API.
885
+ """
886
+ return copy .deepcopy (self ._properties )
887
+
888
+ @classmethod
889
+ def from_api_repr (cls , resource ):
890
+ """Factory: construct a job configuration given its API representation
891
+
892
+ :type resource: dict
893
+ :param resource:
894
+ An extract job configuration in the same representation as is
895
+ returned from the API.
896
+
897
+ :rtype: :class:`google.cloud.bigquery.job.ExtractJobConfig`
898
+ :returns: Configuration parsed from ``resource``.
899
+ """
900
+ config = cls ()
901
+ config ._properties = copy .deepcopy (resource )
902
+ return config
863
903
864
904
865
905
class CopyJob (_AsyncJob ):
@@ -868,41 +908,45 @@ class CopyJob(_AsyncJob):
868
908
:type job_id: str
869
909
:param job_id: the job's ID, within the project belonging to ``client``.
870
910
871
- :type destination: :class:`google.cloud.bigquery.table.Table`
872
- :param destination: Table into which data is to be loaded.
873
-
874
- :type sources: list of :class:`google.cloud.bigquery.table.Table`
911
+ :type sources: list of :class:`google.cloud.bigquery.table.TableReference`
875
912
:param sources: Table into which data is to be loaded.
876
913
914
+ :type destination: :class:`google.cloud.bigquery.table.TableReference`
915
+ :param destination: Table into which data is to be loaded.
916
+
877
917
:type client: :class:`google.cloud.bigquery.client.Client`
878
918
:param client: A client which holds credentials and project configuration
879
919
for the dataset (which requires a project).
880
- """
881
920
921
+ :type job_config: :class:`~google.cloud.bigquery.job.CopyJobConfig`
922
+ :param job_config:
923
+ (Optional) Extra configuration options for the copy job.
924
+ """
882
925
_JOB_TYPE = 'copy'
883
926
884
- def __init__ (self , job_id , destination , sources , client ):
927
+ def __init__ (self , job_id , sources , destination , client , job_config = None ):
885
928
super (CopyJob , self ).__init__ (job_id , client )
929
+
930
+ if job_config is None :
931
+ job_config = CopyJobConfig ()
932
+
886
933
self .destination = destination
887
934
self .sources = sources
888
- self ._configuration = _CopyConfiguration ()
889
-
890
- create_disposition = CreateDisposition ('create_disposition' )
891
- """See
892
- https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.copy.createDisposition
893
- """
935
+ self ._configuration = job_config
894
936
895
- write_disposition = WriteDisposition ('write_disposition' )
896
- """See
897
- https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.copy.writeDisposition
898
- """
937
+ @property
938
+ def create_disposition (self ):
939
+ """See
940
+ :class:`~google.cloud.bigquery.job.CopyJobConfig.create_disposition`.
941
+ """
942
+ return self ._configuration .create_disposition
899
943
900
- def _populate_config_resource ( self , configuration ):
901
- """Helper for _build_resource: copy config properties to resource"""
902
- if self . create_disposition is not None :
903
- configuration [ 'createDisposition' ] = self . create_disposition
904
- if self . write_disposition is not None :
905
- configuration [ 'writeDisposition' ] = self .write_disposition
944
+ @ property
945
+ def write_disposition ( self ):
946
+ """See
947
+ :class:`~google.cloud.bigquery.job.CopyJobConfig.write_disposition`.
948
+ """
949
+ return self . _configuration .write_disposition
906
950
907
951
def _build_resource (self ):
908
952
"""Generate a resource for :meth:`begin`."""
@@ -913,31 +957,27 @@ def _build_resource(self):
913
957
'tableId' : table .table_id ,
914
958
} for table in self .sources ]
915
959
916
- resource = {
960
+ configuration = self ._configuration .to_api_repr ()
961
+ configuration ['sourceTables' ] = source_refs
962
+ configuration ['destinationTable' ] = {
963
+ 'projectId' : self .destination .project ,
964
+ 'datasetId' : self .destination .dataset_id ,
965
+ 'tableId' : self .destination .table_id ,
966
+ }
967
+
968
+ return {
917
969
'jobReference' : {
918
970
'projectId' : self .project ,
919
971
'jobId' : self .job_id ,
920
972
},
921
973
'configuration' : {
922
- self ._JOB_TYPE : {
923
- 'sourceTables' : source_refs ,
924
- 'destinationTable' : {
925
- 'projectId' : self .destination .project ,
926
- 'datasetId' : self .destination .dataset_id ,
927
- 'tableId' : self .destination .table_id ,
928
- },
929
- },
974
+ self ._JOB_TYPE : configuration ,
930
975
},
931
976
}
932
- configuration = resource ['configuration' ][self ._JOB_TYPE ]
933
- self ._populate_config_resource (configuration )
934
-
935
- return resource
936
977
937
978
def _copy_configuration_properties (self , configuration ):
938
979
"""Helper: assign subclass configuration properties in cleaned."""
939
- self .create_disposition = configuration .get ('createDisposition' )
940
- self .write_disposition = configuration .get ('writeDisposition' )
980
+ self ._configuration ._properties = copy .deepcopy (configuration )
941
981
942
982
@classmethod
943
983
def from_api_repr (cls , resource , client ):
@@ -958,27 +998,23 @@ def from_api_repr(cls, resource, client):
958
998
:rtype: :class:`google.cloud.bigquery.job.CopyJob`
959
999
:returns: Job parsed from ``resource``.
960
1000
"""
961
- job_id , config = cls ._get_resource_config (resource )
962
- dest_config = config ['destinationTable' ]
963
- ds_ref = DatasetReference (dest_config ['projectId' ],
964
- dest_config ['datasetId' ],)
965
- dataset = Dataset (ds_ref )
966
- table_ref = TableReference (dataset , dest_config ['tableId' ])
967
- destination = Table (table_ref , client = client )
1001
+ job_id , config_resource = cls ._get_resource_config (resource )
1002
+ config = CopyJobConfig .from_api_repr (config_resource )
1003
+ destination = TableReference .from_api_repr (
1004
+ config_resource ['destinationTable' ])
968
1005
sources = []
969
- source_configs = config .get ('sourceTables' )
1006
+ source_configs = config_resource .get ('sourceTables' )
970
1007
if source_configs is None :
971
- single = config .get ('sourceTable' )
1008
+ single = config_resource .get ('sourceTable' )
972
1009
if single is None :
973
1010
raise KeyError (
974
1011
"Resource missing 'sourceTables' / 'sourceTable'" )
975
1012
source_configs = [single ]
976
1013
for source_config in source_configs :
977
- ds_ref = DatasetReference (source_config ['projectId' ],
978
- source_config ['datasetId' ])
979
- table_ref = ds_ref .table (source_config ['tableId' ])
980
- sources .append (Table (table_ref , client = client ))
981
- job = cls (job_id , destination , sources , client = client )
1014
+ table_ref = TableReference .from_api_repr (source_config )
1015
+ sources .append (table_ref )
1016
+ job = cls (
1017
+ job_id , sources , destination , client = client , job_config = config )
982
1018
job ._set_properties (resource )
983
1019
return job
984
1020
@@ -1017,7 +1053,7 @@ def __init__(self):
1017
1053
"""
1018
1054
1019
1055
def to_api_repr (self ):
1020
- """Build an API representation of the extact job config.
1056
+ """Build an API representation of the extract job config.
1021
1057
1022
1058
:rtype: dict
1023
1059
:returns: A dictionary in the format used by the BigQuery API.
@@ -1243,7 +1279,8 @@ def __init__(self, job_id, query, client,
1243
1279
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.query.allowLargeResults
1244
1280
"""
1245
1281
1246
- create_disposition = CreateDisposition ('create_disposition' )
1282
+ create_disposition = CreateDisposition ('create_disposition' ,
1283
+ 'createDisposition' )
1247
1284
"""See
1248
1285
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.query.createDisposition
1249
1286
"""
@@ -1289,7 +1326,8 @@ def __init__(self, job_id, query, client,
1289
1326
reference/rest/v2/jobs#configuration.dryRun
1290
1327
"""
1291
1328
1292
- write_disposition = WriteDisposition ('write_disposition' )
1329
+ write_disposition = WriteDisposition ('write_disposition' ,
1330
+ 'writeDisposition' )
1293
1331
"""See
1294
1332
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.query.writeDisposition
1295
1333
"""
0 commit comments