書き込みストリームの詳細を取得する

既存の 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 サンプル ブラウザをご覧ください。