Search in sources :

Example 1 with WriteBatch

use of com.google.cloud.firestore.WriteBatch in project java-docs-samples by GoogleCloudPlatform.

the class ManageDataSnippets method writeBatch.

/**
 * Write documents in a batch.
 */
void writeBatch() throws Exception {
    db.collection("cities").document("SF").set(new City()).get();
    db.collection("cities").document("LA").set(new City()).get();
    // [START fs_write_batch]
    // Get a new write batch
    WriteBatch batch = db.batch();
    // Set the value of 'NYC'
    DocumentReference nycRef = db.collection("cities").document("NYC");
    batch.set(nycRef, new City());
    // Update the population of 'SF'
    DocumentReference sfRef = db.collection("cities").document("SF");
    batch.update(sfRef, "population", 1000000L);
    // Delete the city 'LA'
    DocumentReference laRef = db.collection("cities").document("LA");
    batch.delete(laRef);
    // asynchronously commit the batch
    ApiFuture<List<WriteResult>> future = batch.commit();
    // future.get() blocks on batch commit operation
    for (WriteResult result : future.get()) {
        System.out.println("Update time : " + result.getUpdateTime());
    }
// [END fs_write_batch]
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) ArrayList(java.util.ArrayList) List(java.util.List) City(com.example.firestore.snippets.model.City) WriteBatch(com.google.cloud.firestore.WriteBatch) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 2 with WriteBatch

use of com.google.cloud.firestore.WriteBatch in project beam by apache.

the class BaseFirestoreIT method listCollections.

@Test
@TestDataLayoutHint(DataLayout.Deep)
public final void listCollections() throws Exception {
    // verification and cleanup of nested collections is much slower because each document
    // requires an rpc to find its collections, instead of using the usual size, use 20
    // to keep the test quick
    List<String> collectionIds = IntStream.rangeClosed(1, 20).mapToObj(i -> helper.colId()).collect(Collectors.toList());
    ApiFutures.transform(ApiFutures.allAsList(chunkUpDocIds(collectionIds).map(chunk -> {
        WriteBatch batch = helper.getFs().batch();
        chunk.stream().map(col -> helper.getBaseDocument().collection(col).document()).forEach(ref -> batch.set(ref, ImmutableMap.of("foo", "bar")));
        return batch.commit();
    }).collect(Collectors.toList())), FirestoreTestingHelper.flattenListList(), MoreExecutors.directExecutor()).get(10, TimeUnit.SECONDS);
    PCollection<String> actualCollectionIds = testPipeline.apply(Create.of("")).apply(getListCollectionIdsPTransform(testName.getMethodName())).apply(FirestoreIO.v1().read().listCollectionIds().withRpcQosOptions(rpcQosOptions).build());
    PAssert.that(actualCollectionIds).containsInAnyOrder(collectionIds);
    testPipeline.run(options);
}
Also used : IntStream(java.util.stream.IntStream) BatchGetDocumentsResponse(com.google.firestore.v1.BatchGetDocumentsResponse) Assume.assumeThat(org.junit.Assume.assumeThat) RunQueryRequest(com.google.firestore.v1.RunQueryRequest) Write(com.google.firestore.v1.Write) BeforeClass(org.junit.BeforeClass) TestDataLayoutHint(org.apache.beam.sdk.io.gcp.firestore.it.FirestoreTestingHelper.TestDataLayoutHint) CleanupMode(org.apache.beam.sdk.io.gcp.firestore.it.FirestoreTestingHelper.CleanupMode) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) BatchGetDocumentsRequest(com.google.firestore.v1.BatchGetDocumentsRequest) PTransform(org.apache.beam.sdk.transforms.PTransform) DocumentGenerator(org.apache.beam.sdk.io.gcp.firestore.it.FirestoreTestingHelper.DocumentGenerator) Create(org.apache.beam.sdk.transforms.Create) TestName(org.junit.rules.TestName) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) FirestoreOptions(org.apache.beam.sdk.io.gcp.firestore.FirestoreOptions) Timeout(org.junit.rules.Timeout) ListCollectionIdsRequest(com.google.firestore.v1.ListCollectionIdsRequest) FirestoreTestingHelper.assumeEnvVarSet(org.apache.beam.sdk.io.gcp.firestore.it.FirestoreTestingHelper.assumeEnvVarSet) FirestoreTestingHelper.chunkUpDocIds(org.apache.beam.sdk.io.gcp.firestore.it.FirestoreTestingHelper.chunkUpDocIds) Before(org.junit.Before) DoFn(org.apache.beam.sdk.transforms.DoFn) AssumptionViolatedException(org.junit.AssumptionViolatedException) MoreExecutors(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.util.concurrent.MoreExecutors) ApiFutures(com.google.api.core.ApiFutures) GcpOptions(org.apache.beam.sdk.extensions.gcp.options.GcpOptions) PAssert(org.apache.beam.sdk.testing.PAssert) Document(com.google.firestore.v1.Document) Test(org.junit.Test) UUID(java.util.UUID) PCollection(org.apache.beam.sdk.values.PCollection) Collectors(java.util.stream.Collectors) FirestoreIO(org.apache.beam.sdk.io.gcp.firestore.FirestoreIO) TimeUnit(java.util.concurrent.TimeUnit) ListDocumentsRequest(com.google.firestore.v1.ListDocumentsRequest) WriteBatch(com.google.cloud.firestore.WriteBatch) List(java.util.List) Rule(org.junit.Rule) Filter(org.apache.beam.sdk.transforms.Filter) ParDo(org.apache.beam.sdk.transforms.ParDo) DataLayout(org.apache.beam.sdk.io.gcp.firestore.it.FirestoreTestingHelper.DataLayout) Matchers.equalTo(org.hamcrest.Matchers.equalTo) RpcQosOptions(org.apache.beam.sdk.io.gcp.firestore.RpcQosOptions) RunQueryResponse(com.google.firestore.v1.RunQueryResponse) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) PartitionQueryRequest(com.google.firestore.v1.PartitionQueryRequest) WriteBatch(com.google.cloud.firestore.WriteBatch) Test(org.junit.Test) TestDataLayoutHint(org.apache.beam.sdk.io.gcp.firestore.it.FirestoreTestingHelper.TestDataLayoutHint)

Aggregations

WriteBatch (com.google.cloud.firestore.WriteBatch)2 List (java.util.List)2 City (com.example.firestore.snippets.model.City)1 ApiFutures (com.google.api.core.ApiFutures)1 DocumentReference (com.google.cloud.firestore.DocumentReference)1 WriteResult (com.google.cloud.firestore.WriteResult)1 BatchGetDocumentsRequest (com.google.firestore.v1.BatchGetDocumentsRequest)1 BatchGetDocumentsResponse (com.google.firestore.v1.BatchGetDocumentsResponse)1 Document (com.google.firestore.v1.Document)1 ListCollectionIdsRequest (com.google.firestore.v1.ListCollectionIdsRequest)1 ListDocumentsRequest (com.google.firestore.v1.ListDocumentsRequest)1 PartitionQueryRequest (com.google.firestore.v1.PartitionQueryRequest)1 RunQueryRequest (com.google.firestore.v1.RunQueryRequest)1 RunQueryResponse (com.google.firestore.v1.RunQueryResponse)1 Write (com.google.firestore.v1.Write)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 UUID (java.util.UUID)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1