如何使用C++或Java控制台应用程序操作我的Cloud Firestore项目?

huangapple go评论70阅读模式
英文:

How to use C++ or Java Console Application to operate my Cloud Firestore project?

问题

以下是您提供的内容的翻译部分:

我在我的 Android 应用程序中使用 Cloud Firestore 存储数据,然后我想制作一个支持工具,以便轻松操作我的 Cloud Firestore 项目。(通过 Web 页面 GUI 逐个添加超过 100 条数据到 Cloud Firestore 在我和我的手指看来是非常困难和枯燥的。)

因此,我想制作一个支持工具来实现以下功能:

  1. 读取 CSV 文件(我知道如何在 C++ 或 Java 中执行此操作)
  2. 连接到我的 Cloud Firestore 项目
  3. 操作(添加或删除)源自 CSV 文件的数据。

我阅读了谷歌官方的入门指南 "开始使用 Cloud Firestore"(https://firebase.google.com/docs/firestore/quickstart#java_1),并进行了以下操作。

  1. 安装 Gradle
  2. 在以下语句中设置 User 环境变量(位置是由 Cloud Firestore 服务帐户生成的秘密 JSON 文件)。
GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
  1. 编写以下内容的 "build.gradle" 文件。
    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. 编写以下 Java 文件。
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.*;
//以下两个包不存在,由于 Gradle 的编译。
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("没有异常!");

            Firestore db = FirestoreClient.getFirestore();
            //根据我的调查,如果我删除剩下的代码(DocumentReference...~println();}),我就可以成功编译。

            DocumentReference ref = db.collection("AppVersion").document("Android");
            ApiFuture<DocumentSnapshot> future = ref.get();

            DocumentSnapshot document = future.get();
            if (document.exists()) {
                System.out.println("android 应用版本是 " + document.getData());
            } else {
                System.out.println("没有这样的文档!");
            }

        } catch (IOException e) {
            System.out.println("发生 IOException!");
        }
    }
}

我设置了 "Firestore Admin SDK",我错了吗?
如果有人知道如何解决这个问题,如果您能告诉我,我将非常高兴听取您宝贵的建议。
这是我的第一个问题,我不是母语为英语的人。请原谅我难以理解的问题。

英文:

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=&quot;C:\Users\username\Downloads\service-account-file.json&quot;
  1. write "build.gradle" as following sentence.
    apply plugin: &#39;java&#39;
    apply plugin: &#39;application&#39;

    mainClassName = &#39;Main&#39;

    repositories {
        mavenCentral()
    }

    dependencies {
        implementation &#39;com.google.firebase:firebase-admin:6.11.0&#39;
        implementation &#39;com.google.firebase:firebase-firestore:21.2.1&#39;
    }
  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&#39;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(&quot;https://rikotenapp2020.firebaseio.com&quot;).build();
			FirebaseApp.initializeApp(options);

			System.out.println(&quot;no exception!&quot;);

			Firestore db = FirestoreClient.getFirestore();
            //from my survey, if I erase the rest code(DocumentReference...~println();})
            //I can compile it successfully.

			DocumentReference ref = db.collection(&quot;AppVersion&quot;).document(&quot;Android&quot;);
			ApiFuture&lt;DocumentSnapshot&gt; future = ref.get();

			DocumentSnapshot document = future.get();
			if (document.exists()) {
				System.out.println(&quot;android app version is &quot; + document.getData());
			} else {
				System.out.println(&quot;No such document!&quot;);
			}

		} catch (IOException e) {
			System.out.println(&quot;IOException happened!&quot;);
		}
	}
}

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.

答案1

得分: 2

我现在解决了。
我应该做的是按照官方教程进行操作(https://firebase.google.com/docs/firestore/quickstart)。
然而,因为我使用Visual Studio Code来编辑Java程序,并且我没有任何适应外部库“自动导入”的插件,我将这种情况视为一个棘手的问题。

对于其他将要前来的人:
教程中关于Java的介绍没有仔细的导入语句。解决这个问题的最佳直接方法是阅读官方参考(https://googleapis.dev/java/google-cloud-firestore/latest/index.html),并在你的Java程序中编写带有导入语句的代码。

这个问题源于我的误解。我感谢所有帮助我解决这个问题的人。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月17日 18:10:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63935838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定