java - How to use C++ or Java Console Application to operate my Cloud Firestore project?
问题描述
I use Cloud Firestore to store data for my Android application, then I want to make a supporting tool to operate my Cloud Firestore project easily.(It is too hard and bore for me and my fingers to add more 100 datas in a constant format to Cloud Firestrore by Webpage GUI.)
Therefore, I want to make a support tool to
- read CSV file(I know how to do this in C++ or Java)
- connect to my Cloud Firestore project
- operate(add or erase) data deriving from CSV file.
I read Google Official start up guide "Get started with Cloud Firestore"(https://firebase.google.com/docs/firestore/quickstart#java_1), and did following things.
- install gradle
- Set User environment variable in following sentence.(the location is the secret json file made by Cloud Firestore Service Account.)
GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
- write "build.gradle" as following sentence.
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'Main'
repositories {
mavenCentral()
}
dependencies {
implementation 'com.google.firebase:firebase-admin:6.11.0'
implementation 'com.google.firebase:firebase-firestore:21.2.1'
}
- write following java file.
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.Firestore;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import java.io.*;
//the following two package does not exist, by gradle's compiling.
import com.google.firebase.cloud.*;
import com.google.firebase.firestore.*;
public class Main {
public static void main(String args[]) {
try {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.getApplicationDefault())
.setDatabaseUrl("https://rikotenapp2020.firebaseio.com").build();
FirebaseApp.initializeApp(options);
System.out.println("no exception!");
Firestore db = FirestoreClient.getFirestore();
//from my survey, if I erase the rest code(DocumentReference...~println();})
//I can compile it successfully.
DocumentReference ref = db.collection("AppVersion").document("Android");
ApiFuture<DocumentSnapshot> future = ref.get();
DocumentSnapshot document = future.get();
if (document.exists()) {
System.out.println("android app version is " + document.getData());
} else {
System.out.println("No such document!");
}
} catch (IOException e) {
System.out.println("IOException happened!");
}
}
}
I set up "Firestore Admin SDK", was I wrong? If someone know how to resolve this, I'm very glad to get your valuable advices if you can tell me. This is my first question, and I'm not native English speaker.Please forgive my hard-understand question.
解决方案
我现在解决它。我应该做的是遵循官方教程(https://firebase.google.com/docs/firestore/quickstart)。但是,由于我使用 Visual Studio Code 编辑 java 程序,并且我没有任何插件来适应外部库的“自动导入”,我发现这种情况是一个难题。
对于其他会来这里的人:教程中Java的介绍没有仔细的导入语句。解决它的最好和直接的方法是阅读官方参考资料(https://googleapis.dev/java/google-cloud-firestore/latest/index.html)并在你的java程序中写入import语句。
这个问题是从我的误解开始的。我感谢所有人帮助我解决这个问题。
推荐阅读
- web-scraping - 当“按钮”输入字段没有名称时如何使用 Python 请求登录
- swift - 迅速。将北向表示为航向方向的范围
- python - 将 PilImage 转换为 reportlab
- laravel - 无法在 Laravel 中更新用户信息
- r - R-选择满足 R 中数百列的特定条件的行
- php - Laravel Eloquent:选择相关表有 x 的位置,但从相关表中排除某些行
- php - 无法让 url mod_rewrite 在 xampp(windows 8)中工作
- python - Tweepy 抛出回溯错误?
- haskell - 任意类型类的约束中的非类型变量参数
- javascript - 静默访问 Vue 实例上不存在的属性失败