Class BulkWriter (3.8.2)

public final class BulkWriter implements AutoCloseable

A Firestore BulkWriter that can be used to perform a large number of writes in parallel.

Inheritance

Object > BulkWriter

Implements

AutoCloseable

Static Fields

MAX_BATCH_SIZE

public static final int MAX_BATCH_SIZE

The maximum number of writes that can be in a single batch.

Field Value
TypeDescription
int

MAX_RETRY_ATTEMPTS

public static final int MAX_RETRY_ATTEMPTS

The maximum number of retries that will be attempted with backoff before stopping all retry attempts.

Field Value
TypeDescription
int

RETRY_MAX_BATCH_SIZE

public static final int RETRY_MAX_BATCH_SIZE

The maximum number of writes that can be in a batch containing retries.

Field Value
TypeDescription
int

Methods

addWriteErrorListener(BulkWriter.WriteErrorCallback onError)

public void addWriteErrorListener(BulkWriter.WriteErrorCallback onError)

Attaches an error handler listener that is run every time a BulkWriter operation fails.

BulkWriter has a default error handler that retries UNAVAILABLE and ABORTED errors up to a maximum of 10 failed attempts. When an error handler is specified, the default error handler will be overwritten.

For example, see the sample code: BulkWriter bulkWriter = firestore.bulkWriter(); bulkWriter.addWriteErrorListener( (BulkWriterException error) -> { if (error.getStatus() == Status.UNAVAILABLE && error.getFailedAttempts() < MAX_RETRY_ATTEMPTS) { return true; } else { System.out.println("Failed write at document: " + error.getDocumentReference()); return false; } } );

Parameter
NameDescription
onErrorBulkWriter.WriteErrorCallback

A callback to be called every time a BulkWriter operation fails. Returning true will retry the operation. Returning false will stop the retry loop.

addWriteErrorListener(Executor executor, BulkWriter.WriteErrorCallback onError)

public void addWriteErrorListener(Executor executor, BulkWriter.WriteErrorCallback onError)

Attaches an error handler listener that is run every time a BulkWriter operation fails.

The executor cannot be changed once writes have been enqueued onto the BulkWriter.

BulkWriter has a default error handler that retries UNAVAILABLE and ABORTED errors up to a maximum of 10 failed attempts. When an error handler is specified, the default error handler will be overwritten.

For example, see the sample code: BulkWriter bulkWriter = firestore.bulkWriter(); bulkWriter.addWriteErrorListener( (BulkWriterException error) -> { if (error.getStatus() == Status.UNAVAILABLE && error.getFailedAttempts() < MAX_RETRY_ATTEMPTS) { return true; } else { System.out.println("Failed write at document: " + error.getDocumentReference()); return false; } } );

Parameters
NameDescription
executorExecutor

The executor to run the provided callback on.

onErrorBulkWriter.WriteErrorCallback

A callback to be called every time a BulkWriter operation fails. Returning true will retry the operation. Returning false will stop the retry loop.

addWriteResultListener(BulkWriter.WriteResultCallback writeResultCallback)

public void addWriteResultListener(BulkWriter.WriteResultCallback writeResultCallback)

Attaches a listener that is run every time a BulkWriter operation successfully completes. The listener will be run before close() completes.

For example, see the sample code: BulkWriter bulkWriter = firestore.bulkWriter(); bulkWriter.addWriteResultListener( (DocumentReference documentReference, WriteResult result) -> { System.out.println( "Successfully executed write on document: "

  • documentReference
  • " at: "
  • result.getUpdateTime()); } );
Parameter
NameDescription
writeResultCallbackBulkWriter.WriteResultCallback

A callback to be called every time a BulkWriter operation successfully completes.

addWriteResultListener(Executor executor, BulkWriter.WriteResultCallback writeResultCallback)

public void addWriteResultListener(Executor executor, BulkWriter.WriteResultCallback writeResultCallback)

Attaches a listener that is run every time a BulkWriter operation successfully completes.

The executor cannot be changed once writes have been enqueued onto the BulkWriter.

For example, see the sample code: BulkWriter bulkWriter = firestore.bulkWriter(); bulkWriter.addWriteResultListener( (DocumentReference documentReference, WriteResult result) -> { System.out.println( "Successfully executed write on document: "

  • documentReference
  • " at: "
  • result.getUpdateTime()); } );
Parameters
NameDescription
executorExecutor

The executor to run the provided callback on.

writeResultCallbackBulkWriter.WriteResultCallback

A callback to be called every time a BulkWriter operation successfully completes.

close()

public void close()

Commits all enqueued writes and marks the BulkWriter instance as closed.

After calling close(), calling any method will return an error. Any retries scheduled with addWriteErrorListener() will be run before close() completes.

This method completes when there are no more pending writes. Calling this method will send all requests.

