|
| 1 | +# Migrating BigQuery Storage API from v1beta1 to v1: TypeScript |
| 2 | + |
| 3 | +This guide shows how to migrate TypeScript code using the BigQuery Storage API |
| 4 | +from version `v1beta1` to `v1`. |
| 5 | + |
| 6 | +## Key Changes |
| 7 | + |
| 8 | +* **Service Client**: `BigQueryStorageClient` (in `v1beta1` namespace) is |
| 9 | + replaced by `BigQueryReadClient` (in `v1` namespace). |
| 10 | +* **Table Reference**: `tableReference` object is replaced by a simple string |
| 11 | + representation of the table path in `readSession.table`. |
| 12 | +* **Session Configuration**: Configuration fields (table, format, read |
| 13 | + options) have moved into `readSession` object, which is passed in |
| 14 | + `createReadSession` request. |
| 15 | +* **Parallelism**: `requestedStreams` is replaced by `maxStreamCount`. |
| 16 | +* **Sharding Strategy**: `shardingStrategy` is removed. The server now |
| 17 | + automatically balances the streams. |
| 18 | +* **Read Rows Request**: `readPosition` is flattened. You now pass the stream |
| 19 | + name directly as `readStream` and the `offset` as a top-level field in the |
| 20 | + `readRows` request. |
| 21 | + |
| 22 | +## Code Comparison |
| 23 | + |
| 24 | +### 1. Client Initialization |
| 25 | + |
| 26 | +**v1beta1:** |
| 27 | + |
| 28 | +```typescript |
| 29 | +import {v1beta1} from '@google-cloud/bigquery-storage'; |
| 30 | +const client = new v1beta1.BigQueryStorageClient(); |
| 31 | +``` |
| 32 | + |
| 33 | +**v1:** |
| 34 | + |
| 35 | +```typescript |
| 36 | +import {v1} from '@google-cloud/bigquery-storage'; |
| 37 | +const client = new v1.BigQueryReadClient(); |
| 38 | +``` |
| 39 | + |
| 40 | +### 2. Creating a Read Session |
| 41 | + |
| 42 | +**v1beta1:** |
| 43 | + |
| 44 | +```typescript |
| 45 | +import {v1beta1} from '@google-cloud/bigquery-storage'; |
| 46 | + |
| 47 | +const tableReference = { |
| 48 | + projectId: 'bigquery-public-data', |
| 49 | + datasetId: 'usa_names', |
| 50 | + tableId: 'usa_1910_current', |
| 51 | +}; |
| 52 | + |
| 53 | +const readOptions = { |
| 54 | + selectedFields: ['name'], |
| 55 | + rowRestriction: 'state = "WA"', |
| 56 | +}; |
| 57 | + |
| 58 | +const request: v1beta1.protos.google.cloud.bigquery.storage.v1beta1.ICreateReadSessionRequest = { |
| 59 | + parent: 'projects/read-session-project', |
| 60 | + tableReference: tableReference, |
| 61 | + readOptions: readOptions, |
| 62 | + requestedStreams: 1, |
| 63 | + format: v1beta1.protos.google.cloud.bigquery.storage.v1beta1.DataFormat.AVRO, |
| 64 | + shardingStrategy: v1beta1.protos.google.cloud.bigquery.storage.v1beta1.ShardingStrategy.LIQUID, |
| 65 | +}; |
| 66 | + |
| 67 | +const [session] = await client.createReadSession(request); |
| 68 | +``` |
| 69 | + |
| 70 | +**v1:** |
| 71 | + |
| 72 | +```typescript |
| 73 | +import {v1} from '@google-cloud/bigquery-storage'; |
| 74 | + |
| 75 | +// Table path is now a string: projects/{project}/datasets/{dataset}/tables/{table} |
| 76 | +const tablePath = 'projects/bigquery-public-data/datasets/usa_names/tables/usa_1910_current'; |
| 77 | + |
| 78 | +const readOptions = { |
| 79 | + selectedFields: ['name'], |
| 80 | + rowRestriction: 'state = "WA"', |
| 81 | +}; |
| 82 | + |
| 83 | +// ReadSession holds the session configuration |
| 84 | +const readSession = { |
| 85 | + table: tablePath, |
| 86 | + dataFormat: v1.protos.google.cloud.bigquery.storage.v1.DataFormat.AVRO, // format renamed to dataFormat |
| 87 | + readOptions: readOptions, |
| 88 | +}; |
| 89 | + |
| 90 | +const request: v1.protos.google.cloud.bigquery.storage.v1.ICreateReadSessionRequest = { |
| 91 | + parent: `projects/read-session-project`, |
| 92 | + readSession: readSession, |
| 93 | + maxStreamCount: 1, // requestedStreams renamed to maxStreamCount |
| 94 | +}; |
| 95 | + |
| 96 | +const [session] = await client.createReadSession(request); |
| 97 | +``` |
| 98 | + |
| 99 | +### 3. Reading Rows |
| 100 | + |
| 101 | +In Node.js client, `readRows` returns a stream that emits data events. |
| 102 | + |
| 103 | +**v1beta1:** |
| 104 | + |
| 105 | +```typescript |
| 106 | +import {v1beta1} from '@google-cloud/bigquery-storage'; |
| 107 | + |
| 108 | +const stream = session.streams?.[0]; |
| 109 | +if (!stream) { |
| 110 | + throw new Error('No streams available'); |
| 111 | +} |
| 112 | + |
| 113 | +const request: v1beta1.protos.google.cloud.bigquery.storage.v1beta1.IReadRowsRequest = { |
| 114 | + readPosition: { |
| 115 | + stream: stream, // Stream object |
| 116 | + offset: 0, |
| 117 | + }, |
| 118 | +}; |
| 119 | + |
| 120 | +const rowStream = client.readRows(request); |
| 121 | + |
| 122 | +rowStream |
| 123 | + .on('data', (response: v1beta1.protos.google.cloud.bigquery.storage.v1beta1.IReadRowsResponse) => { |
| 124 | + // Process response.avroRows |
| 125 | + }) |
| 126 | + .on('error', (err) => { |
| 127 | + // Handle error |
| 128 | + }) |
| 129 | + .on('end', () => { |
| 130 | + // Done |
| 131 | + }); |
| 132 | +``` |
| 133 | + |
| 134 | +**v1:** |
| 135 | + |
| 136 | +```typescript |
| 137 | +import {v1} from '@google-cloud/bigquery-storage'; |
| 138 | + |
| 139 | +const stream = session.streams?.[0]; |
| 140 | +if (!stream || !stream.name) { |
| 141 | + throw new Error('No streams available'); |
| 142 | +} |
| 143 | + |
| 144 | +// Request is flattened. Pass readStream (string) and offset directly. |
| 145 | +const request: v1.protos.google.cloud.bigquery.storage.v1.IReadRowsRequest = { |
| 146 | + readStream: stream.name, // Stream name string |
| 147 | + offset: 0, |
| 148 | +}; |
| 149 | + |
| 150 | +const rowStream = client.readRows(request); |
| 151 | + |
| 152 | +rowStream |
| 153 | + .on('data', (response: v1.protos.google.cloud.bigquery.storage.v1.IReadRowsResponse) => { |
| 154 | + // Process response.avroRows |
| 155 | + // Note: Prefer using response.rowCount over response.avroRows?.rowCount (deprecated) |
| 156 | + }) |
| 157 | + .on('error', (err) => { |
| 158 | + // Handle error |
| 159 | + }) |
| 160 | + .on('end', () => { |
| 161 | + // Done |
| 162 | + }); |
| 163 | +``` |
0 commit comments