|
| 1 | +# Migrating BigQuery Storage API from v1beta1 to v1: Go |
| 2 | + |
| 3 | +This guide shows how to migrate Go code using the BigQuery Storage API from |
| 4 | +version `v1beta1` to `v1`. |
| 5 | + |
| 6 | +## Key Changes |
| 7 | + |
| 8 | +* **Package Imports**: |
| 9 | + * Client library: `cloud.google.com/go/bigquery/storage/apiv1beta1` -> |
| 10 | + `cloud.google.com/go/bigquery/storage/apiv1` |
| 11 | + * Proto types: |
| 12 | + `google.golang.org/genproto/googleapis/cloud/bigquery/storage/v1beta1` |
| 13 | + -> `cloud.google.com/go/bigquery/storage/apiv1/storagepb` |
| 14 | +* **Service Client**: `NewBigQueryStorageClient` is replaced by |
| 15 | + `NewBigQueryReadClient`. |
| 16 | +* **Table Reference**: `TableReference` struct is replaced by a simple string |
| 17 | + representation of the table path in `ReadSession.Table`. |
| 18 | +* **Session Configuration**: Configuration fields (table, format, read |
| 19 | + options) have moved into `ReadSession` struct, which is passed in |
| 20 | + `CreateReadSessionRequest`. |
| 21 | +* **Parallelism**: `RequestedStreams` is replaced by `MaxStreamCount`. |
| 22 | +* **Sharding Strategy**: `ShardingStrategy` field is removed. The server now |
| 23 | + automatically balances the streams. |
| 24 | +* **Read Rows Request**: `ReadPosition` is flattened. You now pass the stream |
| 25 | + name directly as `ReadStream` and the `Offset` as a top-level field in |
| 26 | + `ReadRowsRequest`. |
| 27 | +* **Stream Type**: `Stream` type is renamed to `ReadStream`. |
| 28 | + |
| 29 | +## Code Comparison |
| 30 | + |
| 31 | +### 1. Imports and Client Initialization |
| 32 | + |
| 33 | +**v1beta1:** |
| 34 | + |
| 35 | +```go |
| 36 | +import ( |
| 37 | + "context" |
| 38 | + |
| 39 | + bqStorage "cloud.google.com/go/bigquery/storage/apiv1beta1" |
| 40 | + bqStoragepb "google.golang.org/genproto/googleapis/cloud/bigquery/storage/v1beta1" |
| 41 | +) |
| 42 | + |
| 43 | +ctx := context.Background() |
| 44 | +client, err := bqStorage.NewBigQueryStorageClient(ctx) |
| 45 | +if err != nil { |
| 46 | + // handle error |
| 47 | +} |
| 48 | +defer client.Close() |
| 49 | +``` |
| 50 | + |
| 51 | +**v1:** |
| 52 | + |
| 53 | +```go |
| 54 | +import ( |
| 55 | + "context" |
| 56 | + |
| 57 | + bqStorage "cloud.google.com/go/bigquery/storage/apiv1" |
| 58 | + bqStoragepb "cloud.google.com/go/bigquery/storage/apiv1/storagepb" |
| 59 | +) |
| 60 | + |
| 61 | +ctx := context.Background() |
| 62 | +client, err := bqStorage.NewBigQueryReadClient(ctx) |
| 63 | +if err != nil { |
| 64 | + // handle error |
| 65 | +} |
| 66 | +defer client.Close() |
| 67 | +``` |
| 68 | + |
| 69 | +### 2. Creating a Read Session |
| 70 | + |
| 71 | +**v1beta1:** |
| 72 | + |
| 73 | +```go |
| 74 | +tableRef := &bqStoragepb.TableReference{ |
| 75 | + ProjectId: "bigquery-public-data", |
| 76 | + DatasetId: "usa_names", |
| 77 | + TableId: "usa_1910_current", |
| 78 | +} |
| 79 | + |
| 80 | +readOptions := &bqStoragepb.ReadSession_TableReadOptions{ |
| 81 | + SelectedFields: []string{"name"}, |
| 82 | + RowRestriction: "state = 'WA'", |
| 83 | +} |
| 84 | + |
| 85 | +req := &bqStoragepb.CreateReadSessionRequest{ |
| 86 | + Parent: "projects/read-session-project", |
| 87 | + TableReference: tableRef, |
| 88 | + ReadOptions: readOptions, |
| 89 | + RequestedStreams: 1, |
| 90 | + Format: bqStoragepb.DataFormat_AVRO, |
| 91 | + ShardingStrategy: bqStoragepb.ShardingStrategy_LIQUID, |
| 92 | +} |
| 93 | + |
| 94 | +session, err := client.CreateReadSession(ctx, req) |
| 95 | +``` |
| 96 | + |
| 97 | +**v1:** |
| 98 | + |
| 99 | +```go |
| 100 | +// Table path is now a string: projects/{project}/datasets/{dataset}/tables/{table} |
| 101 | +tablePath := "projects/bigquery-public-data/datasets/usa_names/tables/usa_1910_current" |
| 102 | + |
| 103 | +readOptions := &bqStoragepb.ReadSession_TableReadOptions{ |
| 104 | + SelectedFields: []string{"name"}, |
| 105 | + RowRestriction: "state = 'WA'", |
| 106 | +} |
| 107 | + |
| 108 | +// ReadSession holds the session configuration |
| 109 | +readSession := &bqStoragepb.ReadSession{ |
| 110 | + Table: tablePath, |
| 111 | + DataFormat: bqStoragepb.DataFormat_AVRO, // Format renamed to DataFormat |
| 112 | + ReadOptions: readOptions, |
| 113 | +} |
| 114 | + |
| 115 | +req := &bqStoragepb.CreateReadSessionRequest{ |
| 116 | + Parent: "projects/read-session-project", |
| 117 | + ReadSession: readSession, |
| 118 | + MaxStreamCount: 1, // RequestedStreams renamed to MaxStreamCount |
| 119 | +} |
| 120 | + |
| 121 | +session, err := client.CreateReadSession(ctx, req) |
| 122 | +``` |
| 123 | + |
| 124 | +### 3. Reading Rows |
| 125 | + |
| 126 | +**v1beta1:** |
| 127 | + |
| 128 | +```go |
| 129 | +// Use the first stream |
| 130 | +stream := session.GetStreams()[0] |
| 131 | + |
| 132 | +req := &bqStoragepb.ReadRowsRequest{ |
| 133 | + ReadPosition: &bqStoragepb.StreamPosition{ |
| 134 | + Stream: stream, // Stream object |
| 135 | + Offset: 0, |
| 136 | + }, |
| 137 | +} |
| 138 | + |
| 139 | +rowStream, err := client.ReadRows(ctx, req) |
| 140 | +// Iterate over rowStream.Recv() |
| 141 | +``` |
| 142 | + |
| 143 | +**v1:** |
| 144 | + |
| 145 | +```go |
| 146 | +// Use the first stream |
| 147 | +stream := session.GetStreams()[0] // returns *bqStoragepb.ReadStream |
| 148 | + |
| 149 | +req := &bqStoragepb.ReadRowsRequest{ |
| 150 | + ReadStream: stream.GetName(), // Pass stream name string directly |
| 151 | + Offset: 0, // Offset is top-level |
| 152 | +} |
| 153 | + |
| 154 | +rowStream, err := client.ReadRows(ctx, req) |
| 155 | +// Iterate over rowStream.Recv() |
| 156 | +``` |
0 commit comments