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

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

connecting to firestore emulator from java admin sdk

问题

  1. 我正在尝试从我的Java应用程序内部连接到Firestore模拟器但并不成功
  2. FirebaseOptions options = FirebaseOptions.builder()
  3. .setCredentials(GoogleCredentials.getApplicationDefault())
  4. .setDatabaseUrl("localhost:8082")
  5. .setProjectId("xxx")
  6. .build();
  7. var app = FirebaseApp.initializeApp(options);
  8. Firestore db = FirestoreClient.getFirestore(app);
  9. final ApiFuture<WriteResult> update = db.collection("users").document().set(Map.of("test", "mest"));
  10. try {
  11. final WriteResult writeResult = update.get();
  12. } catch (InterruptedException | ExecutionException e) {
  13. e.printStackTrace();
  14. }
  15. 这段代码尝试连接到Firebase忽略了数据库URL
  16. 我查看了Node SDK它有一个emulator方法和数据库设置但在Java端不存在
英文:

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

  1. FirebaseOptions options = FirebaseOptions.builder()
  2. .setCredentials(GoogleCredentials.getApplicationDefault())
  3. .setDatabaseUrl(&quot;localhost:8082&quot;)
  4. .setProjectId(&quot;xxx&quot;)
  5. .build();
  6. var app = FirebaseApp.initializeApp(options);
  7. Firestore db = FirestoreClient.getFirestore(app);
  8. final ApiFuture&lt;WriteResult&gt; update = db.collection(&quot;users&quot;).document().set(Map.of(&quot;test&quot;, &quot;mest&quot;));
  9. try {
  10. final WriteResult writeResult = update.get();
  11. } catch (InterruptedException | ExecutionException e) {
  12. e.printStackTrace();
  13. }

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环境变量:

  1. import com.google.cloud.firestore.FirestoreOptions;
  2. import com.google.firebase.FirebaseApp;
  3. import com.google.firebase.FirebaseOptions;
  4. import com.google.firebase.internal.EmulatorCredentials;
  5. FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder()
  6. /*
  7. TODO:用模拟器正在运行的主机替换
  8. */
  9. .setEmulatorHost("localhost:8080")
  10. .build();
  11. FirebaseOptions firebaseOptions = FirebaseOptions.builder()
  12. .setCredentials(new EmulatorCredentials())
  13. /*
  14. 设置有效的项目ID很重要;
  15. 否则,数据在模拟器界面内将不可见
  16. */
  17. .setProjectId("[YOUR_FIREBASE_PROJECT_ID]")
  18. .setFirestoreOptions(firestoreOptions)
  19. .build();
  20. 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:

  1. import com.google.cloud.firestore.FirestoreOptions;
  2. import com.google.firebase.FirebaseApp;
  3. import com.google.firebase.FirebaseOptions;
  4. import com.google.firebase.internal.EmulatorCredentials;
  5. FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder()
  6. /*
  7. TODO: replace with host at which your emulator is running
  8. */
  9. .setEmulatorHost(&quot;localhost:8080&quot;)
  10. .build();
  11. FirebaseOptions firebaseOptions = FirebaseOptions.builder()
  12. .setCredentials(new EmulatorCredentials())
  13. /*
  14. setting valid project-id is IMPORTANT;
  15. otherwise, data will not be visible inside Emulator UI
  16. */
  17. .setProjectId(&quot;[YOUR_FIREBASE_PROJECT_ID]&quot;)
  18. .setFirestoreOptions(firestoreOptions)
  19. .build()
  20. 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

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

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

这对我有用。

后续编辑

使用

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

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

英文:

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

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

It worked for me.

LATER EDIT

Use

  1. .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:

确定