|
16 | 16 |
|
17 | 17 | from __future__ import absolute_import |
18 | 18 |
|
| 19 | +import collections |
19 | 20 | import uuid |
20 | 21 |
|
21 | 22 | from google.api.core import page_iterator |
@@ -492,25 +493,39 @@ def load_table_from_storage(self, job_id, destination, *source_uris): |
492 | 493 | """ |
493 | 494 | return LoadJob(job_id, destination, source_uris, client=self) |
494 | 495 |
|
495 | | - def copy_table(self, job_id, destination, *sources): |
496 | | - """Construct a job for copying one or more tables into another table. |
| 496 | + def copy_table(self, sources, destination, job_id=None, job_config=None): |
| 497 | + """Start a job for copying one or more tables into another table. |
497 | 498 |
|
498 | 499 | See |
499 | 500 | https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.copy |
500 | 501 |
|
501 | | - :type job_id: str |
502 | | - :param job_id: Name of the job. |
| 502 | + :type sources: One of: |
| 503 | + :class:`~google.cloud.bigquery.table.TableReference` |
| 504 | + sequence of |
| 505 | + :class:`~google.cloud.bigquery.table.TableReference` |
| 506 | + :param sources: Table or tables to be copied. |
503 | 507 |
|
504 | | - :type destination: :class:`google.cloud.bigquery.table.Table` |
| 508 | +
|
| 509 | + :type destination: :class:`google.cloud.bigquery.table.TableReference` |
505 | 510 | :param destination: Table into which data is to be copied. |
506 | 511 |
|
507 | | - :type sources: sequence of :class:`google.cloud.bigquery.table.Table` |
508 | | - :param sources: tables to be copied. |
| 512 | + :type job_id: str |
| 513 | + :param job_id: (Optional) The ID of the job. |
| 514 | +
|
| 515 | + :type job_config: :class:`google.cloud.bigquery.job.CopyJobConfig` |
| 516 | + :param job_config: (Optional) Extra configuration options for the job. |
509 | 517 |
|
510 | 518 | :rtype: :class:`google.cloud.bigquery.job.CopyJob` |
511 | 519 | :returns: a new ``CopyJob`` instance |
512 | 520 | """ |
513 | | - return CopyJob(job_id, destination, sources, client=self) |
| 521 | + job_id = _make_job_id(job_id) |
| 522 | + |
| 523 | + if not isinstance(sources, collections.Sequence): |
| 524 | + sources = [sources] |
| 525 | + job = CopyJob(job_id, sources, destination, client=self, |
| 526 | + job_config=job_config) |
| 527 | + job.begin() |
| 528 | + return job |
514 | 529 |
|
515 | 530 | def extract_table(self, source, *destination_uris, **kwargs): |
516 | 531 | """Start a job to extract a table into Cloud Storage files. |
@@ -541,9 +556,7 @@ def extract_table(self, source, *destination_uris, **kwargs): |
541 | 556 | :returns: a new ``ExtractJob`` instance |
542 | 557 | """ |
543 | 558 | job_config = kwargs.get('job_config') |
544 | | - job_id = kwargs.get('job_id') |
545 | | - if job_id is None: |
546 | | - job_id = str(uuid.uuid4()) |
| 559 | + job_id = _make_job_id(kwargs.get('job_id')) |
547 | 560 |
|
548 | 561 | job = ExtractJob( |
549 | 562 | job_id, source, list(destination_uris), client=self, |
@@ -667,3 +680,17 @@ def _item_to_table(iterator, resource): |
667 | 680 | :returns: The next table in the page. |
668 | 681 | """ |
669 | 682 | return Table.from_api_repr(resource, iterator.client) |
| 683 | + |
| 684 | + |
| 685 | +def _make_job_id(job_id): |
| 686 | + """Construct an ID for a new job. |
| 687 | +
|
| 688 | + :type job_id: str or ``NoneType`` |
| 689 | + :param job_id: the user-provided job ID |
| 690 | +
|
| 691 | + :rtype: str |
| 692 | + :returns: A job ID |
| 693 | + """ |
| 694 | + if job_id is None: |
| 695 | + return str(uuid.uuid4()) |
| 696 | + return job_id |
0 commit comments