Search in sources :

Example 16 with QuerySnapshot

use of com.google.cloud.firestore.QuerySnapshot in project getting-started-java by GoogleCloudPlatform.

the class FirestoreDao method listBooks.

// [END bookshelf_firestore_documents_to_books]
// [START bookshelf_firestore_list_books]
@Override
public Result<Book> listBooks(String startTitle) {
    Query booksQuery = booksCollection.orderBy("title").limit(10);
    if (startTitle != null) {
        booksQuery = booksQuery.startAfter(startTitle);
    }
    try {
        QuerySnapshot snapshot = booksQuery.get().get();
        List<Book> results = documentsToBooks(snapshot.getDocuments());
        String newCursor = null;
        if (results.size() > 0) {
            newCursor = results.get(results.size() - 1).getTitle();
        }
        return new Result<>(results, newCursor);
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    return new Result<>(Lists.newArrayList(), null);
}
Also used : Query(com.google.cloud.firestore.Query) Book(com.example.getstarted.objects.Book) ExecutionException(java.util.concurrent.ExecutionException) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) Result(com.example.getstarted.objects.Result)

Example 17 with QuerySnapshot

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

the class FirestoreV1IT method batchWrite_partialFailureOutputsToDeadLetterQueue.

@Test
public void batchWrite_partialFailureOutputsToDeadLetterQueue() throws InterruptedException, ExecutionException, TimeoutException {
    String collectionId = "a";
    String docId = helper.docId();
    Write validWrite = Write.newBuilder().setUpdate(Document.newBuilder().setName(docPath(helper.getBaseDocumentPath(), collectionId, docId)).putFields("foo", Value.newBuilder().setStringValue(docId).build())).build();
    long millis = System.currentTimeMillis();
    Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000).setNanos((int) ((millis % 1000) * 1000000)).build();
    String docId2 = helper.docId();
    helper.getBaseDocument().collection(collectionId).document(docId2).create(ImmutableMap.of("foo", "baz")).get(10, TimeUnit.SECONDS);
    // this will fail because we're setting a updateTime precondition to before it was created
    Write conditionalUpdate = Write.newBuilder().setUpdate(Document.newBuilder().setName(docPath(helper.getBaseDocumentPath(), collectionId, docId2)).putFields("foo", Value.newBuilder().setStringValue(docId).build())).setCurrentDocument(Precondition.newBuilder().setUpdateTime(timestamp)).build();
    List<Write> writes = newArrayList(validWrite, conditionalUpdate);
    RpcQosOptions options = BaseFirestoreIT.rpcQosOptions.toBuilder().withBatchMaxCount(2).build();
    PCollection<WriteFailure> writeFailurePCollection = testPipeline.apply(Create.of(writes)).apply(FirestoreIO.v1().write().batchWrite().withDeadLetterQueue().withRpcQosOptions(options).build());
    PAssert.that(writeFailurePCollection).satisfies((writeFailures) -> {
        Iterator<WriteFailure> iterator = writeFailures.iterator();
        assertTrue(iterator.hasNext());
        WriteFailure failure = iterator.next();
        assertEquals(Code.FAILED_PRECONDITION, Code.forNumber(failure.getStatus().getCode()));
        assertNotNull(failure.getWriteResult());
        assertFalse(failure.getWriteResult().hasUpdateTime());
        assertEquals(conditionalUpdate, failure.getWrite());
        assertFalse(iterator.hasNext());
        return null;
    });
    testPipeline.run(this.options);
    ApiFuture<QuerySnapshot> actualDocsQuery = helper.getBaseDocument().collection(collectionId).orderBy("__name__").get();
    QuerySnapshot querySnapshot = actualDocsQuery.get(10, TimeUnit.SECONDS);
    List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();
    List<KV<String, String>> actualDocumentIds = documents.stream().map(doc -> KV.of(doc.getId(), doc.getString("foo"))).collect(Collectors.toList());
    List<KV<String, String>> expected = newArrayList(KV.of(docId, docId), KV.of(docId2, "baz"));
    assertEquals(expected, actualDocumentIds);
}
Also used : Write(com.google.firestore.v1.Write) KV(org.apache.beam.sdk.values.KV) RunQueryRequest(com.google.firestore.v1.RunQueryRequest) FieldReference(com.google.firestore.v1.StructuredQuery.FieldReference) Write(com.google.firestore.v1.Write) Direction(com.google.firestore.v1.StructuredQuery.Direction) TimeoutException(java.util.concurrent.TimeoutException) Operator(com.google.firestore.v1.StructuredQuery.FieldFilter.Operator) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Timestamp(com.google.protobuf.Timestamp) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) BatchGetDocumentsRequest(com.google.firestore.v1.BatchGetDocumentsRequest) PTransform(org.apache.beam.sdk.transforms.PTransform) Lists.newArrayList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists.newArrayList) Value(com.google.firestore.v1.Value) Create(org.apache.beam.sdk.transforms.Create) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) WriteFailure(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.WriteFailure) ListCollectionIdsRequest(com.google.firestore.v1.ListCollectionIdsRequest) Code(com.google.rpc.Code) DoFn(org.apache.beam.sdk.transforms.DoFn) Precondition(com.google.firestore.v1.Precondition) StructuredQuery(com.google.firestore.v1.StructuredQuery) Iterator(java.util.Iterator) PAssert(org.apache.beam.sdk.testing.PAssert) Assert.assertNotNull(org.junit.Assert.assertNotNull) Filter(com.google.firestore.v1.StructuredQuery.Filter) Document(com.google.firestore.v1.Document) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) PCollection(org.apache.beam.sdk.values.PCollection) Collectors(java.util.stream.Collectors) FirestoreIO(org.apache.beam.sdk.io.gcp.firestore.FirestoreIO) FieldFilter(com.google.firestore.v1.StructuredQuery.FieldFilter) ApiFuture(com.google.api.core.ApiFuture) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ListDocumentsRequest(com.google.firestore.v1.ListDocumentsRequest) List(java.util.List) ParDo(org.apache.beam.sdk.transforms.ParDo) Assert.assertFalse(org.junit.Assert.assertFalse) RpcQosOptions(org.apache.beam.sdk.io.gcp.firestore.RpcQosOptions) Order(com.google.firestore.v1.StructuredQuery.Order) CollectionSelector(com.google.firestore.v1.StructuredQuery.CollectionSelector) Assert.assertEquals(org.junit.Assert.assertEquals) PartitionQueryRequest(com.google.firestore.v1.PartitionQueryRequest) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) WriteFailure(org.apache.beam.sdk.io.gcp.firestore.FirestoreV1.WriteFailure) KV(org.apache.beam.sdk.values.KV) Timestamp(com.google.protobuf.Timestamp) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) RpcQosOptions(org.apache.beam.sdk.io.gcp.firestore.RpcQosOptions) Test(org.junit.Test)

Aggregations

QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)17 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)10 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)8 Query (com.google.cloud.firestore.Query)6 ExecutionException (java.util.concurrent.ExecutionException)4 CollectionReference (com.google.cloud.firestore.CollectionReference)3 Book (com.example.getstarted.objects.Book)2 Result (com.example.getstarted.objects.Result)2 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)1 TranslateMessage (com.getstarted.background.objects.TranslateMessage)1 ApiFuture (com.google.api.core.ApiFuture)1 Firestore (com.google.cloud.firestore.Firestore)1 FirestoreException (com.google.cloud.firestore.FirestoreException)1 ListenerRegistration (com.google.cloud.firestore.ListenerRegistration)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 BatchGetDocumentsRequest (com.google.firestore.v1.BatchGetDocumentsRequest)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