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