Skip to content

Commit 4200033

Browse files
feat: BigQuery Storage v1beta1 API migration guide (#10316)
* feat: BigQuery Storage v1beta1 API migration guide * Update bigquery/bigquerystorage/v1beta1-migration-guide.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update bigquery/bigquerystorage/v1beta1-migration-guide.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent f4ee466 commit 4200033

1 file changed

Lines changed: 169 additions & 0 deletions

File tree

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# Migrating BigQuery Storage API from v1beta1 to v1: Java
2+
3+
This guide shows how to migrate Java code using the BigQuery Storage API from
4+
version `v1beta1` to `v1`.
5+
6+
## Key Changes
7+
8+
* **Service Client**: `BigQueryStorageClient` is replaced by
9+
`BigQueryReadClient`.
10+
* **Table Reference**: `TableReference` proto message is replaced by a simple
11+
string representation of the table path in `ReadSession`.
12+
* **Session Configuration**: Several fields from `CreateReadSessionRequest`
13+
have moved into `ReadSession` (which is now passed as a field in the
14+
request).
15+
* **Parallelism**: `requested_streams` is replaced by `max_stream_count`.
16+
* **Sharding Strategy**: `sharding_strategy` is removed. The server now
17+
automatically balances the streams.
18+
* **Read Rows Request**: `StreamPosition` is flattened. You now pass the
19+
stream name directly as `read_stream` and the `offset` as a top-level field
20+
in `ReadRowsRequest`.
21+
22+
## Code Comparison
23+
24+
### 1. Client Initialization
25+
26+
**v1beta1:**
27+
28+
```java
29+
import com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageClient;
30+
import java.io.IOException;
31+
32+
try (BigQueryStorageClient client = BigQueryStorageClient.create()) {
33+
// use client
34+
} catch (IOException e) {
35+
// Handle client initialization failure
36+
}
37+
```
38+
39+
**v1:**
40+
41+
```java
42+
import com.google.cloud.bigquery.storage.v1.BigQueryReadClient;
43+
import java.io.IOException;
44+
45+
try (BigQueryReadClient client = BigQueryReadClient.create()) {
46+
// use client
47+
} catch (IOException e) {
48+
// Handle client initialization failure
49+
}
50+
```
51+
52+
### 2. Creating a Read Session
53+
54+
**v1beta1:**
55+
56+
```java
57+
import com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageClient;
58+
import com.google.cloud.bigquery.storage.v1beta1.Storage.CreateReadSessionRequest;
59+
import com.google.cloud.bigquery.storage.v1beta1.Storage.DataFormat;
60+
import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession;
61+
import com.google.cloud.bigquery.storage.v1beta1.Storage.ShardingStrategy;
62+
import com.google.cloud.bigquery.storage.v1beta1.ReadOptions.TableReadOptions;
63+
import com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto.TableReference;
64+
65+
// ...
66+
67+
TableReference tableReference = TableReference.newBuilder()
68+
.setProjectId("bigquery-public-data")
69+
.setDatasetId("usa_names")
70+
.setTableId("usa_1910_current")
71+
.build();
72+
73+
TableReadOptions options = TableReadOptions.newBuilder()
74+
.addSelectedFields("name")
75+
.setRowRestriction("state = \"WA\"")
76+
.build();
77+
78+
CreateReadSessionRequest request = CreateReadSessionRequest.newBuilder()
79+
.setParent("projects/read-session-project")
80+
.setTableReference(tableReference)
81+
.setReadOptions(options)
82+
.setRequestedStreams(1)
83+
.setFormat(DataFormat.AVRO)
84+
.setShardingStrategy(ShardingStrategy.LIQUID)
85+
.build();
86+
87+
ReadSession session = client.createReadSession(request);
88+
```
89+
90+
**v1:**
91+
92+
```java
93+
import com.google.cloud.bigquery.storage.v1.BigQueryReadClient;
94+
import com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest;
95+
import com.google.cloud.bigquery.storage.v1.DataFormat;
96+
import com.google.cloud.bigquery.storage.v1.ReadSession;
97+
import com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions;
98+
99+
// ...
100+
101+
// Table path is now a string: projects/{project}/datasets/{dataset}/tables/{table}
102+
String tablePath = "projects/bigquery-public-data/datasets/usa_names/tables/usa_1910_current";
103+
104+
TableReadOptions options = TableReadOptions.newBuilder()
105+
.addSelectedFields("name")
106+
.setRowRestriction("state = \"WA\"")
107+
.build();
108+
109+
// ReadSession holds the session configuration (table, options, format)
110+
ReadSession readSession = ReadSession.newBuilder()
111+
.setTable(tablePath)
112+
.setDataFormat(DataFormat.AVRO) // format renamed to data_format
113+
.setReadOptions(options)
114+
.build();
115+
116+
CreateReadSessionRequest request = CreateReadSessionRequest.newBuilder()
117+
.setParent("projects/read-session-project")
118+
.setReadSession(readSession)
119+
.setMaxStreamCount(1) // requested_streams renamed to max_stream_count
120+
.build();
121+
122+
ReadSession session = client.createReadSession(request);
123+
```
124+
125+
### 3. Reading Rows
126+
127+
**v1beta1:**
128+
129+
```java
130+
import com.google.api.gax.rpc.ServerStream;
131+
import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest;
132+
import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse;
133+
import com.google.cloud.bigquery.storage.v1beta1.Storage.StreamPosition;
134+
135+
// ...
136+
StreamPosition readPosition = StreamPosition.newBuilder()
137+
.setStream(session.getStreams(0)) // Stream object
138+
.setOffset(0)
139+
.build();
140+
141+
ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder()
142+
.setReadPosition(readPosition)
143+
.build();
144+
145+
ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest);
146+
for (ReadRowsResponse response : stream) {
147+
// Process response.getAvroRows()
148+
}
149+
```
150+
151+
**v1:**
152+
153+
```java
154+
import com.google.api.gax.rpc.ServerStream;
155+
import com.google.cloud.bigquery.storage.v1.ReadRowsRequest;
156+
import com.google.cloud.bigquery.storage.v1.ReadRowsResponse;
157+
158+
// ...
159+
// ReadRowsRequest is flattened. We pass the stream name (string) and offset directly.
160+
ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder()
161+
.setReadStream(session.getStreams(0).getName()) // Stream name string
162+
.setOffset(0)
163+
.build();
164+
165+
ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest);
166+
for (ReadRowsResponse response : stream) {
167+
// Process response.getAvroRows()
168+
}
169+
```

0 commit comments

Comments
 (0)