连接到 Java Admin SDK 中的 Firestore 模拟器。

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

connecting to firestore emulator from java admin sdk

问题

我正在尝试从我的Java应用程序内部连接到Firestore模拟器但并不成功

FirebaseOptions options = FirebaseOptions.builder()
        .setCredentials(GoogleCredentials.getApplicationDefault())
        .setDatabaseUrl("localhost:8082")
        .setProjectId("xxx")
        .build();
var app = FirebaseApp.initializeApp(options);

Firestore db = FirestoreClient.getFirestore(app);
final ApiFuture<WriteResult> update = db.collection("users").document().set(Map.of("test", "mest"));
try {
    final WriteResult writeResult = update.get();
} catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
}

这段代码尝试连接到Firebase忽略了数据库URL

我查看了Node SDK它有一个emulator方法和数据库设置但在Java端不存在
英文:

I am trying to connect to firestore emulator from inside my java application, without much success.

FirebaseOptions options = FirebaseOptions.builder()
            .setCredentials(GoogleCredentials.getApplicationDefault())
            .setDatabaseUrl(&quot;localhost:8082&quot;)

            .setProjectId(&quot;xxx&quot;)
            .build();
    var app = FirebaseApp.initializeApp(options);

    Firestore db = FirestoreClient.getFirestore(app);
    final ApiFuture&lt;WriteResult&gt; update = db.collection(&quot;users&quot;).document().set(Map.of(&quot;test&quot;, &quot;mest&quot;));
    try {
        final WriteResult writeResult = update.get();
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

This simply tries to connect to firebase, disregarding the database url.

I looked at the node sdk, which has a emulator method and database settings, but they don't exist on Java side.

答案1

得分: 1

如果您正在使用Firebase Admin SDK(Java),您可以使用以下代码连接到本地模拟器套件,而无需设置FIRESTORE_EMULATOR_HOST环境变量:

import com.google.cloud.firestore.FirestoreOptions;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.internal.EmulatorCredentials;

FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder()
    /* 
      TODO:用模拟器正在运行的主机替换
    */
    .setEmulatorHost("localhost:8080")
    .build();

FirebaseOptions firebaseOptions = FirebaseOptions.builder()
    .setCredentials(new EmulatorCredentials())
    /* 
      设置有效的项目ID很重要;
      否则,数据在模拟器界面内将不可见
    */
    .setProjectId("[YOUR_FIREBASE_PROJECT_ID]")
    .setFirestoreOptions(firestoreOptions)
    .build();

FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions);
英文:

If you are using Firebase Admin SDK (Java), you can use following code to connect to Local Emulator Suite without setting the FIRESTORE_EMULATOR_HOST environment variable:

import com.google.cloud.firestore.FirestoreOptions;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.internal.EmulatorCredentials;

FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder()
    /* 
      TODO: replace with host at which your emulator is running
    */
    .setEmulatorHost(&quot;localhost:8080&quot;)
    .build();

FirebaseOptions firebaseOptions = FirebaseOptions.builder()
    .setCredentials(new EmulatorCredentials())
    /* 
      setting valid project-id is IMPORTANT;
      otherwise, data will not be visible inside Emulator UI
    */
    .setProjectId(&quot;[YOUR_FIREBASE_PROJECT_ID]&quot;)
    .setFirestoreOptions(firestoreOptions)
    .build()

FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions)

答案2

得分: 0

根据文档

> 当设置了FIRESTORE_EMULATOR_HOST环境变量时,Firebase Admin SDK会自动连接到Cloud Firestore模拟器:
>
>export FIRESTORE_EMULATOR_HOST="localhost:8080"

所以我猜无论传递什么主机,只要存在这个环境变量,就没有关系?

英文:

According to the documentation:

> The Firebase Admin SDKs automatically connect to the Cloud Firestore emulator when the FIRESTORE_EMULATOR_HOST environment variable is set:
>
>export FIRESTORE_EMULATOR_HOST="localhost:8080"

So I guess it doesn't matter what host you pass, so long as this environment is present?

答案3

得分: 0

我在数据库链接方面遇到了类似的问题
你可以尝试

.setDatabaseUrl("http://localhost:8082")

这对我有用。

后续编辑

使用

.setDatabaseUrl("http://localhost:8082?ns=fake-project-id")

缺少最后一部分会在后续的网络套接字中触发其他错误。

英文:

I had a similar issue with the database link
You could try

.setDatabaseUrl(&quot;http://localhost:8082&quot;)

It worked for me.

LATER EDIT

Use

.setDatabaseUrl(&quot;http://localhost:8082?ns=fake-project-id&quot;)

The lack of the last part would trigger other errors later on websockets.

huangapple
  • 本文由 发表于 2020年9月8日 00:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63781560.html
匿名

发表评论

匿名网友

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

确定