Exceptions
TypeDescription
InterruptedException
ExecutionException

create(DocumentReference documentReference, Object pojo)

public ApiFuture<WriteResult> create(DocumentReference documentReference, Object pojo)

Create a document with the provided data. This single operation will fail if a document exists at its location.

Parameters
NameDescription
documentReferenceDocumentReference

A reference to the document to be created.

pojoObject

The POJO that will be used to populate the document contents.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing the result of the write. Contains a BulkWriterException if the write fails.

create(DocumentReference documentReference, Map<String,Object> fields)

public ApiFuture<WriteResult> create(DocumentReference documentReference, Map<String,Object> fields)

Create a document with the provided data. This single operation will fail if a document exists at its location.

Parameters
NameDescription
documentReferenceDocumentReference

A reference to the document to be created.

fieldsMap<String,Object>

A map of the fields and values for the document.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing the result of the write. Contains a BulkWriterException if the write fails.

delete(DocumentReference documentReference)

public ApiFuture<WriteResult> delete(DocumentReference documentReference)

Delete a document from the database.

Parameter
NameDescription
documentReferenceDocumentReference

The DocumentReference to delete.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing a sentinel value (Timestamp(0)) for the delete operation. Contains a BulkWriterException if the delete fails.

delete(DocumentReference documentReference, Precondition precondition)

public ApiFuture<WriteResult> delete(DocumentReference documentReference, Precondition precondition)

Delete a document from the database.

Parameters
NameDescription
documentReferenceDocumentReference

The DocumentReference to delete.

preconditionPrecondition

Precondition to enforce for this delete.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing a sentinel value (Timestamp(0)) for the delete operation. Contains a BulkWriterException if the delete fails.

flush()

public ApiFuture<Void> flush()

Commits all writes that have been enqueued up to this point in parallel.

Returns an ApiFuture that completes when all currently queued operations have been committed. The ApiFuture will never return an error since the results for each individual operation are conveyed via their individual ApiFutures.

The ApiFuture completes immediately if there are no pending writes. Otherwise, the ApiFuture waits for all previously issued writes, but it does not wait for writes that were added after the method is called. If you want to wait for additional writes, call flush() again.

Returns
TypeDescription
ApiFuture<Void>

An ApiFuture that completes when all enqueued writes up to this point have been committed.

set(DocumentReference documentReference, Object pojo)

public ApiFuture<WriteResult> set(DocumentReference documentReference, Object pojo)

Write to the document referred to by the provided DocumentReference. If the document does not exist yet, it will be created.

Parameters
NameDescription
documentReferenceDocumentReference

A reference to the document to be set.

pojoObject

The POJO that will be used to populate the document contents.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing the result of the write. Contains a BulkWriterException if the write fails.

set(DocumentReference documentReference, Object pojo, SetOptions options)

public ApiFuture<WriteResult> set(DocumentReference documentReference, Object pojo, SetOptions options)

Write to the document referred to by the provided DocumentReference. If the document does not exist yet, it will be created. If you pass a SetOptions, the provided data can be merged into an existing document.

Parameters
NameDescription
documentReferenceDocumentReference

A reference to the document to be set.

pojoObject

The POJO that will be used to populate the document contents.

optionsSetOptions

An object to configure the set behavior.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing the result of the write. Contains a BulkWriterException if the write fails.

set(DocumentReference documentReference, Map<String,Object> fields)

public ApiFuture<WriteResult> set(DocumentReference documentReference, Map<String,Object> fields)

Write to the document referred to by the provided DocumentReference. If the document does not exist yet, it will be created.

Parameters
NameDescription
documentReferenceDocumentReference

A reference to the document to be set.

fieldsMap<String,Object>

A map of the fields and values for the document.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing the result of the write. Contains a BulkWriterException if the write fails.

set(DocumentReference documentReference, Map<String,Object> fields, SetOptions options)

public ApiFuture<WriteResult> set(DocumentReference documentReference, Map<String,Object> fields, SetOptions options)

Write to the document referred to by the provided DocumentReference. If the document does not exist yet, it will be created. If you pass a SetOptions, the provided data can be merged into an existing document.

Parameters
NameDescription
documentReferenceDocumentReference

A reference to the document to be set.

fieldsMap<String,Object>

A map of the fields and values for the document.

optionsSetOptions

An object to configure the set behavior.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing the result of the write. Contains a BulkWriterException if the write fails.

update(DocumentReference documentReference, FieldPath fieldPath, Object value, Object[] moreFieldsAndValues)

public ApiFuture<WriteResult> update(DocumentReference documentReference, FieldPath fieldPath, Object value, Object[] moreFieldsAndValues)

Update fields of the document referred to by the provided DocumentReference. If the document doesn't yet exist, the update will fail.

