Search in sources :

Example 1 with DocumentReference

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);
}
Also used : DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) FirestoreException(com.google.cloud.firestore.FirestoreException) Map(java.util.Map) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 2 with DocumentReference

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;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 3 with DocumentReference

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;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 4 with DocumentReference

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]
}
Also used : WriteResult(com.google.cloud.firestore.WriteResult) HashMap(java.util.HashMap) City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference)

Example 5 with DocumentReference

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;
}
Also used : QueryDocumentSnapshot(com.google.cloud.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.cloud.firestore.DocumentSnapshot) Transaction(com.google.cloud.firestore.Transaction) City(com.example.firestore.snippets.model.City) DocumentReference(com.google.cloud.firestore.DocumentReference)

Aggregations

DocumentReference (com.google.cloud.firestore.DocumentReference)26 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)14 Test (org.junit.Test)14 City (com.example.firestore.snippets.model.City)12 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)7 WriteResult (com.google.cloud.firestore.WriteResult)7 HashMap (java.util.HashMap)5 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)4 Transaction (com.google.cloud.firestore.Transaction)2 CollectionReference (com.google.cloud.firestore.CollectionReference)1 FirestoreException (com.google.cloud.firestore.FirestoreException)1 WriteBatch (com.google.cloud.firestore.WriteBatch)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1