英文:
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("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();
}
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("localhost:8080")
.build();
FirebaseOptions firebaseOptions = FirebaseOptions.builder()
.setCredentials(new EmulatorCredentials())
/*
setting valid project-id is IMPORTANT;
otherwise, data will not be visible inside Emulator UI
*/
.setProjectId("[YOUR_FIREBASE_PROJECT_ID]")
.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("http://localhost:8082")
It worked for me.
LATER EDIT
Use
.setDatabaseUrl("http://localhost:8082?ns=fake-project-id")
The lack of the last part would trigger other errors later on websockets.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论