简体   繁体   中英

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.

I resolve it now. What I should do is doing following official tutorial( https://firebase.google.com/docs/firestore/quickstart ). However, because I use Visual Studio Code to edit java program, and I don't have any plugin to adapt "Auto Import" about external library, I found this situation as a hard problem.

For other people who will come here: The introduction of Java in tutorial doesn't have careful import sentence. The best and direct way to resolve it is reading official reference( https://googleapis.dev/java/google-cloud-firestore/latest/index.html ) and write in your java program with import sentences.

This question is starting from my misunderstanding. I appreciate all people to help me about this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM