Mendapatkan detail stream tulis

Mengambil informasi tentang aliran tulis BigQuery yang ada. Gunakan ini untuk memeriksa status dan skema aliran data sebelum menambahkan data.

Contoh kode

Java

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Java di Panduan memulai BigQuery menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi BigQuery Java API.

Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.

import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.bigquery.storage.v1.BigQueryWriteClient;
import com.google.cloud.bigquery.storage.v1.GetWriteStreamRequest;
import com.google.cloud.bigquery.storage.v1.WriteStream;
import com.google.cloud.bigquery.storage.v1.WriteStreamName;
import java.io.IOException;

public class GetWriteStream {

  /**
   * Retrieves information about a BigQuery Storage write stream.
   *
   * @param projectId The Google Cloud project ID (for example, "example-project-id").
   * @param datasetId The BigQuery dataset ID containing the table (for example,
   *     "example_dataset").
   * @param tableId The BigQuery table ID (for example, "example_table").
   */
  public static void getWriteStream(String projectId, String datasetId, String tableId) {
    // Every BigQuery table has a built-in "_default" write stream.
    String streamId = "_default";
    WriteStreamName name = WriteStreamName.of(projectId, datasetId, tableId, streamId);

    GetWriteStreamRequest request =
        GetWriteStreamRequest.newBuilder().setName(name.toString()).build();

    try (BigQueryWriteClient client = BigQueryWriteClient.create()) {
      WriteStream stream = client.getWriteStream(request);
      System.out.printf("Got write stream: %s%n", stream.getName());
      System.out.printf("Stream type: %s%n", stream.getType());
      System.out.printf("Stream location: %s%n", stream.getLocation());
    } catch (NotFoundException err) {
      System.err.printf(
          "error: write stream '%s' not found: %s%n", name, err.getMessage());
    } catch (ApiException | IOException err) {
      System.err.printf(
          "error: failed to get write stream '%s': %s%n", name, err.getMessage());
    }
  }

Node.js

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Node.js di Panduan memulai BigQuery menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi BigQuery Node.js API.

Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.

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

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Python di Panduan memulai BigQuery menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi BigQuery Python API.

Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.

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}")

Langkah berikutnya

Untuk menelusuri dan memfilter contoh kode untuk produk Google Cloud lainnya, lihat browser contohGoogle Cloud .