Search in sources :

Example 6 with WriteResult

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

the class ManageDataSnippets method updateSimpleDocument.

/**
 * Partially update a document using the .update(field1, value1..) method.
 */
void updateSimpleDocument() throws Exception {
    db.collection("cities").document("DC").set(new City("Washington D.C.")).get();
    // [START fs_update_doc]
    // Update an existing document
    DocumentReference docRef = db.collection("cities").document("DC");
    // (async) Update one field
    ApiFuture<WriteResult> future = docRef.update("capital", true);
    // ...
    WriteResult result = future.get();
    System.out.println("Write result: " + result);
// [END fs_update_doc]
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 7 with WriteResult

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

the class ManageDataSnippets method addDocumentDataAfterAutoGeneratingId.

/**
 * Add data to a document after generating the document id.
 *
 * @return auto generated id
 */
String addDocumentDataAfterAutoGeneratingId() throws Exception {
    City data = new City();
    // [START fs_add_doc_data_after_auto_id]
    // Add document data after generating an id.
    DocumentReference addedDocRef = db.collection("cities").document();
    System.out.println("Added document with ID: " + addedDocRef.getId());
    // later...
    ApiFuture<WriteResult> writeResult = addedDocRef.set(data);
    // [END fs_add_doc_data_after_auto_id]
    // writeResult.get() blocks on operation
    System.out.println("Update time : " + writeResult.get().getUpdateTime());
    return addedDocRef.getId();
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 8 with WriteResult

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

the class ManageDataSnippets method writeBatch.

/**
 * Write documents in a batch.
 */
void writeBatch() throws Exception {
    db.collection("cities").document("SF").set(new City()).get();
    db.collection("cities").document("LA").set(new City()).get();
    // [START fs_write_batch]
    // Get a new write batch
    WriteBatch batch = db.batch();
    // Set the value of 'NYC'
    DocumentReference nycRef = db.collection("cities").document("NYC");
    batch.set(nycRef, new City());
    // Update the population of 'SF'
    DocumentReference sfRef = db.collection("cities").document("SF");
    batch.update(sfRef, "population", 1000000L);
    // Delete the city 'LA'
    DocumentReference laRef = db.collection("cities").document("LA");
    batch.delete(laRef);
    // asynchronously commit the batch
    ApiFuture<List<WriteResult>> future = batch.commit();
    // future.get() blocks on batch commit operation
    for (WriteResult result : future.get()) {
        System.out.println("Update time : " + result.getUpdateTime());
    }
// [END fs_write_batch]
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) ArrayList(java.util.ArrayList) List(java.util.List) City(com.example.firestore.snippets.model.City) WriteBatch(com.google.cloud.firestore.WriteBatch) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 9 with WriteResult

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

the class ManageDataSnippets method updateServerTimestamp.

/**
 * Update document with server timestamp.
 */
void updateServerTimestamp() throws Exception {
    db.collection("objects").document("some-id").set(new HashMap<String, Object>()).get();
    // [START fs_update_server_timestamp]
    DocumentReference docRef = db.collection("objects").document("some-id");
    // Update the timestamp field with the value from the server
    ApiFuture<WriteResult> writeResult = docRef.update("timestamp", FieldValue.serverTimestamp());
    System.out.println("Update time : " + writeResult.get());
// [END fs_update_server_timestamp]
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) HashMap(java.util.HashMap) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 10 with WriteResult

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

the class TranslateServlet method doPost.

// [END getting_started_background_app_list]
/**
 * 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);
    }
}
Also used : Translation(com.google.cloud.translate.Translation) CollectionReference(com.google.cloud.firestore.CollectionReference) ServletException(javax.servlet.ServletException) WriteResult(com.google.cloud.firestore.WriteResult) Firestore(com.google.cloud.firestore.Firestore) PubSubMessage(com.getstarted.background.objects.PubSubMessage) TranslateMessage(com.getstarted.background.objects.TranslateMessage) ExecutionException(java.util.concurrent.ExecutionException) Translate(com.google.cloud.translate.Translate)

Aggregations

WriteResult (com.google.cloud.firestore.WriteResult)13 DocumentReference (com.google.cloud.firestore.DocumentReference)8 City (com.example.firestore.snippets.model.City)6 HashMap (java.util.HashMap)5 PubSubMessage (com.getstarted.background.objects.PubSubMessage)1 TranslateMessage (com.getstarted.background.objects.TranslateMessage)1 CollectionReference (com.google.cloud.firestore.CollectionReference)1 Firestore (com.google.cloud.firestore.Firestore)1 WriteBatch (com.google.cloud.firestore.WriteBatch)1 Translate (com.google.cloud.translate.Translate)1 Translation (com.google.cloud.translate.Translation)1 ImmutableList (com.google.common.collect.ImmutableList)1 ProtoChange (com.google.startupos.common.firestore.ProtoChange)1 ProtoQuerySnapshot (com.google.startupos.common.firestore.ProtoQuerySnapshot)1 CiRequest (com.google.startupos.tools.reviewer.ReviewerProtos.CiRequest)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 ServletException (javax.servlet.ServletException)1