Class BigQueryWriteClient (2.6.5)

public class BigQueryWriteClient implements BackgroundResource

Service Description: BigQuery Write API.

The Write API can be used to write data to BigQuery.

This class provides the ability to make remote calls to the backing service through method calls that map to API methods. Sample code to get started:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   TableName parent = TableName.of("[PROJECT]", "[DATASET]", "[TABLE]");
   WriteStream writeStream = WriteStream.newBuilder().build();
   WriteStream response = bigQueryWriteClient.createWriteStream(parent, writeStream);
 }
 

Note: close() needs to be called on the BigQueryWriteClient object to clean up resources such as threads. In the example above, try-with-resources is used, which automatically calls close().

The surface of this class includes several types of Java methods for each of the API's methods:

  1. A "flattened" method. With this type of method, the fields of the request type have been converted into function parameters. It may be the case that not all fields are available as parameters, and not every API method will have a flattened method entry point.
  2. A "request object" method. This type of method only takes one parameter, a request object, which must be constructed before the call. Not every API method will have a request object method.
  3. A "callable" method. This type of method takes no parameters and returns an immutable API callable object, which can be used to initiate calls to the service.

See the individual methods for example code.

Many parameters require resource names to be formatted in a particular way. To assist with these names, this class includes a format method for each type of name, and additionally a parse method to extract the individual identifiers contained within names that are returned.

This class can be customized by passing in a custom instance of BigQueryWriteSettings to create(). For example:

To customize credentials:


 BigQueryWriteSettings bigQueryWriteSettings =
     BigQueryWriteSettings.newBuilder()
         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
         .build();
 BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create(bigQueryWriteSettings);
 

To customize the endpoint:


 BigQueryWriteSettings bigQueryWriteSettings =
     BigQueryWriteSettings.newBuilder().setEndpoint(myEndpoint).build();
 BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create(bigQueryWriteSettings);
 

Please refer to the GitHub repository's samples for more quickstart code snippets.

Inheritance

java.lang.Object > BigQueryWriteClient

Implements

BackgroundResource

Constructors

BigQueryWriteClient(BigQueryWriteSettings settings)

protected BigQueryWriteClient(BigQueryWriteSettings settings)

Constructs an instance of BigQueryWriteClient, using the given settings. This is protected so that it is easy to make a subclass, but otherwise, the static factory methods should be preferred.

Parameter
NameDescription
settingsBigQueryWriteSettings

BigQueryWriteClient(BigQueryWriteStub stub)

protected BigQueryWriteClient(BigQueryWriteStub stub)
Parameter
NameDescription
stubBigQueryWriteStub

Methods

appendRowsCallable()

public final BidiStreamingCallable<AppendRowsRequest,AppendRowsResponse> appendRowsCallable()

Appends data to the given stream.

If offset is specified, the offset is checked against the end of stream. The server returns OUT_OF_RANGE in AppendRowsResponse if an attempt is made to append to an offset beyond the current end of the stream or ALREADY_EXISTS if user provids an offset that has already been written to. User can retry with adjusted offset within the same RPC stream. If offset is not specified, append happens at the end of the stream.

The response contains the offset at which the append happened. Responses are received in the same order in which requests are sent. There will be one response for each successful request. If the offset is not set in response, it means append didn't happen due to some errors. If one request fails, all the subsequent requests will also fail until a success request is made again.

If the stream is of PENDING type, data will only be available for read operations after the stream is committed.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   BidiStream
Returns
TypeDescription
BidiStreamingCallable<AppendRowsRequest,AppendRowsResponse>

awaitTermination(long duration, TimeUnit unit)

public boolean awaitTermination(long duration, TimeUnit unit)
Parameters
NameDescription
durationlong
unitTimeUnit
Returns
TypeDescription
boolean
Exceptions
TypeDescription
InterruptedException

batchCommitWriteStreams(BatchCommitWriteStreamsRequest request)

public final BatchCommitWriteStreamsResponse batchCommitWriteStreams(BatchCommitWriteStreamsRequest request)

Atomically commits a group of PENDING streams that belong to the same parent table. Streams must be finalized before commit and cannot be committed multiple times. Once a stream is committed, data in the stream becomes available for read operations.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   BatchCommitWriteStreamsRequest request =
       BatchCommitWriteStreamsRequest.newBuilder()
           .setParent("parent-995424086")
           .addAllWriteStreams(new ArrayList
Parameter
NameDescription
requestBatchCommitWriteStreamsRequest

The request object containing all of the parameters for the API call.

Returns
TypeDescription
BatchCommitWriteStreamsResponse

batchCommitWriteStreams(String parent)

public final BatchCommitWriteStreamsResponse batchCommitWriteStreams(String parent)

Atomically commits a group of PENDING streams that belong to the same parent table. Streams must be finalized before commit and cannot be committed multiple times. Once a stream is committed, data in the stream becomes available for read operations.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   String parent = "parent-995424086";
   BatchCommitWriteStreamsResponse response =
       bigQueryWriteClient.batchCommitWriteStreams(parent);
 }
 
