首页 > 解决方案 > 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

  1. read CSV file(I know how to do this in C++ or Java)
  2. connect to my Cloud Firestore project
  3. 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.

  1. install gradle
  2. 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"
  1. 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'
    }
  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.

标签: javagoogle-cloud-firestoreconsole-application

解决方案


我现在解决它。我应该做的是遵循官方教程(https://firebase.google.com/docs/firestore/quickstart)。但是,由于我使用 Visual Studio Code 编辑 java 程序,并且我没有任何插件来适应外部库的“自动导入”,我发现这种情况是一个难题。

对于其他会来这里的人:教程中Java的介绍没有仔细的导入语句。解决它的最好和直接的方法是阅读官方参考资料(https://googleapis.dev/java/google-cloud-firestore/latest/index.html)并在你的java程序中写入import语句。

这个问题是从我的误解开始的。我感谢所有人帮助我解决这个问题。


推荐阅读