|
46 | 46 | from google.cloud.bigquery.dataset import DatasetListItem |
47 | 47 | from google.cloud.bigquery.dataset import DatasetReference |
48 | 48 | from google.cloud.bigquery import job |
| 49 | +from google.cloud.bigquery.model import Model |
| 50 | +from google.cloud.bigquery.model import ModelReference |
49 | 51 | from google.cloud.bigquery.query import _QueryResults |
50 | 52 | from google.cloud.bigquery.retry import DEFAULT_RETRY |
51 | 53 | from google.cloud.bigquery.table import _table_arg_to_table |
@@ -428,6 +430,33 @@ def get_dataset(self, dataset_ref, retry=DEFAULT_RETRY): |
428 | 430 | api_response = self._call_api(retry, method="GET", path=dataset_ref.path) |
429 | 431 | return Dataset.from_api_repr(api_response) |
430 | 432 |
|
| 433 | + def get_model(self, model_ref, retry=DEFAULT_RETRY): |
| 434 | + """[Beta] Fetch the model referenced by ``model_ref``. |
| 435 | +
|
| 436 | + Args: |
| 437 | + model_ref (Union[ \ |
| 438 | + :class:`~google.cloud.bigquery.model.ModelReference`, \ |
| 439 | + str, \ |
| 440 | + ]): |
| 441 | + A reference to the model to fetch from the BigQuery API. |
| 442 | + If a string is passed in, this method attempts to create a |
| 443 | + model reference from a string using |
| 444 | + :func:`google.cloud.bigquery.model.ModelReference.from_string`. |
| 445 | + retry (:class:`google.api_core.retry.Retry`): |
| 446 | + (Optional) How to retry the RPC. |
| 447 | +
|
| 448 | + Returns: |
| 449 | + google.cloud.bigquery.model.Model: |
| 450 | + A ``Model`` instance. |
| 451 | + """ |
| 452 | + if isinstance(model_ref, str): |
| 453 | + model_ref = ModelReference.from_string( |
| 454 | + model_ref, default_project=self.project |
| 455 | + ) |
| 456 | + |
| 457 | + api_response = self._call_api(retry, method="GET", path=model_ref.path) |
| 458 | + return Model.from_api_repr(api_response) |
| 459 | + |
431 | 460 | def get_table(self, table, retry=DEFAULT_RETRY): |
432 | 461 | """Fetch the table referenced by ``table``. |
433 | 462 |
|
@@ -488,6 +517,41 @@ def update_dataset(self, dataset, fields, retry=DEFAULT_RETRY): |
488 | 517 | ) |
489 | 518 | return Dataset.from_api_repr(api_response) |
490 | 519 |
|
| 520 | + def update_model(self, model, fields, retry=DEFAULT_RETRY): |
| 521 | + """[Beta] Change some fields of a model. |
| 522 | +
|
| 523 | + Use ``fields`` to specify which fields to update. At least one field |
| 524 | + must be provided. If a field is listed in ``fields`` and is ``None`` |
| 525 | + in ``model``, it will be deleted. |
| 526 | +
|
| 527 | + If ``model.etag`` is not ``None``, the update will only succeed if |
| 528 | + the model on the server has the same ETag. Thus reading a model with |
| 529 | + ``get_model``, changing its fields, and then passing it to |
| 530 | + ``update_model`` will ensure that the changes will only be saved if |
| 531 | + no modifications to the model occurred since the read. |
| 532 | +
|
| 533 | + Args: |
| 534 | + model (google.cloud.bigquery.model.Model): The model to update. |
| 535 | + fields (Sequence[str]): |
| 536 | + The fields of ``model`` to change, spelled as the Model |
| 537 | + properties (e.g. "friendly_name"). |
| 538 | + retry (google.api_core.retry.Retry): |
| 539 | + (Optional) A description of how to retry the API call. |
| 540 | +
|
| 541 | + Returns: |
| 542 | + google.cloud.bigquery.model.Model: |
| 543 | + The model resource returned from the API call. |
| 544 | + """ |
| 545 | + partial = model._build_resource(fields) |
| 546 | + if model.etag: |
| 547 | + headers = {"If-Match": model.etag} |
| 548 | + else: |
| 549 | + headers = None |
| 550 | + api_response = self._call_api( |
| 551 | + retry, method="PATCH", path=model.path, data=partial, headers=headers |
| 552 | + ) |
| 553 | + return Model.from_api_repr(api_response) |
| 554 | + |
491 | 555 | def update_table(self, table, fields, retry=DEFAULT_RETRY): |
492 | 556 | """Change some fields of a table. |
493 | 557 |
|
@@ -523,6 +587,64 @@ def update_table(self, table, fields, retry=DEFAULT_RETRY): |
523 | 587 | ) |
524 | 588 | return Table.from_api_repr(api_response) |
525 | 589 |
|
| 590 | + def list_models( |
| 591 | + self, dataset, max_results=None, page_token=None, retry=DEFAULT_RETRY |
| 592 | + ): |
| 593 | + """[Beta] List models in the dataset. |
| 594 | +
|
| 595 | + See |
| 596 | + https://cloud.google.com/bigquery/docs/reference/rest/v2/models/list |
| 597 | +
|
| 598 | + Args: |
| 599 | + dataset (Union[ \ |
| 600 | + :class:`~google.cloud.bigquery.dataset.Dataset`, \ |
| 601 | + :class:`~google.cloud.bigquery.dataset.DatasetReference`, \ |
| 602 | + str, \ |
| 603 | + ]): |
| 604 | + A reference to the dataset whose models to list from the |
| 605 | + BigQuery API. If a string is passed in, this method attempts |
| 606 | + to create a dataset reference from a string using |
| 607 | + :func:`google.cloud.bigquery.dataset.DatasetReference.from_string`. |
| 608 | + max_results (int): |
| 609 | + (Optional) Maximum number of models to return. If not passed, |
| 610 | + defaults to a value set by the API. |
| 611 | + page_token (str): |
| 612 | + (Optional) Token representing a cursor into the models. If |
| 613 | + not passed, the API will return the first page of models. The |
| 614 | + token marks the beginning of the iterator to be returned and |
| 615 | + the value of the ``page_token`` can be accessed at |
| 616 | + ``next_page_token`` of the |
| 617 | + :class:`~google.api_core.page_iterator.HTTPIterator`. |
| 618 | + retry (:class:`google.api_core.retry.Retry`): |
| 619 | + (Optional) How to retry the RPC. |
| 620 | +
|
| 621 | + Returns: |
| 622 | + google.api_core.page_iterator.Iterator: |
| 623 | + Iterator of |
| 624 | + :class:`~google.cloud.bigquery.model.Model` contained |
| 625 | + within the requested dataset. |
| 626 | + """ |
| 627 | + if isinstance(dataset, str): |
| 628 | + dataset = DatasetReference.from_string( |
| 629 | + dataset, default_project=self.project |
| 630 | + ) |
| 631 | + |
| 632 | + if not isinstance(dataset, (Dataset, DatasetReference)): |
| 633 | + raise TypeError("dataset must be a Dataset, DatasetReference, or string") |
| 634 | + |
| 635 | + path = "%s/models" % dataset.path |
| 636 | + result = page_iterator.HTTPIterator( |
| 637 | + client=self, |
| 638 | + api_request=functools.partial(self._call_api, retry), |
| 639 | + path=path, |
| 640 | + item_to_value=_item_to_model, |
| 641 | + items_key="models", |
| 642 | + page_token=page_token, |
| 643 | + max_results=max_results, |
| 644 | + ) |
| 645 | + result.dataset = dataset |
| 646 | + return result |
| 647 | + |
526 | 648 | def list_tables( |
527 | 649 | self, dataset, max_results=None, page_token=None, retry=DEFAULT_RETRY |
528 | 650 | ): |
@@ -629,6 +751,40 @@ def delete_dataset( |
629 | 751 | if not not_found_ok: |
630 | 752 | raise |
631 | 753 |
|
| 754 | + def delete_model(self, model, retry=DEFAULT_RETRY, not_found_ok=False): |
| 755 | + """[Beta] Delete a model |
| 756 | +
|
| 757 | + See |
| 758 | + https://cloud.google.com/bigquery/docs/reference/rest/v2/models/delete |
| 759 | +
|
| 760 | + Args: |
| 761 | + model (Union[ \ |
| 762 | + :class:`~google.cloud.bigquery.model.Model`, \ |
| 763 | + :class:`~google.cloud.bigquery.model.ModelReference`, \ |
| 764 | + str, \ |
| 765 | + ]): |
| 766 | + A reference to the model to delete. If a string is passed in, |
| 767 | + this method attempts to create a model reference from a |
| 768 | + string using |
| 769 | + :func:`google.cloud.bigquery.model.ModelReference.from_string`. |
| 770 | + retry (:class:`google.api_core.retry.Retry`): |
| 771 | + (Optional) How to retry the RPC. |
| 772 | + not_found_ok (bool): |
| 773 | + Defaults to ``False``. If ``True``, ignore "not found" errors |
| 774 | + when deleting the model. |
| 775 | + """ |
| 776 | + if isinstance(model, str): |
| 777 | + model = ModelReference.from_string(model, default_project=self.project) |
| 778 | + |
| 779 | + if not isinstance(model, (Model, ModelReference)): |
| 780 | + raise TypeError("model must be a Model or a ModelReference") |
| 781 | + |
| 782 | + try: |
| 783 | + self._call_api(retry, method="DELETE", path=model.path) |
| 784 | + except google.api_core.exceptions.NotFound: |
| 785 | + if not not_found_ok: |
| 786 | + raise |
| 787 | + |
632 | 788 | def delete_table(self, table, retry=DEFAULT_RETRY, not_found_ok=False): |
633 | 789 | """Delete a table |
634 | 790 |
|
@@ -1823,6 +1979,21 @@ def _item_to_job(iterator, resource): |
1823 | 1979 | return iterator.client.job_from_resource(resource) |
1824 | 1980 |
|
1825 | 1981 |
|
| 1982 | +def _item_to_model(iterator, resource): |
| 1983 | + """Convert a JSON model to the native object. |
| 1984 | +
|
| 1985 | + Args: |
| 1986 | + iterator (google.api_core.page_iterator.Iterator): |
| 1987 | + The iterator that is currently in use. |
| 1988 | + resource (dict): |
| 1989 | + An item to be converted to a model. |
| 1990 | +
|
| 1991 | + Returns: |
| 1992 | + google.cloud.bigquery.model.Model: The next model in the page. |
| 1993 | + """ |
| 1994 | + return Model.from_api_repr(resource) |
| 1995 | + |
| 1996 | + |
1826 | 1997 | def _item_to_table(iterator, resource): |
1827 | 1998 | """Convert a JSON table to the native object. |
1828 | 1999 |
|
|
0 commit comments