The update() method accepts either an object with field paths encoded as keys and field values encoded as values, or a variable number of arguments that alternate between field paths and field values. Nested fields can be updated by providing dot-separated field path strings or by providing FieldPath objects.

Parameters
NameDescription
documentReferenceDocumentReference

A reference to the document to be updated.

fieldPathFieldPath

The first field to set.

valueObject

The first value to set.

moreFieldsAndValuesObject[]

String and Object pairs with more fields to be set.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing the result of the write. Contains a BulkWriterException if the write fails.

update(DocumentReference documentReference, Precondition precondition, FieldPath fieldPath, Object value, Object[] moreFieldsAndValues)

public ApiFuture<WriteResult> update(DocumentReference documentReference, Precondition precondition, FieldPath fieldPath, Object value, Object[] moreFieldsAndValues)

Update fields of the document referred to by the provided DocumentReference. If the document doesn't yet exist, the update will fail.

The update() method accepts either an object with field paths encoded as keys and field values encoded as values, or a variable number of arguments that alternate between field paths and field values. Nested fields can be updated by providing dot-separated field path strings or by providing FieldPath objects.

Parameters
NameDescription
documentReferenceDocumentReference

A reference to the document to be updated.

preconditionPrecondition

Precondition to enforce on this update.

fieldPathFieldPath

The first field to set.

valueObject

The first value to set.

moreFieldsAndValuesObject[]

String and Object pairs with more fields to be set.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing the result of the write. Contains a BulkWriterException if the write fails.

update(DocumentReference documentReference, Precondition precondition, String field, Object value, Object[] moreFieldsAndValues)

public ApiFuture<WriteResult> update(DocumentReference documentReference, Precondition precondition, String field, Object value, Object[] moreFieldsAndValues)

Update fields of the document referred to by the provided DocumentReference. If the document doesn't yet exist, the update will fail.

The update() method accepts either an object with field paths encoded as keys and field values encoded as values, or a variable number of arguments that alternate between field paths and field values. Nested fields can be updated by providing dot-separated field path strings or by providing FieldPath objects.

Parameters
NameDescription
documentReferenceDocumentReference

A reference to the document to be updated.

preconditionPrecondition
fieldString

The first field to set.

valueObject

The first value to set.

moreFieldsAndValuesObject[]

String and Object pairs with more fields to be set.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing the result of the write. Contains a BulkWriterException if the write fails.

update(DocumentReference documentReference, String field, Object value, Object[] moreFieldsAndValues)

public ApiFuture<WriteResult> update(DocumentReference documentReference, String field, Object value, Object[] moreFieldsAndValues)

Update fields of the document referred to by the provided DocumentReference. If the document doesn't yet exist, the update will fail.

The update() method accepts either an object with field paths encoded as keys and field values encoded as values, or a variable number of arguments that alternate between field paths and field values. Nested fields can be updated by providing dot-separated field path strings or by providing FieldPath objects.

Parameters
NameDescription
documentReferenceDocumentReference

A reference to the document to be updated.

fieldString

The first field to set.

valueObject

The first value to set.

moreFieldsAndValuesObject[]

String and Object pairs with more fields to be set.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing the result of the write. Contains a BulkWriterException if the write fails.

update(DocumentReference documentReference, Map<String,Object> fields)

public ApiFuture<WriteResult> update(DocumentReference documentReference, Map<String,Object> fields)

Update fields of the document referred to by the provided DocumentReference. If the document doesn't yet exist, the update will fail.

The update() method accepts either an object with field paths encoded as keys and field values encoded as values, or a variable number of arguments that alternate between field paths and field values. Nested fields can be updated by providing dot-separated field path strings or by providing FieldPath objects.

Parameters
NameDescription
documentReferenceDocumentReference

A reference to the document to be updated.

fieldsMap<String,Object>

A map of the fields and values for the document.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing the result of the write. Contains a BulkWriterException if the write fails.

update(DocumentReference documentReference, Map<String,Object> fields, Precondition precondition)

public ApiFuture<WriteResult> update(DocumentReference documentReference, Map<String,Object> fields, Precondition precondition)

Update fields of the document referred to by the provided DocumentReference. If the document doesn't yet exist, the update will fail.

The update() method accepts either an object with field paths encoded as keys and field values encoded as values, or a variable number of arguments that alternate between field paths and field values. Nested fields can be updated by providing dot-separated field path strings or by providing FieldPath objects.

Parameters
NameDescription
documentReferenceDocumentReference

A reference to the document to be updated.

fieldsMap<String,Object>

A map of the fields and values for the document.

preconditionPrecondition

Precondition to enforce on this update.

Returns
TypeDescription
ApiFuture<WriteResult>

An ApiFuture containing the result of the write. Contains a BulkWriterException if the write fails.