Parameter
NameDescription
parentString

Required. Parent table that all the streams should belong to, in the form of projects/{project}/datasets/{dataset}/tables/{table}.

Returns
TypeDescription
BatchCommitWriteStreamsResponse

batchCommitWriteStreamsCallable()

public final UnaryCallable<BatchCommitWriteStreamsRequest,BatchCommitWriteStreamsResponse> batchCommitWriteStreamsCallable()

Atomically commits a group of PENDING streams that belong to the same parent table. Streams must be finalized before commit and cannot be committed multiple times. Once a stream is committed, data in the stream becomes available for read operations.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   BatchCommitWriteStreamsRequest request =
       BatchCommitWriteStreamsRequest.newBuilder()
           .setParent("parent-995424086")
           .addAllWriteStreams(new ArrayList
Returns
TypeDescription
UnaryCallable<BatchCommitWriteStreamsRequest,BatchCommitWriteStreamsResponse>

close()

public final void close()

create()

public static final BigQueryWriteClient create()

Constructs an instance of BigQueryWriteClient with default settings.

Returns
TypeDescription
BigQueryWriteClient
Exceptions
TypeDescription
IOException

create(BigQueryWriteSettings settings)

public static final BigQueryWriteClient create(BigQueryWriteSettings settings)

Constructs an instance of BigQueryWriteClient, using the given settings. The channels are created based on the settings passed in, or defaults for any settings that are not set.

Parameter
NameDescription
settingsBigQueryWriteSettings
Returns
TypeDescription
BigQueryWriteClient
Exceptions
TypeDescription
IOException

create(BigQueryWriteStub stub)

public static final BigQueryWriteClient create(BigQueryWriteStub stub)

Constructs an instance of BigQueryWriteClient, using the given stub for making calls. This is for advanced usage - prefer using create(BigQueryWriteSettings).

Parameter
NameDescription
stubBigQueryWriteStub
Returns
TypeDescription
BigQueryWriteClient

createWriteStream(CreateWriteStreamRequest request)

public final WriteStream createWriteStream(CreateWriteStreamRequest request)

Creates a write stream to the given table. Additionally, every table has a special COMMITTED stream named '_default' to which data can be written. This stream doesn't need to be created using CreateWriteStream. It is a stream that can be used simultaneously by any number of clients. Data written to this stream is considered committed as soon as an acknowledgement is received.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   CreateWriteStreamRequest request =
       CreateWriteStreamRequest.newBuilder()
           .setParent(TableName.of("[PROJECT]", "[DATASET]", "[TABLE]").toString())
           .setWriteStream(WriteStream.newBuilder().build())
           .build();
   WriteStream response = bigQueryWriteClient.createWriteStream(request);
 }
 
Parameter
NameDescription
requestCreateWriteStreamRequest

The request object containing all of the parameters for the API call.

Returns
TypeDescription
WriteStream

createWriteStream(TableName parent, WriteStream writeStream)

public final WriteStream createWriteStream(TableName parent, WriteStream writeStream)

Creates a write stream to the given table. Additionally, every table has a special COMMITTED stream named '_default' to which data can be written. This stream doesn't need to be created using CreateWriteStream. It is a stream that can be used simultaneously by any number of clients. Data written to this stream is considered committed as soon as an acknowledgement is received.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   TableName parent = TableName.of("[PROJECT]", "[DATASET]", "[TABLE]");
   WriteStream writeStream = WriteStream.newBuilder().build();
   WriteStream response = bigQueryWriteClient.createWriteStream(parent, writeStream);
 }
 
Parameters
NameDescription
parentTableName

Required. Reference to the table to which the stream belongs, in the format of projects/{project}/datasets/{dataset}/tables/{table}.

writeStreamWriteStream

Required. Stream to be created.

Returns
TypeDescription
WriteStream

createWriteStream(String parent, WriteStream writeStream)

public final WriteStream createWriteStream(String parent, WriteStream writeStream)

Creates a write stream to the given table. Additionally, every table has a special COMMITTED stream named '_default' to which data can be written. This stream doesn't need to be created using CreateWriteStream. It is a stream that can be used simultaneously by any number of clients. Data written to this stream is considered committed as soon as an acknowledgement is received.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   String parent = TableName.of("[PROJECT]", "[DATASET]", "[TABLE]").toString();
   WriteStream writeStream = WriteStream.newBuilder().build();
   WriteStream response = bigQueryWriteClient.createWriteStream(parent, writeStream);
 }
 
