取得寫入串流詳細資料

擷取現有 BigQuery 寫入串流的相關資訊。您可以使用這項功能檢查串流的狀態和結構定義,再附加資料。

程式碼範例

Node.js

在試用這個範例之前,請先按照「使用用戶端程式庫的 BigQuery 快速入門導覽課程」中的 Node.js 設定說明操作。詳情請參閱 BigQuery Node.js API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證作業」。

const {BigQueryWriteClient} = require('@google-cloud/bigquery-storage');
const {status} = require('@grpc/grpc-js');

const client = new BigQueryWriteClient();

/**
 * Gets information about a single write stream.
 *
 * @param {string} projectId The project ID of the table. e.g. 'my-project'
 * @param {string} datasetId The dataset ID of the table. e.g. 'my_dataset'
 * @param {string} tableId The ID of the table. e.g. 'my_table'
 */
async function getWriteStream(projectId, datasetId, tableId) {

  const streamId = "_default";
  const name = client.writeStreamPath(projectId, datasetId, tableId, streamId);

  const request = {
    name,
  };

  try {
    const [response] = await client.getWriteStream(request);

    console.log(`Got write stream: ${response.name}`);
    console.log(`Stream type: ${response.type}`);
    console.log(`Stream location: ${response.location}`);
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.log(`Write stream ${name} not found.`);
    } else {
      console.error('Error getting write stream:', err);
    }
  }
}

Python

在試用這個範例之前,請先按照「使用用戶端程式庫的 BigQuery 快速入門導覽課程」中的 Python 設定說明操作。詳情請參閱 BigQuery Python API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證作業」。

import google.api_core.exceptions
from google.cloud import bigquery_storage_v1


def get_write_stream(
    project_id: str, dataset_id: str, table_id: str, stream_id: str
) -> None:
    """Gets information about a single write stream.

    Args:
        project_id: The project id.
        dataset_id: The dataset id
        table_id: The table id
        stream_id: The stream id
    """
    client = bigquery_storage_v1.BigQueryWriteClient()
    stream_name = (
        f"projects/{project_id}/datasets/{dataset_id}"
        f"/tables/{table_id}/streams/{stream_id}"
    )

    try:
        stream = client.get_write_stream(name=stream_name)

        print(f"Got write stream: {stream.name}")
        print(f"Stream type: {stream.Type(stream.type_).name}")

    except google.api_core.exceptions.NotFound:
        print(f"Write stream not found: {stream_name}")

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 瀏覽器範例