Java Code Examples for com.google.cloud.firestore.Firestore#collection()
The following examples show how to use
com.google.cloud.firestore.Firestore#collection() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TranslateServlet.java From getting-started-java with Apache License 2.0 | 6 votes |
@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); }
Example 2
Source File: TranslateServlet.java From getting-started-java with Apache License 2.0 | 4 votes |
/** * Handle a posted message from Pubsub. * * @param req The message Pubsub posts to this process. * @param resp Not used. */ @Override public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // Block requests that don't contain the proper verification token. String pubsubVerificationToken = PUBSUB_VERIFICATION_TOKEN; if (req.getParameter("token").compareTo(pubsubVerificationToken) != 0) { resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } // [START getting_started_background_translate_string] String body = req.getReader().lines().collect(Collectors.joining(System.lineSeparator())); PubSubMessage pubsubMessage = gson.fromJson(body, PubSubMessage.class); TranslateMessage message = pubsubMessage.getMessage(); // Use Translate service client to translate the message. Translate translate = (Translate) this.getServletContext().getAttribute("translate"); message.setData(decode(message.getData())); Translation translation = translate.translate( message.getData(), Translate.TranslateOption.sourceLanguage(message.getAttributes().getSourceLang()), Translate.TranslateOption.targetLanguage(message.getAttributes().getTargetLang())); // [END getting_started_background_translate_string] message.setTranslatedText(translation.getTranslatedText()); try { // [START getting_started_background_translate] // Use Firestore service client to store the translation in Firestore. Firestore firestore = (Firestore) this.getServletContext().getAttribute("firestore"); CollectionReference translations = firestore.collection("translations"); ApiFuture<WriteResult> setFuture = translations.document().set(message, SetOptions.merge()); setFuture.get(); resp.getWriter().write(translation.getTranslatedText()); // [END getting_started_background_translate] } catch (InterruptedException | ExecutionException e) { throw new ServletException("Exception storing data in Firestore.", e); } }
Example 3
Source File: FirestoreDao.java From getting-started-java with Apache License 2.0 | 4 votes |
public FirestoreDao() { Firestore firestore = FirestoreOptions.getDefaultInstance().getService(); booksCollection = firestore.collection("books"); }