Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
Move BigQuery list_ methods to use iterators#2565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
5b0f9c8092dd090e7b01fd3a6dff76fa05cFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -32,7 +32,8 @@ | ||
| from google.cloud.streaming.transfer import RESUMABLE_UPLOAD | ||
| from google.cloud.streaming.transfer import Upload | ||
| from google.cloud.bigquery.schema import SchemaField | ||
| from google.cloud.bigquery._helpers import _rows_from_json | ||
| from google.cloud.bigquery._helpers import _row_from_json | ||
| from google.cloud.iterator import HTTPIterator | ||
| _TABLE_HAS_NO_SCHEMA = "Table has no schema: call 'table.reload()'" | ||
| @@ -653,47 +654,36 @@ def fetch_data(self, max_results=None, page_token=None, client=None): | ||
| up-to-date with the schema as defined on the back-end: if the | ||
| two schemas are not identical, the values returned may be | ||
| incomplete. To ensure that the local copy of the schema is | ||
| up-to-date, call the table's ``reload`` method. | ||
| up-to-date, call :meth:`reload`. | ||
| :type max_results: int | ||
| :param max_results: (Optional) maximum number of rows to return. | ||
| :param max_results: (Optional) Maximum number of rows to return. | ||
| :type page_token: str | ||
| :param page_token: | ||
| (Optional) token representing a cursor into the table's rows. | ||
| :type client: :class:`~google.cloud.bigquery.client.Client` or | ||
| ``NoneType`` | ||
| :param client: the client to use. If not passed, falls back to the | ||
| ``client`` stored on the current dataset. | ||
| :rtype: tuple | ||
| :returns: ``(row_data, total_rows, page_token)``, where ``row_data`` | ||
| is a list of tuples, one per result row, containing only | ||
| the values; ``total_rows`` is a count of the total number | ||
| of rows in the table; and ``page_token`` is an opaque | ||
| string which can be used to fetch the next batch of rows | ||
| (``None`` if no further batches can be fetched). | ||
| :param page_token: (Optional) Token representing a cursor into the | ||
| table's rows. | ||
| :type client: :class:`~google.cloud.bigquery.client.Client` | ||
| :param client: (Optional) The client to use. If not passed, falls | ||
| back to the ``client`` stored on the current dataset. | ||
| :rtype: :class:`~google.cloud.iterator.Iterator` | ||
| :returns: Iterator of row data :class:`tuple`s. During each page, the | ||
| iterator will have the ``total_rows`` attribute set, | ||
| which counts the total number of rows **in the table** | ||
| (this is distinct from the total number of rows in the | ||
| current page: ``iterator.page.num_items``). | ||
| """ | ||
| client = self._require_client(client) | ||
| params = {} | ||
| if max_results is not None: | ||
| params['maxResults'] = max_results | ||
| if page_token is not None: | ||
| params['pageToken'] = page_token | ||
| response = client.connection.api_request(method='GET', | ||
| path='%s/data' % self.path, | ||
| query_params=params) | ||
| total_rows = response.get('totalRows') | ||
| if total_rows is not None: | ||
| total_rows = int(total_rows) | ||
| page_token = response.get('pageToken') | ||
| rows_data = _rows_from_json(response.get('rows', ()), self._schema) | ||
| return rows_data, total_rows, page_token | ||
| path = '%s/data' % (self.path,) | ||
| iterator = HTTPIterator(client=client, path=path, | ||
| item_to_value=_item_to_row, items_key='rows', | ||
| page_token=page_token, max_results=max_results, | ||
| page_start=_rows_page_start) | ||
| iterator.schema = self._schema | ||
| # Over-ride the key used to retrieve the next page token. | ||
| iterator._NEXT_TOKEN = 'pageToken' | ||
| return iterator | ||
| def insert_data(self, | ||
| rows, | ||
| @@ -1083,6 +1073,47 @@ def _build_schema_resource(fields): | ||
| return infos | ||
| def _item_to_row(iterator, resource): | ||
| """Convert a JSON row to the native object. | ||
| .. note:: | ||
| This assumes that the ``schema`` attribute has been | ||
| added to the iterator after being created, which | ||
| should be done by the caller. | ||
| :type iterator: :class:`~google.cloud.iterator.Iterator` | ||
| :param iterator: The iterator that is currently in use. | ||
| :type resource: dict | ||
| :param resource: An item to be converted to a row. | ||
| :rtype: tuple | ||
| :returns: The next row in the page. | ||
| """ | ||
| return _row_from_json(resource, iterator.schema) | ||
| # pylint: disable=unused-argument | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| def _rows_page_start(iterator, page, response): | ||
| """Grab total rows after a :class:`~google.cloud.iterator.Page` started. | ||
| :type iterator: :class:`~google.cloud.iterator.Iterator` | ||
| :param iterator: The iterator that is currently in use. | ||
| :type page: :class:`~google.cloud.iterator.Page` | ||
| :param page: The page that was just created. | ||
| :type response: dict | ||
| :param response: The JSON API response for a page of rows in a table. | ||
| """ | ||
| total_rows = response.get('totalRows') | ||
| if total_rows is not None: | ||
| total_rows = int(total_rows) | ||
| iterator.total_rows = total_rows | ||
| # pylint: enable=unused-argument | ||
| class _UploadConfig(object): | ||
| """Faux message FBO apitools' 'configure_request'.""" | ||
| accept = ['*/*'] | ||
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.