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