Parameters
NameDescription
parentString

Required. Reference to the table to which the stream belongs, in the format of projects/{project}/datasets/{dataset}/tables/{table}.

writeStreamWriteStream

Required. Stream to be created.

Returns
TypeDescription
WriteStream

createWriteStreamCallable()

public final UnaryCallable<CreateWriteStreamRequest,WriteStream> createWriteStreamCallable()

Creates a write stream to the given table. Additionally, every table has a special COMMITTED stream named '_default' to which data can be written. This stream doesn't need to be created using CreateWriteStream. It is a stream that can be used simultaneously by any number of clients. Data written to this stream is considered committed as soon as an acknowledgement is received.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   CreateWriteStreamRequest request =
       CreateWriteStreamRequest.newBuilder()
           .setParent(TableName.of("[PROJECT]", "[DATASET]", "[TABLE]").toString())
           .setWriteStream(WriteStream.newBuilder().build())
           .build();
   ApiFuture
Returns
TypeDescription
UnaryCallable<CreateWriteStreamRequest,WriteStream>

finalizeWriteStream(FinalizeWriteStreamRequest request)

public final FinalizeWriteStreamResponse finalizeWriteStream(FinalizeWriteStreamRequest request)

Finalize a write stream so that no new data can be appended to the stream. Finalize is not supported on the '_default' stream.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   FinalizeWriteStreamRequest request =
       FinalizeWriteStreamRequest.newBuilder()
           .setName(
               WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]").toString())
           .build();
   FinalizeWriteStreamResponse response = bigQueryWriteClient.finalizeWriteStream(request);
 }
 
Parameter
NameDescription
requestFinalizeWriteStreamRequest

The request object containing all of the parameters for the API call.

Returns
TypeDescription
FinalizeWriteStreamResponse

finalizeWriteStream(WriteStreamName name)

public final FinalizeWriteStreamResponse finalizeWriteStream(WriteStreamName name)

Finalize a write stream so that no new data can be appended to the stream. Finalize is not supported on the '_default' stream.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   WriteStreamName name = WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]");
   FinalizeWriteStreamResponse response = bigQueryWriteClient.finalizeWriteStream(name);
 }
 
Parameter
NameDescription
nameWriteStreamName

Required. Name of the stream to finalize, in the form of projects/{project}/datasets/{dataset}/tables/{table}/streams/{stream}.

Returns
TypeDescription
FinalizeWriteStreamResponse

finalizeWriteStream(String name)

public final FinalizeWriteStreamResponse finalizeWriteStream(String name)

Finalize a write stream so that no new data can be appended to the stream. Finalize is not supported on the '_default' stream.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   String name = WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]").toString();
   FinalizeWriteStreamResponse response = bigQueryWriteClient.finalizeWriteStream(name);
 }
 
Parameter
NameDescription
nameString

Required. Name of the stream to finalize, in the form of projects/{project}/datasets/{dataset}/tables/{table}/streams/{stream}.

Returns
TypeDescription
FinalizeWriteStreamResponse

finalizeWriteStreamCallable()

public final UnaryCallable<FinalizeWriteStreamRequest,FinalizeWriteStreamResponse> finalizeWriteStreamCallable()

Finalize a write stream so that no new data can be appended to the stream. Finalize is not supported on the '_default' stream.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   FinalizeWriteStreamRequest request =
       FinalizeWriteStreamRequest.newBuilder()
           .setName(
               WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]").toString())
           .build();
   ApiFuture
Returns
TypeDescription
UnaryCallable<FinalizeWriteStreamRequest,FinalizeWriteStreamResponse>

flushRows(FlushRowsRequest request)

public final FlushRowsResponse flushRows(FlushRowsRequest request)

Flushes rows to a BUFFERED stream. If users are appending rows to BUFFERED stream, flush operation is required in order for the rows to become available for reading. A Flush operation flushes up to any previously flushed offset in a BUFFERED stream, to the offset specified in the request. Flush is not supported on the _default stream, since it is not BUFFERED.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   FlushRowsRequest request =
       FlushRowsRequest.newBuilder()
           .setWriteStream(
               WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]").toString())
           .setOffset(Int64Value.newBuilder().build())
           .build();
   FlushRowsResponse response = bigQueryWriteClient.flushRows(request);
 }
 
Parameter
NameDescription
requestFlushRowsRequest

The request object containing all of the parameters for the API call.

Returns
TypeDescription
FlushRowsResponse

