-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: BigQuery Storage v1beta1 API migration guide #13537
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
Open
benhxy
wants to merge
1
commit into
googleapis:main
Choose a base branch
from
benhxy:v1b1-guide
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+164
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| # Migrating BigQuery Storage API from v1beta1 to v1: Java | ||
|
|
||
| This guide shows how to migrate Java code using the BigQuery Storage API from | ||
| version `v1beta1` to `v1`. | ||
|
|
||
| ## Key Changes | ||
|
|
||
| * **Service Client**: `BigQueryStorageClient` is replaced by | ||
| `BigQueryReadClient`. | ||
| * **Table Reference**: `TableReference` proto message is replaced by a simple | ||
| string representation of the table path in `ReadSession`. | ||
| * **Session Configuration**: Several fields from `CreateReadSessionRequest` | ||
| have moved into `ReadSession` (which is now passed as a field in the | ||
| request). | ||
| * **Parallelism**: `requested_streams` is replaced by `max_stream_count`. | ||
| * **Sharding Strategy**: `sharding_strategy` is removed. The server now | ||
| automatically balances the streams. | ||
| * **Read Rows Request**: `StreamPosition` is flattened. You now pass the | ||
| stream name directly as `read_stream` and the `offset` as a top-level field | ||
| in `ReadRowsRequest`. | ||
|
|
||
| ## Code Comparison | ||
|
|
||
| ### 1. Client Initialization | ||
|
|
||
| **v1beta1:** | ||
|
|
||
| ```java | ||
| import com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageClient; | ||
|
|
||
| try (BigQueryStorageClient client = BigQueryStorageClient.create()) { | ||
| // use client | ||
| } | ||
| ``` | ||
|
|
||
| **v1:** | ||
|
|
||
| ```java | ||
| import com.google.cloud.bigquery.storage.v1.BigQueryReadClient; | ||
|
|
||
| try (BigQueryReadClient client = BigQueryReadClient.create()) { | ||
| // use client | ||
| } | ||
| ``` | ||
|
|
||
| ### 2. Creating a Read Session | ||
|
|
||
| **v1beta1:** | ||
|
|
||
| ```java | ||
| import com.google.cloud.bigquery.storage.v1beta1.BigQueryStorageClient; | ||
| import com.google.cloud.bigquery.storage.v1beta1.Storage.CreateReadSessionRequest; | ||
| import com.google.cloud.bigquery.storage.v1beta1.Storage.DataFormat; | ||
| import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadSession; | ||
| import com.google.cloud.bigquery.storage.v1beta1.Storage.ShardingStrategy; | ||
| import com.google.cloud.bigquery.storage.v1beta1.ReadOptions.TableReadOptions; | ||
| import com.google.cloud.bigquery.storage.v1beta1.TableReferenceProto.TableReference; | ||
|
|
||
| // ... | ||
|
|
||
| TableReference tableReference = TableReference.newBuilder() | ||
| .setProjectId("bigquery-public-data") | ||
| .setDatasetId("usa_names") | ||
| .setTableId("usa_1910_current") | ||
| .build(); | ||
|
|
||
| TableReadOptions options = TableReadOptions.newBuilder() | ||
| .addSelectedFields("name") | ||
| .setRowRestriction("state = \"WA\"") | ||
| .build(); | ||
|
|
||
| CreateReadSessionRequest request = CreateReadSessionRequest.newBuilder() | ||
| .setParent("projects/read-session-project") | ||
| .setTableReference(tableReference) | ||
| .setReadOptions(options) | ||
| .setRequestedStreams(1) | ||
| .setFormat(DataFormat.AVRO) | ||
| .setShardingStrategy(ShardingStrategy.LIQUID) | ||
| .build(); | ||
|
|
||
| ReadSession session = client.createReadSession(request); | ||
| ``` | ||
|
|
||
| **v1:** | ||
|
|
||
| ```java | ||
| import com.google.cloud.bigquery.storage.v1.BigQueryReadClient; | ||
| import com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest; | ||
| import com.google.cloud.bigquery.storage.v1.DataFormat; | ||
| import com.google.cloud.bigquery.storage.v1.ReadSession; | ||
| import com.google.cloud.bigquery.storage.v1.ReadSession.TableReadOptions; | ||
|
|
||
| // ... | ||
|
|
||
| // Table path is now a string: projects/{project}/datasets/{dataset}/tables/{table} | ||
| String tablePath = "projects/bigquery-public-data/datasets/usa_names/tables/usa_1910_current"; | ||
|
|
||
| TableReadOptions options = TableReadOptions.newBuilder() | ||
| .addSelectedFields("name") | ||
| .setRowRestriction("state = \"WA\"") | ||
| .build(); | ||
|
|
||
| // ReadSession holds the session configuration (table, options, format) | ||
| ReadSession readSession = ReadSession.newBuilder() | ||
| .setTable(tablePath) | ||
| .setDataFormat(DataFormat.AVRO) // format renamed to data_format | ||
| .setReadOptions(options) | ||
| .build(); | ||
|
|
||
| CreateReadSessionRequest request = CreateReadSessionRequest.newBuilder() | ||
| .setParent("projects/read-session-project") | ||
| .setReadSession(readSession) | ||
| .setMaxStreamCount(1) // requested_streams renamed to max_stream_count | ||
| .build(); | ||
|
|
||
| ReadSession session = client.createReadSession(request); | ||
| ``` | ||
|
|
||
| ### 3. Reading Rows | ||
|
|
||
| **v1beta1:** | ||
|
|
||
| ```java | ||
| import com.google.api.gax.rpc.ServerStream; | ||
| import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsRequest; | ||
| import com.google.cloud.bigquery.storage.v1beta1.Storage.ReadRowsResponse; | ||
| import com.google.cloud.bigquery.storage.v1beta1.Storage.StreamPosition; | ||
|
|
||
| // ... | ||
| StreamPosition readPosition = StreamPosition.newBuilder() | ||
| .setStream(session.getStreams(0)) // Stream object | ||
| .setOffset(0) | ||
| .build(); | ||
|
|
||
| ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder() | ||
| .setReadPosition(readPosition) | ||
| .build(); | ||
|
|
||
| ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest); | ||
| for (ReadRowsResponse response : stream) { | ||
| // Process response.getAvroRows() | ||
| } | ||
| ``` | ||
|
|
||
| **v1:** | ||
|
|
||
| ```java | ||
| import com.google.api.gax.rpc.ServerStream; | ||
| import com.google.cloud.bigquery.storage.v1.ReadRowsRequest; | ||
| import com.google.cloud.bigquery.storage.v1.ReadRowsResponse; | ||
|
|
||
| // ... | ||
| // ReadRowsRequest is flattened. We pass the stream name (string) and offset directly. | ||
| ReadRowsRequest readRowsRequest = ReadRowsRequest.newBuilder() | ||
| .setReadStream(session.getStreams(0).getName()) // Stream name string | ||
| .setOffset(0) | ||
| .build(); | ||
|
|
||
| ServerStream<ReadRowsResponse> stream = client.readRowsCallable().call(readRowsRequest); | ||
| for (ReadRowsResponse response : stream) { | ||
| // Process response.getAvroRows() | ||
| // Note: Prefer using response.getRowCount() over response.getAvroRows().getRowCount() (deprecated) | ||
| } | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
v1API, theAvroRowsmessage does not contain arow_countfield (it was removed entirely, not just deprecated). Therefore, attempting to callresponse.getAvroRows().getRowCount()will result in a compilation error. The comment should be updated to clarify thatrow_counthas been moved toReadRowsResponseand is no longer available onAvroRows.