Class FirestoreClient (3.7.3)

Stay organized with collections Save and categorize content based on your preferences.
public class FirestoreClient implements BackgroundResource

Service Description: The Cloud Firestore service.

Cloud Firestore is a fast, fully managed, serverless, cloud-native NoSQL document database that simplifies storing, syncing, and querying data for your mobile, web, and IoT apps at global scale. Its client libraries provide live synchronization and offline support, while its security features and integrations with Firebase and Google Cloud Platform (GCP) accelerate building truly serverless apps.

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:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (FirestoreClient firestoreClient = FirestoreClient.create()) {
   GetDocumentRequest request =
       GetDocumentRequest.newBuilder()
           .setName("name3373707")
           .setMask(DocumentMask.newBuilder().build())
           .build();
   Document response = firestoreClient.getDocument(request);
 }
 

Note: close() needs to be called on the FirestoreClient 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 FirestoreSettings to create(). For example:

To customize credentials:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 FirestoreSettings firestoreSettings =
     FirestoreSettings.newBuilder()
         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
         .build();
 FirestoreClient firestoreClient = FirestoreClient.create(firestoreSettings);
 

To customize the endpoint:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 FirestoreSettings firestoreSettings =
     FirestoreSettings.newBuilder().setEndpoint(myEndpoint).build();
 FirestoreClient firestoreClient = FirestoreClient.create(firestoreSettings);
 

To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over the wire:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 FirestoreSettings firestoreSettings = FirestoreSettings.newHttpJsonBuilder().build();
 FirestoreClient firestoreClient = FirestoreClient.create(firestoreSettings);
 

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

Inheritance

java.lang.Object > FirestoreClient

Implements

BackgroundResource

Static Methods

create()

public static final FirestoreClient create()

Constructs an instance of FirestoreClient with default settings.

Returns
TypeDescription
FirestoreClient
Exceptions
TypeDescription
IOException

create(FirestoreSettings settings)

public static final FirestoreClient create(FirestoreSettings settings)

Constructs an instance of FirestoreClient, 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
settingsFirestoreSettings
Returns
TypeDescription
FirestoreClient
Exceptions
TypeDescription
IOException

create(FirestoreStub stub)

public static final FirestoreClient create(FirestoreStub stub)

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

Parameter
NameDescription
stubFirestoreStub
Returns
TypeDescription
FirestoreClient

Constructors

FirestoreClient(FirestoreSettings settings)

protected FirestoreClient(FirestoreSettings settings)

Constructs an instance of FirestoreClient, 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
settingsFirestoreSettings

FirestoreClient(FirestoreStub stub)

protected FirestoreClient(FirestoreStub stub)
Parameter
NameDescription
stubFirestoreStub

Methods

awaitTermination(long duration, TimeUnit unit)

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

batchGetDocumentsCallable()

public final ServerStreamingCallable<BatchGetDocumentsRequest,BatchGetDocumentsResponse> batchGetDocumentsCallable()

Gets multiple documents.

Documents returned by this method are not guaranteed to be returned in the same order that they were requested.

Sample code:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (FirestoreClient firestoreClient = FirestoreClient.create()) {
   BatchGetDocumentsRequest request =
       BatchGetDocumentsRequest.newBuilder()
           .setDatabase("database1789464955")
           .addAllDocuments(new ArrayList<String>())
           .setMask(DocumentMask.newBuilder().build())
           .build();
   ServerStream<BatchGetDocumentsResponse> stream =
       firestoreClient.batchGetDocumentsCallable().call(request);
   for (BatchGetDocumentsResponse response : stream) {
     // Do something when a response is received.
   }
 }
 
Returns
TypeDescription
ServerStreamingCallable<BatchGetDocumentsRequest,BatchGetDocumentsResponse>

batchWrite(BatchWriteRequest request)

public final BatchWriteResponse batchWrite(BatchWriteRequest request)

Applies a batch of write operations.

The BatchWrite method does not apply the write operations atomically and can apply them out of order. Method does not allow more than one write per document. Each write succeeds or fails independently. See the BatchWriteResponse for the success status of each write.

If you require an atomically applied set of writes, use Commit instead.

Sample code:


 // This snippet has been automatically generated and should be regarded as a code template only.
 // It will require modifications to work:
 // - It may require correct/in-range values for request initialization.
 // - It may require specifying regional endpoints when creating the service client as shown in
 // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
 try (FirestoreClient firestoreClient = FirestoreClient.create()) {
   BatchWriteRequest request =
       BatchWriteRequest.newBuilder()
           .setDatabase("database1789464955")
           .addAllWrites(new ArrayList<Write>())
           .putAllLabels(new HashMap<String, String>())
           .build();
   BatchWriteResponse response = firestoreClient.batchWrite(request);
 }