所以我试图访问谷歌云上已经存在的数据存储。正如此屏幕截图所示,数据存储已经存在并称为“默认”。我相信它是“数据存储”模式下的 Firestore 数据库(而不是“本机”模式)。
我的目标:获取“默认”中“国家”类型的所有记录 数据存储数据库。数据存在于“国家/地区”类型的证明位于 下图...
以下是我针对云服务器日志上的所有相应和相关异常尝试过的各种选项:
代码:
pathToKeyFile = "WEB-INF/<my-project-name>-<my-project-number>.json";
sac = ServiceAccountCredentials.fromStream(new FileInputStream(new File(pathToKeyFile)));
googleCredentials = sac.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
dsOptions = DatastoreOptions.newBuilder().setProjectId("<my-project-number>").setCredentials(googleCredentials).setDatabaseId("/projects/<my-project-number>/databases/(default)").build();
Datastore datastore = dsOptions.getService();
例外:
Invalid database id /projects/<my-project-number>/databases/(default)
亮点:setDatabaseId("/projects/my-project-number/databases/(default)")
代码:
pathToKeyFile = "WEB-INF/<my-project-name>-<my-project-number>.json";
sac = ServiceAccountCredentials.fromStream(new FileInputStream(new File(pathToKeyFile)));
googleCredentials = sac.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
dsOptions = DatastoreOptions.newBuilder().setProjectId("<my-project-number>").setCredentials(googleCredentials).setDatabaseId("/projects/my-project-name/databases/(default)").build();
Datastore datastore = dsOptions.getService();
例外:
Invalid database id /projects/<my-project-name>/databases/(default)
重点:setDatabaseId("/projects/my-project-name/databases/(default)")
代码:
pathToKeyFile = "WEB-INF/<my-project-name>-<my-project-number>.json";
sac = ServiceAccountCredentials.fromStream(new FileInputStream(new File(pathToKeyFile)));
googleCredentials = sac.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
dsOptions = DatastoreOptions.newBuilder().setProjectId("<my-project-number>").setCredentials(googleCredentials).setDatabaseId("/databases/(default)").build();
Datastore datastore = dsOptions.getService();
例外:
Invalid database id /databases/(default)
重点:setDatabaseId("/databases/(default)")
代码:
pathToKeyFile = "WEB-INF/<my-project-name>-<my-project-number>.json";
sac = ServiceAccountCredentials.fromStream(new FileInputStream(new File(pathToKeyFile)));
googleCredentials = sac.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
dsOptions = DatastoreOptions.newBuilder().setProjectId("<my-project-number>").setCredentials(googleCredentials).setDatabaseId("(default)").build();
Datastore datastore = dsOptions.getService();
例外:
(default) is not a valid databaseId. Please use the empty string to denote the (default) database
重点:setDatabaseId("(默认)")
所以,最后......最后一个选择......
代码:
pathToKeyFile = "WEB-INF/<my-project-name>-<my-project-number>.json";
sac = ServiceAccountCredentials.fromStream(new FileInputStream(new File(pathToKeyFile)));
googleCredentials = sac.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
dsOptions = DatastoreOptions.newBuilder().setProjectId("<my-project-number>").setCredentials(googleCredentials).setDatabaseId("").build();
Datastore datastore = dsOptions.getService();
例外:
The database (default) does not exist for project <my-project-number>
重点:setDatabaseId("")
代码:数据存储 datastore = DatastoreOptions.getDefaultInstance().getService();
例外:
Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential
重点:getDefaultInstance()
上面选项 5 抛出的异常与上面第一张图片不一致,其中数据存储数据库“默认”实际存在,并且有一个名为“国家”的种类,并且“国家/地区”种类中包含数据。翻阅文档但找不到如何设置数据库名称。
问题:我做错了什么?
编辑:
我的 pom.xml 包含以下内容:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.31.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
......
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-datastore</artifactId>
</dependency>
.......
</dependencies>
编辑: 直到 2024 年 1 月 30 日,我一直在使用 DataNucleus 访问该数据库,一切都按预期工作。我现在正在尝试其他竞争选项(因为 DN 不适用于 Java11)来访问同一数据库。第一个选项是
google-cloud-datastore
API,这是这个问题的主题。
如果您将
(default)
数据库与 Datastore 客户端库一起使用,则可以通过不设置数据库 ID 来访问数据库。