Search in sources :

Example 1 with DocumentSnapshot

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

use of com.google.cloud.firestore.DocumentSnapshot in project spring-cloud-gcp by spring-cloud.

the class FirestoreSampleApp method readDocumentToMap.

private void readDocumentToMap() throws ExecutionException, InterruptedException {
    DocumentReference docRef = this.firestore.document("users/ada");
    ApiFuture<DocumentSnapshot> documentSnapshotApiFuture = docRef.get();
    DocumentSnapshot document = documentSnapshotApiFuture.get();
    System.out.println("read: " + document.getData());
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 3 with DocumentSnapshot

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

the class ListenDataSnippets method listenToDocument.

/**
 * Listen to a single document, returning data after the first snapshot.
 */
Map<String, Object> listenToDocument() throws Exception {
    final SettableApiFuture<Map<String, Object>> future = SettableApiFuture.create();
    // [START listen_to_document]
    DocumentReference docRef = db.collection("cities").document("SF");
    docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {

        @Override
        public void onEvent(@Nullable DocumentSnapshot snapshot, @Nullable FirestoreException e) {
            if (e != null) {
                System.err.println("Listen failed: " + e);
                return;
            }
            if (snapshot != null && snapshot.exists()) {
                System.out.println("Current data: " + snapshot.getData());
            } else {
                System.out.print("Current data: null");
            }
            // [START_EXCLUDE silent]
            if (!future.isDone()) {
                future.set(snapshot.getData());
            }
        // [END_EXCLUDE]
        }
    });
    return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) FirestoreException(com.google.cloud.firestore.FirestoreException) Map(java.util.Map) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 4 with DocumentSnapshot

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

the class QueryDataSnippets method createAQuery.

/**
 * Creates a sample query.
 *
 * @return query
 */
Query createAQuery() throws Exception {
    // [START fs_create_query]
    // Create a reference to the cities collection
    CollectionReference cities = db.collection("cities");
    // Create a query against the collection.
    Query query = cities.whereEqualTo("capital", true);
    // retrieve  query results asynchronously using query.get()
    ApiFuture<QuerySnapshot> querySnapshot = query.get();
    for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {
        System.out.println(document.getId());
    }
    // [END fs_create_query]
    return query;
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Query(com.google.cloud.firestore.Query) CollectionReference(com.google.cloud.firestore.CollectionReference) QuerySnapshot(com.google.cloud.firestore.QuerySnapshot)

Example 5 with DocumentSnapshot

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

the class RetrieveDataSnippets method getDocumentAsMap.

/**
 * Retrieves document in collection as map.
 *
 * @return map (string => object)
 */
public Map<String, Object> getDocumentAsMap() throws Exception {
    // [START fs_get_doc_as_map]
    DocumentReference docRef = db.collection("cities").document("SF");
    // asynchronously retrieve the document
    ApiFuture<DocumentSnapshot> future = docRef.get();
    // ...
    // future.get() blocks on response
    DocumentSnapshot document = future.get();
    if (document.exists()) {
        System.out.println("Document data: " + document.getData());
    } else {
        System.out.println("No such document!");
    }
    // [END fs_get_doc_as_map]
    return (document.exists()) ? document.getData() : null;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) DocumentReference(com.google.cloud.firestore.DocumentReference)

Aggregations

DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)39 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)22 DocumentReference (com.google.cloud.firestore.DocumentReference)15 QuerySnapshot (com.google.cloud.firestore.QuerySnapshot)10 ExecutionException (java.util.concurrent.ExecutionException)10 FileSystemExecutionException (bio.terra.service.filedata.exception.FileSystemExecutionException)6 CollectionReference (com.google.cloud.firestore.CollectionReference)6 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)4 Query (com.google.cloud.firestore.Query)4 Test (org.junit.Test)4 AbortedException (com.google.api.gax.rpc.AbortedException)3 ArrayList (java.util.ArrayList)3 DatasetDataProject (bio.terra.service.dataset.DatasetDataProject)2 City (com.example.firestore.snippets.model.City)2 Transaction (com.google.cloud.firestore.Transaction)2 HashSet (java.util.HashSet)2 NotImplementedException (bio.terra.common.exception.NotImplementedException)1 TranslateMessage (com.getstarted.background.objects.TranslateMessage)1 Firestore (com.google.cloud.firestore.Firestore)1 FirestoreException (com.google.cloud.firestore.FirestoreException)1