Create a write stream

Creates a write stream to append data rows to a BigQuery table. The stream is created in a PENDING state and can be used to write data. The stream must be finalized and committed before the data is visible in the table.

Code sample

Node.js

Before trying this sample, follow the Node.js setup instructions in the BigQuery quickstart using client libraries . For more information, see the BigQuery Node.js API reference documentation .

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  const 
  
 { 
  
 BigQueryWriteClient 
 , 
  
 managedwriter 
 , 
 } 
  
 = 
  
 require 
 ( 
 ' @google-cloud/bigquery-storage 
' 
 ); 
 const 
  
 { 
 status 
 } 
  
 = 
  
 require 
 ( 
 '@grpc/grpc-js' 
 ); 
 const 
  
 client 
  
 = 
  
 new 
  
  BigQueryWriteClient 
 
 (); 
 /** 
 * Creates a write stream of PENDING type to a BigQuery table. 
 * 
 * @param {string} projectId The project ID of the table, e.g. 'my-project-id'. 
 * @param {string} datasetId The dataset ID of the table, e.g. 'my_dataset'. 
 * @param {string} tableId The ID of the table to create a stream for, e.g. 'my_table'. 
 */ 
 async 
  
 function 
  
 createWriteStream 
 ( 
 projectId 
 , 
  
 datasetId 
 , 
  
 tableId 
 ) 
  
 { 
  
 try 
  
 { 
  
 const 
  
 parent 
  
 = 
  
 client 
 . 
 tablePath 
 ( 
 projectId 
 , 
  
 datasetId 
 , 
  
 tableId 
 ); 
  
 // A PENDING type stream is used for batch loads. The stream is created 
  
 // in a PENDING state and does not become visible until it is committed. 
  
 const 
  
 writeStream 
  
 = 
  
 { 
  
 type 
 : 
  
 managedwriter 
 . 
  PendingStream 
 
 , 
  
 }; 
  
 const 
  
 request 
  
 = 
  
 { 
  
 parent 
 , 
  
 writeStream 
 , 
  
 }; 
  
 const 
  
 [ 
 response 
 ] 
  
 = 
  
 await 
  
 client 
 . 
 createWriteStream 
 ( 
 request 
 ); 
  
 console 
 . 
 log 
 ( 
 'Created write stream:' 
 ); 
  
 console 
 . 
 log 
 ( 
 ` 
 ${ 
 response 
 . 
 name 
 } 
 ` 
 ); 
  
 console 
 . 
 log 
 ( 
 'Stream type:' 
 ); 
  
 console 
 . 
 log 
 ( 
 ` 
 ${ 
 response 
 . 
 type 
 } 
 ` 
 ); 
  
 } 
  
 catch 
  
 ( 
 err 
 ) 
  
 { 
  
 if 
  
 ( 
 err 
 . 
 code 
  
 === 
  
 status 
 . 
 NOT_FOUND 
 ) 
  
 { 
  
 console 
 . 
 log 
 ( 
  
 `Table 
 ${ 
 tableId 
 } 
 not found in dataset 
 ${ 
 datasetId 
 } 
 in project 
 ${ 
 projectId 
 } 
 . Please create the table before running the sample.` 
 , 
  
 ); 
  
 } 
  
 else 
  
 { 
  
 console 
 . 
 error 
 ( 
 'Error creating write stream:' 
 , 
  
 err 
 ); 
  
 } 
  
 } 
 } 
 

Python

Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries . For more information, see the BigQuery Python API reference documentation .

To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries .

  from 
  
 google.api_core.exceptions 
  
 import 
 NotFound 
 from 
  
 google.cloud.bigquery_storage_v1 
  
 import 
 BigQueryWriteClient 
 from 
  
 google.cloud.bigquery_storage_v1.types 
  
 import 
  WriteStream 
 
 client 
 = 
 BigQueryWriteClient 
 () 
 def 
  
 create_write_stream 
 ( 
 project_id 
 : 
 str 
 , 
 dataset_id 
 : 
 str 
 , 
 table_id 
 : 
 str 
 ) 
 - 
> None 
 : 
  
 """Creates a write stream to a BigQuery table. 
 A write stream is a channel that can be used to write data to a BigQuery 
 table. This sample creates a 'COMMITTED' type write stream, which means 
 that data written to the stream is immediately available for query. 
 Args: 
 project_id: The Google Cloud project ID. 
 dataset_id: The BigQuery dataset ID. 
 table_id: The BigQuery table ID. 
 """ 
 parent 
 = 
  client 
 
 . 
  table_path 
 
 ( 
 project_id 
 , 
 dataset_id 
 , 
 table_id 
 ) 
 write_stream 
 = 
 WriteStream 
 () 
 write_stream 
 . 
 type_ 
 = 
  WriteStream 
 
 . 
 Type 
 . 
 COMMITTED 
 try 
 : 
 created_stream 
 = 
  client 
 
 . 
  create_write_stream 
 
 ( 
 parent 
 = 
 parent 
 , 
 write_stream 
 = 
 write_stream 
 ) 
 print 
 ( 
 f 
 "Created write stream: 
 { 
 created_stream 
 . 
 name 
 } 
 " 
 ) 
 except 
 NotFound 
 : 
 print 
 ( 
 f 
 "Parent table not found: 
 { 
 parent 
 } 
 . Please create the table before running this sample." 
 ) 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Design a Mobile Site
View Site in Mobile | Classic
Share by: