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);
}
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());
}
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);
}
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;
}
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;
}
Aggregations