Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit a0fc0af

Browse files
authored
feat: add manual layer for v1 endpoint (#16)
* feat: add manual layer for v1 endpoint * migrate the manual client/reader work for v1 * TableReferences went away as a first class message, now just a formatted string * changes to read rows * estimated_row_count removed due to differences in reported status * you no longer have to deal with a StreamPosition message, instead there's just a stream name and an offset as top-level request fields * session creation changes * you now provide a prototypical ReadSession message when requesting a new read session, and most options (like selected fields, table modifiers, and data format) have moved inside it. * requested_streams -> max_stream_count There's additional changes to the surface, but there wasn't much manual help in front of it so not reflected here.
1 parent e522bf8 commit a0fc0af

13 files changed

Lines changed: 2405 additions & 2 deletions
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2020 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from __future__ import absolute_import
18+
19+
import pkg_resources
20+
21+
__version__ = pkg_resources.get_distribution(
22+
"google-cloud-bigquery-storage"
23+
).version # noqa
24+
25+
from google.cloud.bigquery_storage_v1 import types
26+
from google.cloud.bigquery_storage_v1 import client
27+
from google.cloud.bigquery_storage_v1.gapic import enums
28+
29+
30+
class BigQueryReadClient(client.BigQueryReadClient):
31+
__doc__ = client.BigQueryReadClient.__doc__
32+
enums = enums
33+
34+
35+
__all__ = (
36+
# google.cloud.bigquery_storage_v1
37+
"__version__",
38+
"types",
39+
# google.cloud.bigquery_storage_v1.client
40+
"BigQueryReadClient",
41+
# google.cloud.bigquery_storage_v1.gapic
42+
"enums",
43+
)
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2020 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Parent client for calling the Cloud BigQuery Storage API.
18+
19+
This is the base from which all interactions with the API occur.
20+
"""
21+
22+
from __future__ import absolute_import
23+
24+
import google.api_core.gapic_v1.method
25+
26+
from google.cloud.bigquery_storage_v1 import reader
27+
from google.cloud.bigquery_storage_v1.gapic import big_query_read_client # noqa
28+
29+
30+
_SCOPES = (
31+
"https://www.googleapis.com/auth/bigquery",
32+
"https://www.googleapis.com/auth/cloud-platform",
33+
)
34+
35+
36+
class BigQueryReadClient(big_query_read_client.BigQueryReadClient):
37+
"""Client for interacting with BigQuery Storage API.
38+
39+
The BigQuery storage API can be used to read data stored in BigQuery.
40+
"""
41+
42+
def read_rows(
43+
self,
44+
name,
45+
offset=0,
46+
retry=google.api_core.gapic_v1.method.DEFAULT,
47+
timeout=google.api_core.gapic_v1.method.DEFAULT,
48+
metadata=None,
49+
):
50+
"""
51+
Reads rows from the table in the format prescribed by the read
52+
session. Each response contains one or more table rows, up to a
53+
maximum of 10 MiB per response; read requests which attempt to read
54+
individual rows larger than this will fail.
55+
56+
Each request also returns a set of stream statistics reflecting the
57+
estimated total number of rows in the read stream. This number is
58+
computed based on the total table size and the number of active
59+
streams in the read session, and may change as other streams continue
60+
to read data.
61+
62+
Example:
63+
>>> from google.cloud import bigquery_storage_v1
64+
>>>
65+
>>> client = bigquery_storage_v1.BigQueryReadClient()
66+
>>>
67+
>>> # TODO: Initialize ``table``:
68+
>>> table = "projects/{}/datasets/{}/tables/{}".format(
69+
... 'project_id': 'your-data-project-id',
70+
... 'dataset_id': 'your_dataset_id',
71+
... 'table_id': 'your_table_id',
72+
... )
73+
>>>
74+
>>> # TODO: Initialize `parent`:
75+
>>> parent = 'projects/your-billing-project-id'
76+
>>>
77+
>>> session = client.create_read_session(table, parent)
78+
>>> stream=session.streams[0], # TODO: Read the other streams.
79+
...
80+
>>>
81+
>>> for element in client.read_rows(stream):
82+
... # process element
83+
... pass
84+
85+
Args:
86+
name (str):
87+
Required. Name of the stream to start
88+
reading from, of the form
89+
`projects/{project_id}/locations/{location}/sessions/{session_id}/streams/{stream_id}`
90+
offset (Optional[int]):
91+
The starting offset from which to begin reading rows from
92+
in the stream. The offset requested must be less than the last
93+
row read from ReadRows. Requesting a larger offset is
94+
undefined.
95+
retry (Optional[google.api_core.retry.Retry]): A retry object used
96+
to retry requests. If ``None`` is specified, requests will not
97+
be retried.
98+
timeout (Optional[float]): The amount of time, in seconds, to wait
99+
for the request to complete. Note that if ``retry`` is
100+
specified, the timeout applies to each individual attempt.
101+
metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
102+
that is provided to the method.
103+
104+
Returns:
105+
~google.cloud.bigquery_storage_v1.reader.ReadRowsStream:
106+
An iterable of
107+
:class:`~google.cloud.bigquery_storage_v1.types.ReadRowsResponse`.
108+
109+
Raises:
110+
google.api_core.exceptions.GoogleAPICallError: If the request
111+
failed for any reason.
112+
google.api_core.exceptions.RetryError: If the request failed due
113+
to a retryable error and retry attempts failed.
114+
ValueError: If the parameters are invalid.
115+
"""
116+
gapic_client = super(BigQueryReadClient, self)
117+
stream = gapic_client.read_rows(
118+
read_stream=name,
119+
offset=offset,
120+
retry=retry,
121+
timeout=timeout,
122+
metadata=metadata,
123+
)
124+
return reader.ReadRowsStream(
125+
stream,
126+
gapic_client,
127+
name,
128+
offset,
129+
{"retry": retry, "timeout": timeout, "metadata": metadata},
130+
)

0 commit comments

Comments
 (0)