use of com.google.cloud.firestore.DocumentReference 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.DocumentReference 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;
}
use of com.google.cloud.firestore.DocumentReference in project java-docs-samples by GoogleCloudPlatform.
the class RetrieveDataSnippets method getDocumentAsEntity.
/**
* Retrieves document in collection as a custom object.
*
* @return document data as City object
*/
public City getDocumentAsEntity() throws Exception {
// [START fs_get_doc_as_entity]
DocumentReference docRef = db.collection("cities").document("BJ");
// asynchronously retrieve the document
ApiFuture<DocumentSnapshot> future = docRef.get();
// block on response
DocumentSnapshot document = future.get();
City city = null;
if (document.exists()) {
// convert document to POJO
city = document.toObject(City.class);
System.out.println(city);
} else {
System.out.println("No such document!");
}
// [END fs_get_doc_as_entity]
return city;
}
use of com.google.cloud.firestore.DocumentReference in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method deleteFields.
/**
* Delete specific fields when updating a document.
*/
void deleteFields() throws Exception {
City city = new City("Beijing");
city.setCapital(true);
db.collection("cities").document("BJ").set(city).get();
// [START fs_delete_fields]
DocumentReference docRef = db.collection("cities").document("BJ");
Map<String, Object> updates = new HashMap<>();
updates.put("capital", FieldValue.delete());
// Update and delete the "capital" field in the document
ApiFuture<WriteResult> writeResult = docRef.update(updates);
System.out.println("Update time : " + writeResult.get());
// [END fs_delete_fields]
}
use of com.google.cloud.firestore.DocumentReference in project java-docs-samples by GoogleCloudPlatform.
the class ManageDataSnippets method runSimpleTransaction.
// [END fs_delete_collection]
/**
* Run a simple transaction to perform a field value increment.
*
* @return transaction future
*/
ApiFuture<Void> runSimpleTransaction() throws Exception {
// [START fs_run_simple_transaction]
// Initialize doc
final DocumentReference docRef = db.collection("cities").document("SF");
City city = new City("SF");
city.setCountry("USA");
city.setPopulation(860000L);
docRef.set(city).get();
// run an asynchronous transaction
ApiFuture<Void> transaction = db.runTransaction(new Transaction.Function<Void>() {
@Override
public Void updateCallback(Transaction transaction) throws Exception {
// retrieve document and increment population field
DocumentSnapshot snapshot = transaction.get(docRef).get();
long oldPopulation = snapshot.getLong("population");
transaction.update(docRef, "population", oldPopulation + 1);
return null;
}
});
// [END fs_run_simple_transaction]
return transaction;
}
Aggregations