Search in sources :

Example 1 with City

use of com.example.firestore.snippets.model.City in project java-docs-samples by GoogleCloudPlatform.

the class QueryDataSnippets method prepareExamples.

/**
 * Creates cities collection and add sample documents to test queries.
 *
 * @return collection reference
 */
void prepareExamples() throws Exception {
    // [START fs_query_create_examples]
    CollectionReference cities = db.collection("cities");
    List<ApiFuture<WriteResult>> futures = new ArrayList<>();
    futures.add(cities.document("SF").set(new City("San Francisco", "CA", "USA", false, 860000L)));
    futures.add(cities.document("LA").set(new City("Los Angeles", "CA", "USA", false, 3900000L)));
    futures.add(cities.document("DC").set(new City("Washington D.C.", null, "USA", true, 680000L)));
    futures.add(cities.document("TOK").set(new City("Tokyo", null, "Japan", true, 9000000L)));
    futures.add(cities.document("BJ").set(new City("Beijing", null, "China", true, 21500000L)));
    // (optional) block on documents successfully added
    ApiFutures.allAsList(futures).get();
// [END fs_query_create_examples]
}
Also used : ApiFuture(com.google.api.core.ApiFuture) ArrayList(java.util.ArrayList) City(com.example.firestore.snippets.model.City) CollectionReference(com.google.cloud.firestore.CollectionReference)

Example 2 with City

use of com.example.firestore.snippets.model.City 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 3 with City

use of com.example.firestore.snippets.model.City 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 4 with City

use of com.example.firestore.snippets.model.City 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)

Example 5 with City

use of com.example.firestore.snippets.model.City in project java-docs-samples by GoogleCloudPlatform.

the class ManageDataSnippets method updateUsingMap.

/**
 * Partially update fields of a document using a map (field => value).
 */
void updateUsingMap() throws Exception {
    db.collection("cities").document("DC").set(new City("Washington D.C.")).get();
    // [START fs_update_doc_map]
    // update multiple fields using a map
    DocumentReference docRef = db.collection("cities").document("DC");
    Map<String, Object> updates = new HashMap<>();
    updates.put("name", "Washington D.C.");
    updates.put("country", "USA");
    updates.put("capital", true);
    // asynchronously update doc
    ApiFuture<WriteResult> writeResult = docRef.update(updates);
    // ...
    System.out.println("Update time : " + writeResult.get().getUpdateTime());
// [END fs_update_doc_map]
}
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)

Aggregations

City (com.example.firestore.snippets.model.City)16 DocumentReference (com.google.cloud.firestore.DocumentReference)12 BaseIntegrationTest (com.example.firestore.BaseIntegrationTest)6 WriteResult (com.google.cloud.firestore.WriteResult)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)3 ApiFuture (com.google.api.core.ApiFuture)2 CollectionReference (com.google.cloud.firestore.CollectionReference)2 DocumentSnapshot (com.google.cloud.firestore.DocumentSnapshot)2 QueryDocumentSnapshot (com.google.cloud.firestore.QueryDocumentSnapshot)2 HashMap (java.util.HashMap)2 Transaction (com.google.cloud.firestore.Transaction)1 WriteBatch (com.google.cloud.firestore.WriteBatch)1 List (java.util.List)1