Retrieves information about an existing BigQuery write stream. Use this to check the state and schema of a stream before appending data.

## Code sample

### Java


Before trying this sample, follow the Java setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/overview).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    import com.google.api.gax.rpc.https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ApiException.html;
    import com.google.api.gax.rpc.https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.NotFoundException.html;
    import com.google.cloud.bigquery.storage.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.BigQueryWriteClient.html;
    import com.google.cloud.bigquery.storage.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.GetWriteStreamRequest.html;
    import com.google.cloud.bigquery.storage.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.WriteStream.html;
    import com.google.cloud.bigquery.storage.v1.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.WriteStreamName.html;
    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";
        https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.WriteStreamName.html name = https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.WriteStreamName.html.of(projectId, datasetId, tableId, streamId);

        https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.GetWriteStreamRequest.html request =
            https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.GetWriteStreamRequest.html.newBuilder().setName(name.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.WriteStreamName.html#com_google_cloud_bigquery_storage_v1_WriteStreamName_toString__()).build();

        try (https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.BigQueryWriteClient.html client = https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.BigQueryWriteClient.html.create()) {
          https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.WriteStream.html stream = client.getWriteStream(request);
          System.out.printf("Got write stream: %s%n", https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ServerStream.html#com_google_api_gax_rpc_ServerStream_stream__.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.WriteStream.html#com_google_cloud_bigquery_storage_v1_WriteStream_getName__());
          System.out.printf("Stream type: %s%n", https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ServerStream.html#com_google_api_gax_rpc_ServerStream_stream__.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.WriteStream.html#com_google_cloud_bigquery_storage_v1_WriteStream_getType__());
          System.out.printf("Stream location: %s%n", https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ServerStream.html#com_google_api_gax_rpc_ServerStream_stream__.https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquerystorage/latest/com.google.cloud.bigquery.storage.v1.WriteStream.html#com_google_cloud_bigquery_storage_v1_WriteStream_getLocation__());
        } catch (https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.NotFoundException.html err) {
          System.err.printf(
              "error: write stream '%s' not found: %s%n", name, err.getMessage());
        } catch (https://docs.cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.rpc.ApiException.html | IOException err) {
          System.err.printf(
              "error: failed to get write stream '%s': %s%n", name, err.getMessage());
        }
      }

### Node.js


Before trying this sample, follow the Node.js setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Node.js API
reference documentation](https://googleapis.dev/nodejs/bigquery/latest/index.html).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    const {BigQueryWriteClient} = require('https://docs.cloud.google.com/nodejs/docs/reference/bigquery-storage/latest/overview.html');
    const {status} = require('@grpc/grpc-js');

    const client = new https://docs.cloud.google.com/nodejs/docs/reference/bigquery-storage-api/latest/overview.html();

    /**
     * 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 = "https://docs.cloud.google.com/nodejs/docs/reference/bigquery-storage-api/latest/overview.html";
      const name = client.writeStreamPath(projectId, datasetId, tableId, streamId);

      const request = {
        name,
      };

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

        console.log(`Got write stream: ${https://docs.cloud.google.com/nodejs/docs/reference/bigquery-storage-api/latest/bigquery-storage-api/protos.google.cloud.bigquery.storage.v1.appendrowsresponse-class.html.name}`);
        console.log(`Stream type: ${https://docs.cloud.google.com/nodejs/docs/reference/bigquery-storage-api/latest/bigquery-storage-api/protos.google.cloud.bigquery.storage.v1.appendrowsresponse-class.html.type}`);
        console.log(`Stream location: ${https://docs.cloud.google.com/nodejs/docs/reference/bigquery-storage-api/latest/bigquery-storage-api/protos.google.cloud.bigquery.storage.v1.appendrowsresponse-class.html.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


Before trying this sample, follow the Python setup instructions in the
[BigQuery quickstart using
client libraries](https://docs.cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries).


For more information, see the
[BigQuery Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/bigquery/latest).


To authenticate to BigQuery, set up Application Default Credentials.
For more information, see

[Set up authentication for client libraries](https://docs.cloud.google.com/bigquery/docs/authentication#client-libs).

    import google.api_core.exceptions
    from google.cloud import https://docs.cloud.google.com/python/docs/reference/bigquerystorage/latest


    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 = https://docs.cloud.google.com/python/docs/reference/bigquerystorage/latest.BigQueryWriteClient()
        stream_name = (
            f"projects/{project_id}/datasets/{dataset_id}"
            f"/tables/{table_id}/streams/{stream_id}"
        )

        try:
            stream = https://docs.cloud.google.com/python/docs/reference/bigquerystorage/latest/google.cloud.bigquery_storage_v1.client.html.https://docs.cloud.google.com/python/docs/reference/bigquerystorage/latest/google.cloud.bigquery_storage_v1.services.big_query_write.BigQueryWriteClient.html#google_cloud_bigquery_storage_v1_services_big_query_write_BigQueryWriteClient_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}")

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=bigquery).