Search in sources :

Example 6 with QuerySnapshot

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

the class QueryDataSnippetsIT method testMultipleCursorConditions.

@Test
public void testMultipleCursorConditions() throws Exception {
    // populate us_cities collection
    Map<String, String> city1 = new ImmutableMap.Builder<String, String>().put("name", "Springfield").put("state", "Massachusetts").build();
    Map<String, String> city2 = new ImmutableMap.Builder<String, String>().put("name", "Springfield").put("state", "Missouri").build();
    Map<String, String> city3 = new ImmutableMap.Builder<String, String>().put("name", "Springfield").put("state", "Wisconsin").build();
    db.collection("us_cities").document("Massachusetts").set(city1).get();
    db.collection("us_cities").document("Missouri").set(city2).get();
    db.collection("us_cities").document("Wisconsin").set(city3).get();
    Query query1 = db.collection("us_cities").orderBy("name").orderBy("state").startAt("Springfield");
    // all documents are retrieved
    QuerySnapshot querySnapshot = query1.get().get();
    List<QueryDocumentSnapshot> docs = querySnapshot.getDocuments();
    assertEquals(3, docs.size());
    // Will return "Springfield, Missouri" and "Springfield, Wisconsin"
    Query query2 = db.collection("us_cities").orderBy("name").orderBy("state").startAt("Springfield", "Missouri");
    // only Missouri and Wisconsin are retrieved
    List<String> expectedResults = Arrays.asList("Missouri", "Wisconsin");
    List<String> result = getResults(query2);
    assertTrue(Objects.equals(result, expectedResults));
}
Also used : Query(com.google.cloud.firestore.Query) QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) ImmutableMap(com.google.common.collect.ImmutableMap) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot) Test(org.junit.Test) BaseIntegrationTest(com.example.firestore.BaseIntegrationTest)

Example 7 with QuerySnapshot

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

the class QueryDataSnippetsIT method getResults.

private List<String> getResults(Query query) throws Exception {
    // asynchronously retrieve query results
    ApiFuture<QuerySnapshot> future = query.get();
    // block on response
    QuerySnapshot querySnapshot = future.get();
    List<String> docIds = new ArrayList<>();
    for (DocumentSnapshot document : querySnapshot.getDocuments()) {
        docIds.add(document.getId());
    }
    return docIds;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) ArrayList(java.util.ArrayList) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 8 with QuerySnapshot

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

the class RetrieveDataSnippetsIT method deleteAllDocuments.

private static void deleteAllDocuments() throws Exception {
    ApiFuture<QuerySnapshot> future = db.collection("cities").get();
    QuerySnapshot querySnapshot = future.get();
    for (DocumentSnapshot doc : querySnapshot.getDocuments()) {
        // block on delete operation
        db.collection("cities").document(doc.getId()).delete().get();
    }
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 9 with QuerySnapshot

use of com.google.cloud.firestore.QuerySnapshot 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 10 with QuerySnapshot

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

the class FirestoreDao method listBooksByUser.

// [END bookshelf_firestore_list_books]
// [START bookshelf_firestore_list_by_user]
@Override
public Result<Book> listBooksByUser(String userId, String startTitle) {
    Query booksQuery = booksCollection.orderBy("title").whereEqualTo(Book.CREATED_BY_ID, userId).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)

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