flushRows(WriteStreamName writeStream)

public final FlushRowsResponse flushRows(WriteStreamName writeStream)

Flushes rows to a BUFFERED stream. If users are appending rows to BUFFERED stream, flush operation is required in order for the rows to become available for reading. A Flush operation flushes up to any previously flushed offset in a BUFFERED stream, to the offset specified in the request. Flush is not supported on the _default stream, since it is not BUFFERED.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   WriteStreamName writeStream =
       WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]");
   FlushRowsResponse response = bigQueryWriteClient.flushRows(writeStream);
 }
 
Parameter
NameDescription
writeStreamWriteStreamName

Required. The stream that is the target of the flush operation.

Returns
TypeDescription
FlushRowsResponse

flushRows(String writeStream)

public final FlushRowsResponse flushRows(String writeStream)

Flushes rows to a BUFFERED stream. If users are appending rows to BUFFERED stream, flush operation is required in order for the rows to become available for reading. A Flush operation flushes up to any previously flushed offset in a BUFFERED stream, to the offset specified in the request. Flush is not supported on the _default stream, since it is not BUFFERED.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   String writeStream =
       WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]").toString();
   FlushRowsResponse response = bigQueryWriteClient.flushRows(writeStream);
 }
 
Parameter
NameDescription
writeStreamString

Required. The stream that is the target of the flush operation.

Returns
TypeDescription
FlushRowsResponse

flushRowsCallable()

public final UnaryCallable<FlushRowsRequest,FlushRowsResponse> flushRowsCallable()

Flushes rows to a BUFFERED stream. If users are appending rows to BUFFERED stream, flush operation is required in order for the rows to become available for reading. A Flush operation flushes up to any previously flushed offset in a BUFFERED stream, to the offset specified in the request. Flush is not supported on the _default stream, since it is not BUFFERED.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   FlushRowsRequest request =
       FlushRowsRequest.newBuilder()
           .setWriteStream(
               WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]").toString())
           .setOffset(Int64Value.newBuilder().build())
           .build();
   ApiFuture
Returns
TypeDescription
UnaryCallable<FlushRowsRequest,FlushRowsResponse>

getSettings()

public final BigQueryWriteSettings getSettings()
Returns
TypeDescription
BigQueryWriteSettings

getStub()

public BigQueryWriteStub getStub()
Returns
TypeDescription
BigQueryWriteStub

getWriteStream(GetWriteStreamRequest request)

public final WriteStream getWriteStream(GetWriteStreamRequest request)

Gets a write stream.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   GetWriteStreamRequest request =
       GetWriteStreamRequest.newBuilder()
           .setName(
               WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]").toString())
           .build();
   WriteStream response = bigQueryWriteClient.getWriteStream(request);
 }
 
Parameter
NameDescription
requestGetWriteStreamRequest

The request object containing all of the parameters for the API call.

Returns
TypeDescription
WriteStream

getWriteStream(WriteStreamName name)

public final WriteStream getWriteStream(WriteStreamName name)

Gets a write stream.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   WriteStreamName name = WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]");
   WriteStream response = bigQueryWriteClient.getWriteStream(name);
 }
 
Parameter
NameDescription
nameWriteStreamName

Required. Name of the stream to get, in the form of projects/{project}/datasets/{dataset}/tables/{table}/streams/{stream}.

Returns
TypeDescription
WriteStream

getWriteStream(String name)

public final WriteStream getWriteStream(String name)

Gets a write stream.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   String name = WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]").toString();
   WriteStream response = bigQueryWriteClient.getWriteStream(name);
 }
 
Parameter
NameDescription
nameString

Required. Name of the stream to get, in the form of projects/{project}/datasets/{dataset}/tables/{table}/streams/{stream}.

Returns
TypeDescription
WriteStream

getWriteStreamCallable()

public final UnaryCallable<GetWriteStreamRequest,WriteStream> getWriteStreamCallable()

Gets a write stream.

Sample code:


 try (BigQueryWriteClient bigQueryWriteClient = BigQueryWriteClient.create()) {
   GetWriteStreamRequest request =
       GetWriteStreamRequest.newBuilder()
           .setName(
               WriteStreamName.of("[PROJECT]", "[DATASET]", "[TABLE]", "[STREAM]").toString())
           .build();
   ApiFuture
Returns
TypeDescription
UnaryCallable<GetWriteStreamRequest,WriteStream>

isShutdown()

public boolean isShutdown()
Returns
TypeDescription
boolean

isTerminated()

public boolean isTerminated()
Returns
TypeDescription
boolean

shutdown()

public void shutdown()

shutdownNow()

public void shutdownNow()