|
80 | 80 | _MAX_MULTIPART_SIZE = 5 * 1024 * 1024
|
81 | 81 | _DEFAULT_NUM_RETRIES = 6
|
82 | 82 | _BASE_UPLOAD_TEMPLATE = (
|
83 |
| -u"https://bigquery.googleapis.com/upload/bigquery/v2/projects/" |
84 |
| -u"{project}/jobs?uploadType=" |
| 83 | +"https://bigquery.googleapis.com/upload/bigquery/v2/projects/" |
| 84 | +"{project}/jobs?uploadType=" |
85 | 85 | )
|
86 |
| -_MULTIPART_URL_TEMPLATE = _BASE_UPLOAD_TEMPLATE + u"multipart" |
87 |
| -_RESUMABLE_URL_TEMPLATE = _BASE_UPLOAD_TEMPLATE + u"resumable" |
88 |
| -_GENERIC_CONTENT_TYPE = u"*/*" |
| 86 | +_MULTIPART_URL_TEMPLATE = _BASE_UPLOAD_TEMPLATE + "multipart" |
| 87 | +_RESUMABLE_URL_TEMPLATE = _BASE_UPLOAD_TEMPLATE + "resumable" |
| 88 | +_GENERIC_CONTENT_TYPE = "*/*" |
89 | 89 | _READ_LESS_THAN_SIZE = (
|
90 | 90 | "Size {:d} was specified but the file-like object only had " "{:d} bytes remaining."
|
91 | 91 | )
|
92 | 92 | _NEED_TABLE_ARGUMENT = (
|
93 | 93 | "The table argument should be a table ID string, Table, or TableReference"
|
94 | 94 | )
|
| 95 | +_LIST_ROWS_FROM_QUERY_RESULTS_FIELDS = "jobReference,totalRows,pageToken,rows" |
95 | 96 |
|
96 | 97 |
|
97 | 98 | class Project(object):
|
@@ -293,7 +294,7 @@ def api_request(*args, **kwargs):
|
293 | 294 | span_attributes=span_attributes,
|
294 | 295 | *args,
|
295 | 296 | timeout=timeout,
|
296 |
| -**kwargs |
| 297 | +**kwargs, |
297 | 298 | )
|
298 | 299 |
|
299 | 300 | return page_iterator.HTTPIterator(
|
@@ -371,7 +372,7 @@ def api_request(*args, **kwargs):
|
371 | 372 | span_attributes=span_attributes,
|
372 | 373 | *args,
|
373 | 374 | timeout=timeout,
|
374 |
| -**kwargs |
| 375 | +**kwargs, |
375 | 376 | )
|
376 | 377 |
|
377 | 378 | return page_iterator.HTTPIterator(
|
@@ -1129,7 +1130,7 @@ def api_request(*args, **kwargs):
|
1129 | 1130 | span_attributes=span_attributes,
|
1130 | 1131 | *args,
|
1131 | 1132 | timeout=timeout,
|
1132 |
| -**kwargs |
| 1133 | +**kwargs, |
1133 | 1134 | )
|
1134 | 1135 |
|
1135 | 1136 | result = page_iterator.HTTPIterator(
|
@@ -1207,7 +1208,7 @@ def api_request(*args, **kwargs):
|
1207 | 1208 | span_attributes=span_attributes,
|
1208 | 1209 | *args,
|
1209 | 1210 | timeout=timeout,
|
1210 |
| -**kwargs |
| 1211 | +**kwargs, |
1211 | 1212 | )
|
1212 | 1213 |
|
1213 | 1214 | result = page_iterator.HTTPIterator(
|
@@ -1284,7 +1285,7 @@ def api_request(*args, **kwargs):
|
1284 | 1285 | span_attributes=span_attributes,
|
1285 | 1286 | *args,
|
1286 | 1287 | timeout=timeout,
|
1287 |
| -**kwargs |
| 1288 | +**kwargs, |
1288 | 1289 | )
|
1289 | 1290 |
|
1290 | 1291 | result = page_iterator.HTTPIterator(
|
@@ -1510,7 +1511,7 @@ def delete_table(
|
1510 | 1511 | raise
|
1511 | 1512 |
|
1512 | 1513 | def _get_query_results(
|
1513 |
| -self, job_id, retry, project=None, timeout_ms=None, location=None, timeout=None |
| 1514 | +self, job_id, retry, project=None, timeout_ms=None, location=None, timeout=None, |
1514 | 1515 | ):
|
1515 | 1516 | """Get the query results object for a query job.
|
1516 | 1517 |
|
@@ -1890,7 +1891,7 @@ def api_request(*args, **kwargs):
|
1890 | 1891 | span_attributes=span_attributes,
|
1891 | 1892 | *args,
|
1892 | 1893 | timeout=timeout,
|
1893 |
| -**kwargs |
| 1894 | +**kwargs, |
1894 | 1895 | )
|
1895 | 1896 |
|
1896 | 1897 | return page_iterator.HTTPIterator(
|
@@ -2374,7 +2375,7 @@ def load_table_from_json(
|
2374 | 2375 |
|
2375 | 2376 | destination = _table_arg_to_table_ref(destination, default_project=self.project)
|
2376 | 2377 |
|
2377 |
| -data_str = u"\n".join(json.dumps(item) for item in json_rows) |
| 2378 | +data_str = "\n".join(json.dumps(item) for item in json_rows) |
2378 | 2379 | encoded_str = data_str.encode()
|
2379 | 2380 | data_file = io.BytesIO(encoded_str)
|
2380 | 2381 | return self.load_table_from_file(
|
@@ -3169,6 +3170,83 @@ def list_rows(
|
3169 | 3170 | # Pass in selected_fields separately from schema so that full
|
3170 | 3171 | # tables can be fetched without a column filter.
|
3171 | 3172 | selected_fields=selected_fields,
|
| 3173 | +total_rows=getattr(table, "num_rows", None), |
| 3174 | +) |
| 3175 | +return row_iterator |
| 3176 | + |
| 3177 | +def _list_rows_from_query_results( |
| 3178 | +self, |
| 3179 | +job_id, |
| 3180 | +location, |
| 3181 | +project, |
| 3182 | +schema, |
| 3183 | +total_rows=None, |
| 3184 | +destination=None, |
| 3185 | +max_results=None, |
| 3186 | +start_index=None, |
| 3187 | +page_size=None, |
| 3188 | +retry=DEFAULT_RETRY, |
| 3189 | +timeout=None, |
| 3190 | +): |
| 3191 | +"""List the rows of a completed query. |
| 3192 | +See |
| 3193 | +https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/getQueryResults |
| 3194 | +Args: |
| 3195 | +job_id (str): |
| 3196 | +ID of a query job. |
| 3197 | +location (str): Location of the query job. |
| 3198 | +project (str): |
| 3199 | +ID of the project where the query job was run. |
| 3200 | +schema (Sequence[google.cloud.bigquery.schema.SchemaField]): |
| 3201 | +The fields expected in these query results. Used to convert |
| 3202 | +from JSON to expected Python types. |
| 3203 | +total_rows (Optional[int]): |
| 3204 | +Total number of rows in the query results. |
| 3205 | +destination (Optional[Union[ \ |
| 3206 | + google.cloud.bigquery.table.Table, \ |
| 3207 | + google.cloud.bigquery.table.TableListItem, \ |
| 3208 | + google.cloud.bigquery.table.TableReference, \ |
| 3209 | + str, \ |
| 3210 | + ]]): |
| 3211 | +Destination table reference. Used to fetch the query results |
| 3212 | +with the BigQuery Storage API. |
| 3213 | +max_results (Optional[int]): |
| 3214 | +Maximum number of rows to return across the whole iterator. |
| 3215 | +start_index (Optional[int]): |
| 3216 | +The zero-based index of the starting row to read. |
| 3217 | +page_size (Optional[int]): |
| 3218 | +The maximum number of rows in each page of results from this request. |
| 3219 | +Non-positive values are ignored. Defaults to a sensible value set by the API. |
| 3220 | +retry (Optional[google.api_core.retry.Retry]): |
| 3221 | +How to retry the RPC. |
| 3222 | +timeout (Optional[float]): |
| 3223 | +The number of seconds to wait for the underlying HTTP transport |
| 3224 | +before using ``retry``. |
| 3225 | +If multiple requests are made under the hood, ``timeout`` |
| 3226 | +applies to each individual request. |
| 3227 | +Returns: |
| 3228 | +google.cloud.bigquery.table.RowIterator: |
| 3229 | +Iterator of row data |
| 3230 | +:class:`~google.cloud.bigquery.table.Row`-s. |
| 3231 | +""" |
| 3232 | +params = { |
| 3233 | +"fields": _LIST_ROWS_FROM_QUERY_RESULTS_FIELDS, |
| 3234 | +"location": location, |
| 3235 | +} |
| 3236 | + |
| 3237 | +if start_index is not None: |
| 3238 | +params["startIndex"] = start_index |
| 3239 | + |
| 3240 | +row_iterator = RowIterator( |
| 3241 | +client=self, |
| 3242 | +api_request=functools.partial(self._call_api, retry, timeout=timeout), |
| 3243 | +path=f"/projects/{project}/queries/{job_id}", |
| 3244 | +schema=schema, |
| 3245 | +max_results=max_results, |
| 3246 | +page_size=page_size, |
| 3247 | +table=destination, |
| 3248 | +extra_params=params, |
| 3249 | +total_rows=total_rows, |
3172 | 3250 | )
|
3173 | 3251 | return row_iterator
|
3174 | 3252 |
|
|
0 commit comments