我正在尝试使用java admin sdk向Cloud Firestore数据库添加一些数据。我正在按照文档中的步骤进行初始化:
InputStream serviceAccount = Main.class.getClassLoader().getResourceAsStream("xxx-service-account.json");
GoogleCredentials credentials =
GoogleCredentials.fromStream(serviceAccount);
FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder().setTimestampsInSnapshotsEnabled(true).build();
Firestore firestore = firestoreOptions.getService();
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(credentials)
.build();
FirebaseApp.initializeApp(options);
Firestore db = FirestoreClient.getFirestore();
然后我尝试添加一些数据,如下所示:
Map<String, Object> docData = new HashMap<>();
System.out.println("DOCTOR LIST SIZE: "+doctorList.size());
for (Doctor doctor: doctorList) {
docData.put("name", doctor.getName());
ApiFuture<WriteResult> future =
db.collection("Doktor").document().set(docData);
System.out.println("FUTURE : "+future.toString());
ApiFutures.addCallback(future, new ApiFutureCallback<WriteResult>(){
@Override
public void onFailure(Throwable t) {
System.out.println("PUSH DOCTORS FAILURE: "+t.getCause());
}
@Override
public void onSuccess(WriteResult result) {
System.out.println("PUSH DOCTORS SUCCESS!");
}
});
但是我无法将任何数据写入Firestore。 Future.isDone()始终为false。有人可以帮我弄清楚我在做什么错吗?有什么建议吗?谢谢。