|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.example.firestore; |
| 16 | + |
| 17 | +import com.google.api.core.ApiFuture; |
| 18 | +import com.google.cloud.firestore.DocumentReference; |
| 19 | +import com.google.cloud.firestore.DocumentSnapshot; |
| 20 | +// [START fs_include_dependencies] |
| 21 | +import com.google.cloud.firestore.Firestore; |
| 22 | +import com.google.cloud.firestore.FirestoreOptions; |
| 23 | +// [END fs_include_dependencies] |
| 24 | +import com.google.cloud.firestore.QuerySnapshot; |
| 25 | +import com.google.cloud.firestore.WriteResult; |
| 26 | +import com.google.common.collect.ImmutableMap; |
| 27 | + |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +/** |
| 33 | + * A simple Quick start application demonstrating how to connect to Firestore |
| 34 | + * and add and query documents. |
| 35 | + */ |
| 36 | +public class Quickstart { |
| 37 | + |
| 38 | + private Firestore db; |
| 39 | + |
| 40 | + /** |
| 41 | + * Initialize Firestore using default project ID. |
| 42 | + */ |
| 43 | + public Quickstart() { |
| 44 | + // [START fs_initialize] |
| 45 | + Firestore db = FirestoreOptions.getDefaultInstance().getService(); |
| 46 | + // [END fs_initialize] |
| 47 | + this.db = db; |
| 48 | + } |
| 49 | + |
| 50 | + public Quickstart(String projectId) { |
| 51 | + // [START fs_initialize_project_id] |
| 52 | + FirestoreOptions firestoreOptions = |
| 53 | + FirestoreOptions.getDefaultInstance().toBuilder() |
| 54 | + .setProjectId(projectId) |
| 55 | + .build(); |
| 56 | + Firestore db = firestoreOptions.getService(); |
| 57 | + // [END fs_initialize_project_id] |
| 58 | + this.db = db; |
| 59 | + } |
| 60 | + |
| 61 | + Firestore getDb() { |
| 62 | + return db; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Add named test documents with fields first, last, middle (optional), born. |
| 67 | + * |
| 68 | + * @param docName document name |
| 69 | + */ |
| 70 | + void addDocument(String docName) throws Exception { |
| 71 | + switch (docName) { |
| 72 | + case "alovelace": { |
| 73 | + // [START fs_add_data_1] |
| 74 | + DocumentReference docRef = db.collection("users").document("alovelace"); |
| 75 | + // Add document data with id "alovelace" using a hashmap |
| 76 | + Map<String, Object> data = new HashMap<>(); |
| 77 | + data.put("first", "Ada"); |
| 78 | + data.put("last", "Lovelace"); |
| 79 | + data.put("born", 1815); |
| 80 | + //asynchronously write data |
| 81 | + ApiFuture<WriteResult> result = docRef.set(data); |
| 82 | + // ... |
| 83 | + // result.get() blocks on response |
| 84 | + System.out.println("Update time : " + result.get().getUpdateTime()); |
| 85 | + // [END fs_add_data_1] |
| 86 | + break; |
| 87 | + } |
| 88 | + case "aturing": { |
| 89 | + // [START fs_add_data_2] |
| 90 | + DocumentReference docRef = db.collection("users").document("aturing"); |
| 91 | + // Add document data with an additional field ("middle") |
| 92 | + Map<String, Object> data = new HashMap<>(); |
| 93 | + data.put("first", "Alan"); |
| 94 | + data.put("middle", "Mathison"); |
| 95 | + data.put("last", "Turing"); |
| 96 | + data.put("born", 1912); |
| 97 | + |
| 98 | + ApiFuture<WriteResult> result = docRef.set(data); |
| 99 | + System.out.println("Update time : " + result.get().getUpdateTime()); |
| 100 | + // [END fs_add_data_2] |
| 101 | + break; |
| 102 | + } |
| 103 | + case "cbabbage": { |
| 104 | + DocumentReference docRef = db.collection("users").document("cbabbage"); |
| 105 | + Map<String, Object> data = |
| 106 | + new ImmutableMap.Builder<String, Object>() |
| 107 | + .put("first", "Charles") |
| 108 | + .put("last", "Babbage") |
| 109 | + .put("born", 1791) |
| 110 | + .build(); |
| 111 | + ApiFuture<WriteResult> result = docRef.set(data); |
| 112 | + System.out.println("Update time : " + result.get().getUpdateTime()); |
| 113 | + break; |
| 114 | + } |
| 115 | + default: |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + void runAQuery() throws Exception { |
| 120 | + // [START fs_add_query] |
| 121 | + // asynchronously query for all users born before 1900 |
| 122 | + ApiFuture<QuerySnapshot> query = |
| 123 | + db.collection("users").whereLessThan("born", 1900).get(); |
| 124 | + // ... |
| 125 | + // query.get() blocks on response |
| 126 | + QuerySnapshot querySnapshot = query.get(); |
| 127 | + List<DocumentSnapshot> documents = querySnapshot.getDocuments(); |
| 128 | + for (DocumentSnapshot document : documents) { |
| 129 | + System.out.println("User: " + document.getId()); |
| 130 | + System.out.println("First: " + document.getString("first")); |
| 131 | + if (document.contains("middle")) { |
| 132 | + System.out.println("Middle: " + document.getString("middle")); |
| 133 | + } |
| 134 | + System.out.println("Last: " + document.getString("last")); |
| 135 | + System.out.println("Born: " + document.getLong("born")); |
| 136 | + } |
| 137 | + // [END fs_add_query] |
| 138 | + } |
| 139 | + |
| 140 | + void retrieveAllDocuments() throws Exception { |
| 141 | + // [START fs_get_all] |
| 142 | + // asynchronously retrieve all users |
| 143 | + ApiFuture<QuerySnapshot> query = db.collection("users").get(); |
| 144 | + // ... |
| 145 | + // query.get() blocks on response |
| 146 | + QuerySnapshot querySnapshot = query.get(); |
| 147 | + List<DocumentSnapshot> documents = querySnapshot.getDocuments(); |
| 148 | + for (DocumentSnapshot document : documents) { |
| 149 | + System.out.println("User: " + document.getId()); |
| 150 | + System.out.println("First: " + document.getString("first")); |
| 151 | + if (document.contains("middle")) { |
| 152 | + System.out.println("Middle: " + document.getString("middle")); |
| 153 | + } |
| 154 | + System.out.println("Last: " + document.getString("last")); |
| 155 | + System.out.println("Born: " + document.getLong("born")); |
| 156 | + } |
| 157 | + // [END fs_get_all] |
| 158 | + } |
| 159 | + |
| 160 | + void run() throws Exception { |
| 161 | + String[] docNames = {"alovelace", "aturing", "cbabbage"}; |
| 162 | + |
| 163 | + // Adding document 1 |
| 164 | + System.out.println("########## Adding document 1 ##########"); |
| 165 | + addDocument(docNames[0]); |
| 166 | + |
| 167 | + // Adding document 2 |
| 168 | + System.out.println("########## Adding document 2 ##########"); |
| 169 | + addDocument(docNames[1]); |
| 170 | + |
| 171 | + // Adding document 3 |
| 172 | + System.out.println("########## Adding document 3 ##########"); |
| 173 | + addDocument(docNames[2]); |
| 174 | + |
| 175 | + // retrieve all users born before 1900 |
| 176 | + System.out.println("########## users born before 1900 ##########"); |
| 177 | + runAQuery(); |
| 178 | + |
| 179 | + // retrieve all users |
| 180 | + System.out.println("########## All users ##########"); |
| 181 | + retrieveAllDocuments(); |
| 182 | + System.out.println("###################################"); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * A quick start application to get started with Firestore. |
| 187 | + * |
| 188 | + * @param args firestore-project-id (optional) |
| 189 | + */ |
| 190 | + public static void main(String[] args) throws Exception { |
| 191 | + // default project is will be used if project-id argument is not available |
| 192 | + String projectId = (args.length == 0) ? null : args[0]; |
| 193 | + Quickstart quickStart = (projectId != null) ? new Quickstart(projectId) : new Quickstart(); |
| 194 | + quickStart.run(); |
| 195 | + } |
| 196 | +} |
0 commit comments