Search in sources :

Example 6 with QueryDocumentSnapshot

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

the class UserJourneyTestIT method tearDownClass.

@AfterClass
public static void tearDownClass() throws ExecutionException, InterruptedException {
    // Clear the firestore list if we're not using the local emulator
    Firestore firestore = FirestoreOptions.getDefaultInstance().getService();
    for (QueryDocumentSnapshot docSnapshot : firestore.collection("translations").get().get().getDocuments()) {
        try {
            docSnapshot.getReference().delete().get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
    service.stop();
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) Firestore(com.google.cloud.firestore.Firestore) ExecutionException(java.util.concurrent.ExecutionException) AfterClass(org.junit.AfterClass)

Example 7 with QueryDocumentSnapshot

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

the class TranslateServlet method doGet.

// [START getting_started_background_app_list]
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Firestore firestore = (Firestore) this.getServletContext().getAttribute("firestore");
    CollectionReference translations = firestore.collection("translations");
    QuerySnapshot snapshot;
    try {
        snapshot = translations.limit(10).get().get();
    } catch (InterruptedException | ExecutionException e) {
        throw new ServletException("Exception retrieving documents from Firestore.", e);
    }
    List<TranslateMessage> translateMessages = Lists.newArrayList();
    List<QueryDocumentSnapshot> documents = Lists.newArrayList(snapshot.getDocuments());
    documents.sort(Comparator.comparing(DocumentSnapshot::getCreateTime));
    for (DocumentSnapshot document : Lists.reverse(documents)) {
        String encoded = gson.toJson(document.getData());
        TranslateMessage message = gson.fromJson(encoded, TranslateMessage.class);
        message.setData(decode(message.getData()));
        translateMessages.add(message);
    }
    req.setAttribute("messages", translateMessages);
    req.setAttribute("page", "list");
    req.getRequestDispatcher("/base.jsp").forward(req, resp);
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) ServletException(javax.servlet.ServletException) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Firestore(com.google.cloud.firestore.Firestore) TranslateMessage(com.getstarted.background.objects.TranslateMessage) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with QueryDocumentSnapshot

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

the class RetrieveDataSnippetsIT method testRetrieveAllDocuments.

@Test
public void testRetrieveAllDocuments() throws Exception {
    List<QueryDocumentSnapshot> docs = retrieveDataSnippets.getAllDocuments();
    assertEquals(docs.size(), 5);
    Set<String> docIds = new HashSet<>();
    for (DocumentSnapshot doc : docs) {
        docIds.add(doc.getId());
    }
    assertTrue(docIds.contains("SF") && docIds.contains("LA") && docIds.contains("DC") && docIds.contains("TOK") && docIds.contains("BJ"));
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) HashSet(java.util.HashSet) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 9 with QueryDocumentSnapshot

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

the class Quickstart method retrieveAllDocuments.

void retrieveAllDocuments() throws Exception {
    // [START fs_get_all]
    // asynchronously retrieve all users
    ApiFuture<QuerySnapshot> query = db.collection("users").get();
    // ...
    // query.get() blocks on response
    QuerySnapshot querySnapshot = query.get();
    List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();
    for (QueryDocumentSnapshot document : documents) {
        System.out.println("User: " + document.getId());
        System.out.println("First: " + document.getString("first"));
        if (document.contains("middle")) {
            System.out.println("Middle: " + document.getString("middle"));
        }
        System.out.println("Last: " + document.getString("last"));
        System.out.println("Born: " + document.getLong("born"));
    }
// [END fs_get_all]
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 10 with QueryDocumentSnapshot

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

the class RetrieveDataSnippets method getQueryResults.

/**
 * Return multiple documents from a collection based on a query.
 *
 * @return list of documents of capital cities.
 */
public List<QueryDocumentSnapshot> getQueryResults() throws Exception {
    // [START fs_get_multiple_docs]
    // asynchronously retrieve multiple documents
    ApiFuture<QuerySnapshot> future = db.collection("cities").whereEqualTo("capital", true).get();
    // future.get() blocks on response
    List<QueryDocumentSnapshot> documents = future.get().getDocuments();
    for (DocumentSnapshot document : documents) {
        System.out.println(document.getId() + " => " + document.toObject(City.class));
    }
    // [END fs_get_multiple_docs]
    return documents;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Aggregations

QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)11 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)8 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)4 Test (org.junit.Test)4 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)3 ExecutionException (java.util.concurrent.ExecutionException)3 Firestore (com.google.cloud.firestore.Firestore)2 HashSet (java.util.HashSet)2 TranslateMessage (com.getstarted.background.objects.TranslateMessage)1 ApiFuture (com.google.api.core.ApiFuture)1 CollectionReference (com.google.cloud.firestore.CollectionReference)1 Query (com.google.cloud.firestore.Query)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 Precondition (com.google.firestore.v1.Precondition)1 RunQueryRequest (com.google.firestore.v1.RunQueryRequest)1