@Generated(value="by gapic-generator") @BetaApi public class FirestoreClient extends Object implements com.google.api.gax.core.BackgroundResource
This service exposes several types of comparable timestamps:
* `create_time` - The time at which a document was created. Changes only when a document is deleted, then re-created. Increases in a strict monotonic fashion. * `update_time` - The time at which a document was last updated. Changes every time a document is modified. Does not change when a write results in no modifications. Increases in a strict monotonic fashion. * `read_time` - The time at which a particular state was observed. Used to denote a consistent snapshot of the database or the time at which a Document was observed to not exist. * `commit_time` - The time at which the writes in a transaction were committed. Any read with an equal or greater `read_time` is guaranteed to see the effects of the transaction.
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 (FirestoreClient firestoreClient = FirestoreClient.create()) {
Document document = Document.newBuilder().build();
DocumentMask updateMask = DocumentMask.newBuilder().build();
Document response = firestoreClient.updateDocument(document, updateMask);
}
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:
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:
FirestoreSettings firestoreSettings =
FirestoreSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
.build();
FirestoreClient firestoreClient =
FirestoreClient.create(firestoreSettings);
To customize the endpoint:
FirestoreSettings firestoreSettings =
FirestoreSettings.newBuilder().setEndpoint(myEndpoint).build();
FirestoreClient firestoreClient =
FirestoreClient.create(firestoreSettings);
| Modifier and Type | Class and Description |
|---|---|
static class |
FirestoreClient.ListCollectionIdsFixedSizeCollection |
static class |
FirestoreClient.ListCollectionIdsPage |
static class |
FirestoreClient.ListCollectionIdsPagedResponse |
static class |
FirestoreClient.ListDocumentsFixedSizeCollection |
static class |
FirestoreClient.ListDocumentsPage |
static class |
FirestoreClient.ListDocumentsPagedResponse |
| Modifier | Constructor and Description |
|---|---|
protected |
FirestoreClient(FirestoreSettings settings)
Constructs an instance of FirestoreClient, using the given settings.
|
protected |
FirestoreClient(FirestoreStub stub) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
awaitTermination(long duration,
TimeUnit unit) |
com.google.api.gax.rpc.ServerStreamingCallable<com.google.firestore.v1beta1.BatchGetDocumentsRequest,com.google.firestore.v1beta1.BatchGetDocumentsResponse> |
batchGetDocumentsCallable()
Gets multiple documents.
|
com.google.firestore.v1beta1.BeginTransactionResponse |
beginTransaction(com.google.firestore.v1beta1.BeginTransactionRequest request)
Starts a new transaction.
|
com.google.firestore.v1beta1.BeginTransactionResponse |
beginTransaction(String database)
Starts a new transaction.
|
com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.BeginTransactionRequest,com.google.firestore.v1beta1.BeginTransactionResponse> |
beginTransactionCallable()
Starts a new transaction.
|
void |
close() |
com.google.firestore.v1beta1.CommitResponse |
commit(com.google.firestore.v1beta1.CommitRequest request)
Commits a transaction, while optionally updating documents.
|
com.google.firestore.v1beta1.CommitResponse |
commit(String database,
List<com.google.firestore.v1beta1.Write> writes)
Commits a transaction, while optionally updating documents.
|
com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.CommitRequest,com.google.firestore.v1beta1.CommitResponse> |
commitCallable()
Commits a transaction, while optionally updating documents.
|
static FirestoreClient |
create()
Constructs an instance of FirestoreClient with default settings.
|
static FirestoreClient |
create(FirestoreSettings settings)
Constructs an instance of FirestoreClient, using the given settings.
|
static FirestoreClient |
create(FirestoreStub stub)
Constructs an instance of FirestoreClient, using the given stub for making calls.
|
com.google.firestore.v1beta1.Document |
createDocument(com.google.firestore.v1beta1.CreateDocumentRequest request)
Creates a new document.
|
com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.CreateDocumentRequest,com.google.firestore.v1beta1.Document> |
createDocumentCallable()
Creates a new document.
|
void |
deleteDocument(com.google.firestore.v1beta1.DeleteDocumentRequest request)
Deletes a document.
|
void |
deleteDocument(String name)
Deletes a document.
|
com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.DeleteDocumentRequest,com.google.protobuf.Empty> |
deleteDocumentCallable()
Deletes a document.
|
static String |
formatAnyPathName(String project,
String database,
String document,
String anyPath)
Formats a string containing the fully-qualified path to represent a any_path resource.
|
static String |
formatDatabaseRootName(String project,
String database)
Formats a string containing the fully-qualified path to represent a database_root resource.
|
static String |
formatDocumentPathName(String project,
String database,
String documentPath)
Formats a string containing the fully-qualified path to represent a document_path resource.
|
static String |
formatDocumentRootName(String project,
String database)
Formats a string containing the fully-qualified path to represent a document_root resource.
|
com.google.firestore.v1beta1.Document |
getDocument(com.google.firestore.v1beta1.GetDocumentRequest request)
Gets a single document.
|
com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.GetDocumentRequest,com.google.firestore.v1beta1.Document> |
getDocumentCallable()
Gets a single document.
|
FirestoreSettings |
getSettings() |
FirestoreStub |
getStub() |
boolean |
isShutdown() |
boolean |
isTerminated() |
FirestoreClient.ListCollectionIdsPagedResponse |
listCollectionIds(com.google.firestore.v1beta1.ListCollectionIdsRequest request)
Lists all the collection IDs underneath a document.
|
FirestoreClient.ListCollectionIdsPagedResponse |
listCollectionIds(String parent)
Lists all the collection IDs underneath a document.
|
com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.ListCollectionIdsRequest,com.google.firestore.v1beta1.ListCollectionIdsResponse> |
listCollectionIdsCallable()
Lists all the collection IDs underneath a document.
|
com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.ListCollectionIdsRequest,FirestoreClient.ListCollectionIdsPagedResponse> |
listCollectionIdsPagedCallable()
Lists all the collection IDs underneath a document.
|
FirestoreClient.ListDocumentsPagedResponse |
listDocuments(com.google.firestore.v1beta1.ListDocumentsRequest request)
Lists documents.
|
com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.ListDocumentsRequest,com.google.firestore.v1beta1.ListDocumentsResponse> |
listDocumentsCallable()
Lists documents.
|
com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.ListDocumentsRequest,FirestoreClient.ListDocumentsPagedResponse> |
listDocumentsPagedCallable()
Lists documents.
|
com.google.api.gax.rpc.BidiStreamingCallable<com.google.firestore.v1beta1.ListenRequest,com.google.firestore.v1beta1.ListenResponse> |
listenCallable()
Listens to changes.
|
static String |
parseAnyPathFromAnyPathName(String anyPathName)
Parses the any_path from the given fully-qualified path which represents a any_path resource.
|
static String |
parseDatabaseFromAnyPathName(String anyPathName)
Parses the database from the given fully-qualified path which represents a any_path resource.
|
static String |
parseDatabaseFromDatabaseRootName(String databaseRootName)
Parses the database from the given fully-qualified path which represents a database_root
resource.
|
static String |
parseDatabaseFromDocumentPathName(String documentPathName)
Parses the database from the given fully-qualified path which represents a document_path
resource.
|
static String |
parseDatabaseFromDocumentRootName(String documentRootName)
Parses the database from the given fully-qualified path which represents a document_root
resource.
|
static String |
parseDocumentFromAnyPathName(String anyPathName)
Parses the document from the given fully-qualified path which represents a any_path resource.
|
static String |
parseDocumentPathFromDocumentPathName(String documentPathName)
Parses the document_path from the given fully-qualified path which represents a document_path
resource.
|
static String |
parseProjectFromAnyPathName(String anyPathName)
Parses the project from the given fully-qualified path which represents a any_path resource.
|
static String |
parseProjectFromDatabaseRootName(String databaseRootName)
Parses the project from the given fully-qualified path which represents a database_root
resource.
|
static String |
parseProjectFromDocumentPathName(String documentPathName)
Parses the project from the given fully-qualified path which represents a document_path
resource.
|
static String |
parseProjectFromDocumentRootName(String documentRootName)
Parses the project from the given fully-qualified path which represents a document_root
resource.
|
void |
rollback(com.google.firestore.v1beta1.RollbackRequest request)
Rolls back a transaction.
|
void |
rollback(String database,
com.google.protobuf.ByteString transaction)
Rolls back a transaction.
|
com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.RollbackRequest,com.google.protobuf.Empty> |
rollbackCallable()
Rolls back a transaction.
|
com.google.api.gax.rpc.ServerStreamingCallable<com.google.firestore.v1beta1.RunQueryRequest,com.google.firestore.v1beta1.RunQueryResponse> |
runQueryCallable()
Runs a query.
|
void |
shutdown() |
void |
shutdownNow() |
com.google.firestore.v1beta1.Document |
updateDocument(com.google.firestore.v1beta1.Document document,
com.google.firestore.v1beta1.DocumentMask updateMask)
Updates or inserts a document.
|
com.google.firestore.v1beta1.Document |
updateDocument(com.google.firestore.v1beta1.UpdateDocumentRequest request)
Updates or inserts a document.
|
com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.UpdateDocumentRequest,com.google.firestore.v1beta1.Document> |
updateDocumentCallable()
Updates or inserts a document.
|
com.google.api.gax.rpc.BidiStreamingCallable<com.google.firestore.v1beta1.WriteRequest,com.google.firestore.v1beta1.WriteResponse> |
writeCallable()
Streams batches of document updates and deletes, in order.
|
protected FirestoreClient(FirestoreSettings settings) throws IOException
IOException@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") protected FirestoreClient(FirestoreStub stub)
public static final String formatDatabaseRootName(String project, String database)
public static final String formatDocumentRootName(String project, String database)
public static final String formatDocumentPathName(String project, String database, String documentPath)
public static final String formatAnyPathName(String project, String database, String document, String anyPath)
public static final String parseProjectFromDatabaseRootName(String databaseRootName)
public static final String parseDatabaseFromDatabaseRootName(String databaseRootName)
public static final String parseProjectFromDocumentRootName(String documentRootName)
public static final String parseDatabaseFromDocumentRootName(String documentRootName)
public static final String parseProjectFromDocumentPathName(String documentPathName)
public static final String parseDatabaseFromDocumentPathName(String documentPathName)
public static final String parseDocumentPathFromDocumentPathName(String documentPathName)
public static final String parseProjectFromAnyPathName(String anyPathName)
public static final String parseDatabaseFromAnyPathName(String anyPathName)
public static final String parseDocumentFromAnyPathName(String anyPathName)
public static final String parseAnyPathFromAnyPathName(String anyPathName)
public static final FirestoreClient create() throws IOException
IOExceptionpublic static final FirestoreClient create(FirestoreSettings settings) throws IOException
IOException@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") public static final FirestoreClient create(FirestoreStub stub)
public final FirestoreSettings getSettings()
@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") public FirestoreStub getStub()
public final com.google.firestore.v1beta1.Document getDocument(com.google.firestore.v1beta1.GetDocumentRequest request)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedName = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
GetDocumentRequest request = GetDocumentRequest.newBuilder()
.setName(formattedName)
.build();
Document response = firestoreClient.getDocument(request);
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.GetDocumentRequest,com.google.firestore.v1beta1.Document> getDocumentCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedName = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
GetDocumentRequest request = GetDocumentRequest.newBuilder()
.setName(formattedName)
.build();
ApiFuture<Document> future = firestoreClient.getDocumentCallable().futureCall(request);
// Do something
Document response = future.get();
}
public final FirestoreClient.ListDocumentsPagedResponse listDocuments(com.google.firestore.v1beta1.ListDocumentsRequest request)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedParent = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
String collectionId = "";
ListDocumentsRequest request = ListDocumentsRequest.newBuilder()
.setParent(formattedParent)
.setCollectionId(collectionId)
.build();
for (Document element : firestoreClient.listDocuments(request).iterateAll()) {
// doThingsWith(element);
}
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.ListDocumentsRequest,FirestoreClient.ListDocumentsPagedResponse> listDocumentsPagedCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedParent = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
String collectionId = "";
ListDocumentsRequest request = ListDocumentsRequest.newBuilder()
.setParent(formattedParent)
.setCollectionId(collectionId)
.build();
ApiFuture<ListDocumentsPagedResponse> future = firestoreClient.listDocumentsPagedCallable().futureCall(request);
// Do something
for (Document element : future.get().iterateAll()) {
// doThingsWith(element);
}
}
public final com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.ListDocumentsRequest,com.google.firestore.v1beta1.ListDocumentsResponse> listDocumentsCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedParent = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
String collectionId = "";
ListDocumentsRequest request = ListDocumentsRequest.newBuilder()
.setParent(formattedParent)
.setCollectionId(collectionId)
.build();
while (true) {
ListDocumentsResponse response = firestoreClient.listDocumentsCallable().call(request);
for (Document element : response.getDocumentsList()) {
// doThingsWith(element);
}
String nextPageToken = response.getNextPageToken();
if (!Strings.isNullOrEmpty(nextPageToken)) {
request = request.toBuilder().setPageToken(nextPageToken).build();
} else {
break;
}
}
}
public final com.google.firestore.v1beta1.Document createDocument(com.google.firestore.v1beta1.CreateDocumentRequest request)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedParent = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
String collectionId = "";
String documentId = "";
Document document = Document.newBuilder().build();
CreateDocumentRequest request = CreateDocumentRequest.newBuilder()
.setParent(formattedParent)
.setCollectionId(collectionId)
.setDocumentId(documentId)
.setDocument(document)
.build();
Document response = firestoreClient.createDocument(request);
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.CreateDocumentRequest,com.google.firestore.v1beta1.Document> createDocumentCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedParent = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
String collectionId = "";
String documentId = "";
Document document = Document.newBuilder().build();
CreateDocumentRequest request = CreateDocumentRequest.newBuilder()
.setParent(formattedParent)
.setCollectionId(collectionId)
.setDocumentId(documentId)
.setDocument(document)
.build();
ApiFuture<Document> future = firestoreClient.createDocumentCallable().futureCall(request);
// Do something
Document response = future.get();
}
public final com.google.firestore.v1beta1.Document updateDocument(com.google.firestore.v1beta1.Document document,
com.google.firestore.v1beta1.DocumentMask updateMask)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
Document document = Document.newBuilder().build();
DocumentMask updateMask = DocumentMask.newBuilder().build();
Document response = firestoreClient.updateDocument(document, updateMask);
}
document - The updated document. Creates the document if it does not already exist.updateMask - The fields to update. None of the field paths in the mask may contain a
reserved name.
If the document exists on the server and has fields not referenced in the mask, they are left unchanged. Fields referenced in the mask, but not present in the input document, are deleted from the document on the server.
com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.firestore.v1beta1.Document updateDocument(com.google.firestore.v1beta1.UpdateDocumentRequest request)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
Document document = Document.newBuilder().build();
DocumentMask updateMask = DocumentMask.newBuilder().build();
UpdateDocumentRequest request = UpdateDocumentRequest.newBuilder()
.setDocument(document)
.setUpdateMask(updateMask)
.build();
Document response = firestoreClient.updateDocument(request);
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.UpdateDocumentRequest,com.google.firestore.v1beta1.Document> updateDocumentCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
Document document = Document.newBuilder().build();
DocumentMask updateMask = DocumentMask.newBuilder().build();
UpdateDocumentRequest request = UpdateDocumentRequest.newBuilder()
.setDocument(document)
.setUpdateMask(updateMask)
.build();
ApiFuture<Document> future = firestoreClient.updateDocumentCallable().futureCall(request);
// Do something
Document response = future.get();
}
public final void deleteDocument(String name)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedName = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
firestoreClient.deleteDocument(formattedName);
}
name - The resource name of the Document to delete. In the format:
`projects/{project_id}/databases/{database_id}/documents/{document_path}`.com.google.api.gax.rpc.ApiException - if the remote call failspublic final void deleteDocument(com.google.firestore.v1beta1.DeleteDocumentRequest request)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedName = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
DeleteDocumentRequest request = DeleteDocumentRequest.newBuilder()
.setName(formattedName)
.build();
firestoreClient.deleteDocument(request);
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.DeleteDocumentRequest,com.google.protobuf.Empty> deleteDocumentCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedName = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
DeleteDocumentRequest request = DeleteDocumentRequest.newBuilder()
.setName(formattedName)
.build();
ApiFuture<Void> future = firestoreClient.deleteDocumentCallable().futureCall(request);
// Do something
future.get();
}
public final com.google.api.gax.rpc.ServerStreamingCallable<com.google.firestore.v1beta1.BatchGetDocumentsRequest,com.google.firestore.v1beta1.BatchGetDocumentsResponse> batchGetDocumentsCallable()
Documents returned by this method are not guaranteed to be returned in the same order that they were requested.
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
ApiStreamObserver<BatchGetDocumentsResponse> responseObserver =
new ApiStreamObserver<BatchGetDocumentsResponse>() {
@Override
public void onNext(BatchGetDocumentsResponse response) {
// Do something when receive a response
}
@Override
public void onError(Throwable t) {
// Add error-handling
}
@Override
public void onCompleted() {
// Do something when complete.
}
};
String formattedDatabase = FirestoreClient.formatDatabaseRootName("[PROJECT]", "[DATABASE]");
List<String> documents = new ArrayList<>();
BatchGetDocumentsRequest request = BatchGetDocumentsRequest.newBuilder()
.setDatabase(formattedDatabase)
.addAllDocuments(documents)
.build();
firestoreClient.batchGetDocumentsCallable().serverStreamingCall(request, responseObserver));
}
public final com.google.firestore.v1beta1.BeginTransactionResponse beginTransaction(String database)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedDatabase = FirestoreClient.formatDatabaseRootName("[PROJECT]", "[DATABASE]");
BeginTransactionResponse response = firestoreClient.beginTransaction(formattedDatabase);
}
database - The database name. In the format:
`projects/{project_id}/databases/{database_id}`.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.firestore.v1beta1.BeginTransactionResponse beginTransaction(com.google.firestore.v1beta1.BeginTransactionRequest request)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedDatabase = FirestoreClient.formatDatabaseRootName("[PROJECT]", "[DATABASE]");
BeginTransactionRequest request = BeginTransactionRequest.newBuilder()
.setDatabase(formattedDatabase)
.build();
BeginTransactionResponse response = firestoreClient.beginTransaction(request);
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.BeginTransactionRequest,com.google.firestore.v1beta1.BeginTransactionResponse> beginTransactionCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedDatabase = FirestoreClient.formatDatabaseRootName("[PROJECT]", "[DATABASE]");
BeginTransactionRequest request = BeginTransactionRequest.newBuilder()
.setDatabase(formattedDatabase)
.build();
ApiFuture<BeginTransactionResponse> future = firestoreClient.beginTransactionCallable().futureCall(request);
// Do something
BeginTransactionResponse response = future.get();
}
public final com.google.firestore.v1beta1.CommitResponse commit(String database, List<com.google.firestore.v1beta1.Write> writes)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedDatabase = FirestoreClient.formatDatabaseRootName("[PROJECT]", "[DATABASE]");
List<Write> writes = new ArrayList<>();
CommitResponse response = firestoreClient.commit(formattedDatabase, writes);
}
database - The database name. In the format:
`projects/{project_id}/databases/{database_id}`.writes - The writes to apply.
Always executed atomically and in order.
com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.firestore.v1beta1.CommitResponse commit(com.google.firestore.v1beta1.CommitRequest request)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedDatabase = FirestoreClient.formatDatabaseRootName("[PROJECT]", "[DATABASE]");
List<Write> writes = new ArrayList<>();
CommitRequest request = CommitRequest.newBuilder()
.setDatabase(formattedDatabase)
.addAllWrites(writes)
.build();
CommitResponse response = firestoreClient.commit(request);
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.CommitRequest,com.google.firestore.v1beta1.CommitResponse> commitCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedDatabase = FirestoreClient.formatDatabaseRootName("[PROJECT]", "[DATABASE]");
List<Write> writes = new ArrayList<>();
CommitRequest request = CommitRequest.newBuilder()
.setDatabase(formattedDatabase)
.addAllWrites(writes)
.build();
ApiFuture<CommitResponse> future = firestoreClient.commitCallable().futureCall(request);
// Do something
CommitResponse response = future.get();
}
public final void rollback(String database, com.google.protobuf.ByteString transaction)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedDatabase = FirestoreClient.formatDatabaseRootName("[PROJECT]", "[DATABASE]");
ByteString transaction = ByteString.copyFromUtf8("");
firestoreClient.rollback(formattedDatabase, transaction);
}
database - The database name. In the format:
`projects/{project_id}/databases/{database_id}`.transaction - The transaction to roll back.com.google.api.gax.rpc.ApiException - if the remote call failspublic final void rollback(com.google.firestore.v1beta1.RollbackRequest request)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedDatabase = FirestoreClient.formatDatabaseRootName("[PROJECT]", "[DATABASE]");
ByteString transaction = ByteString.copyFromUtf8("");
RollbackRequest request = RollbackRequest.newBuilder()
.setDatabase(formattedDatabase)
.setTransaction(transaction)
.build();
firestoreClient.rollback(request);
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.RollbackRequest,com.google.protobuf.Empty> rollbackCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedDatabase = FirestoreClient.formatDatabaseRootName("[PROJECT]", "[DATABASE]");
ByteString transaction = ByteString.copyFromUtf8("");
RollbackRequest request = RollbackRequest.newBuilder()
.setDatabase(formattedDatabase)
.setTransaction(transaction)
.build();
ApiFuture<Void> future = firestoreClient.rollbackCallable().futureCall(request);
// Do something
future.get();
}
public final com.google.api.gax.rpc.ServerStreamingCallable<com.google.firestore.v1beta1.RunQueryRequest,com.google.firestore.v1beta1.RunQueryResponse> runQueryCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
ApiStreamObserver<RunQueryResponse> responseObserver =
new ApiStreamObserver<RunQueryResponse>() {
@Override
public void onNext(RunQueryResponse response) {
// Do something when receive a response
}
@Override
public void onError(Throwable t) {
// Add error-handling
}
@Override
public void onCompleted() {
// Do something when complete.
}
};
String formattedParent = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
RunQueryRequest request = RunQueryRequest.newBuilder()
.setParent(formattedParent)
.build();
firestoreClient.runQueryCallable().serverStreamingCall(request, responseObserver));
}
public final com.google.api.gax.rpc.BidiStreamingCallable<com.google.firestore.v1beta1.WriteRequest,com.google.firestore.v1beta1.WriteResponse> writeCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
ApiStreamObserver<WriteResponse> responseObserver =
new ApiStreamObserver<WriteResponse>() {
@Override
public void onNext(WriteResponse response) {
// Do something when receive a response
}
@Override
public void onError(Throwable t) {
// Add error-handling
}
@Override
public void onCompleted() {
// Do something when complete.
}
};
ApiStreamObserver<StreamingRecognizeRequest> requestObserver =
firestoreClient.writeCallable().bidiStreamingCall(responseObserver));
String formattedDatabase = FirestoreClient.formatDatabaseRootName("[PROJECT]", "[DATABASE]");
WriteRequest request = WriteRequest.newBuilder()
.setDatabase(formattedDatabase)
.build();
requestObserver.onNext(request);
}
public final com.google.api.gax.rpc.BidiStreamingCallable<com.google.firestore.v1beta1.ListenRequest,com.google.firestore.v1beta1.ListenResponse> listenCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
ApiStreamObserver<ListenResponse> responseObserver =
new ApiStreamObserver<ListenResponse>() {
@Override
public void onNext(ListenResponse response) {
// Do something when receive a response
}
@Override
public void onError(Throwable t) {
// Add error-handling
}
@Override
public void onCompleted() {
// Do something when complete.
}
};
ApiStreamObserver<StreamingRecognizeRequest> requestObserver =
firestoreClient.listenCallable().bidiStreamingCall(responseObserver));
String formattedDatabase = FirestoreClient.formatDatabaseRootName("[PROJECT]", "[DATABASE]");
ListenRequest request = ListenRequest.newBuilder()
.setDatabase(formattedDatabase)
.build();
requestObserver.onNext(request);
}
public final FirestoreClient.ListCollectionIdsPagedResponse listCollectionIds(String parent)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedParent = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
for (String element : firestoreClient.listCollectionIds(formattedParent).iterateAll()) {
// doThingsWith(element);
}
}
parent - The parent document. In the format:
`projects/{project_id}/databases/{database_id}/documents/{document_path}`. For example:
`projects/my-project/databases/my-database/documents/chatrooms/my-chatroom`com.google.api.gax.rpc.ApiException - if the remote call failspublic final FirestoreClient.ListCollectionIdsPagedResponse listCollectionIds(com.google.firestore.v1beta1.ListCollectionIdsRequest request)
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedParent = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
ListCollectionIdsRequest request = ListCollectionIdsRequest.newBuilder()
.setParent(formattedParent)
.build();
for (String element : firestoreClient.listCollectionIds(request).iterateAll()) {
// doThingsWith(element);
}
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.ListCollectionIdsRequest,FirestoreClient.ListCollectionIdsPagedResponse> listCollectionIdsPagedCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedParent = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
ListCollectionIdsRequest request = ListCollectionIdsRequest.newBuilder()
.setParent(formattedParent)
.build();
ApiFuture<ListCollectionIdsPagedResponse> future = firestoreClient.listCollectionIdsPagedCallable().futureCall(request);
// Do something
for (String element : future.get().iterateAll()) {
// doThingsWith(element);
}
}
public final com.google.api.gax.rpc.UnaryCallable<com.google.firestore.v1beta1.ListCollectionIdsRequest,com.google.firestore.v1beta1.ListCollectionIdsResponse> listCollectionIdsCallable()
Sample code:
try (FirestoreClient firestoreClient = FirestoreClient.create()) {
String formattedParent = FirestoreClient.formatAnyPathName("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]");
ListCollectionIdsRequest request = ListCollectionIdsRequest.newBuilder()
.setParent(formattedParent)
.build();
while (true) {
ListCollectionIdsResponse response = firestoreClient.listCollectionIdsCallable().call(request);
for (String element : response.getCollectionIdsList()) {
// doThingsWith(element);
}
String nextPageToken = response.getNextPageToken();
if (!Strings.isNullOrEmpty(nextPageToken)) {
request = request.toBuilder().setPageToken(nextPageToken).build();
} else {
break;
}
}
}
public final void close()
close in interface AutoCloseablepublic void shutdown()
shutdown in interface com.google.api.gax.core.BackgroundResourcepublic boolean isShutdown()
isShutdown in interface com.google.api.gax.core.BackgroundResourcepublic boolean isTerminated()
isTerminated in interface com.google.api.gax.core.BackgroundResourcepublic void shutdownNow()
shutdownNow in interface com.google.api.gax.core.BackgroundResourcepublic boolean awaitTermination(long duration,
TimeUnit unit)
throws InterruptedException
awaitTermination in interface com.google.api.gax.core.BackgroundResourceInterruptedExceptionCopyright © 2018 Google LLC. All rights reserved.