So I am trying to access an already existing datastore on google cloud. As this screen-shot suggests, the datastore already exists and is called "default". I believe it is a Firestore database in the "datastore" mode (as opposed to the "native" mode).
My goal: get all records in the "Countries" kind in the "default" datastore db. Proof that data exists in the "Countries" kind is in the pic below...
Here are the various options that I have tried with all the respective and associated Exceptions on the cloud server log:
Option 1:
Code:
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();
Exception: Invalid database id /projects/<my-project-number>/databases/(default)
Highlight: setDatabaseId("/projects/my-project-number/databases/(default)")
Option 2:
Code:
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();
Exception: Invalid database id /projects/<my-project-name>/databases/(default)
Highlight: setDatabaseId("/projects/my-project-name/databases/(default)")
Option 3:
Code:
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();
Exception: Invalid database id /databases/(default)
Highlight: setDatabaseId("/databases/(default)")
Option 4:
Code:
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();
Exception: (default) is not a valid databaseId. Please use the empty string to denote the (default) database
Highlight: setDatabaseId("(default)")
So, finally......the last option....
Option 5:
Code:
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();
Exception: The database (default) does not exist for project <my-project-number>
Highlight: setDatabaseId("")
Option 6:
Code: Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
Exception: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential
Highlight: getDefaultInstance()
The exception thrown at Option 5 above is not in agreement with the first pic above where the datastore database "default" actually exists and has a Kind named Countries and the Countries kind has data in it. Scoured through the documentation but could not find how to set the DB name.
Question: What am I doing wrong?
Edit:
My pom.xml contains, among other things, the following:
<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>
Edit:
Up until 30Jan2024, I was accessing this database using DataNucleus and everything as working was expected. I am now trying other competing options (because DN does not work with Java11) to access the same DB. The first option was the google-cloud-datastore API which is the subject matter of this question.
Edit:
I created a new database "clinixdemods", exported all data from the (default) DB to a bucket and imported from the bucket into the "clinixdemods" DB and tried to connect again with this line of code....
DatastoreOptions.newBuilder().setProjectId("9999999999999").setDatabaseId("clinixdemods").setCredentials(googleCredentials).build();
Again I got this exception:
The database clinixdemods does not exist for project 9999999999999
Requesting help from anyone who has any knowledge about this. I have searched all over the place but have not been able to find any solution.
Please help!
Edit:
After Jim's comment...... If this is what you meant....
dsOptions= DatastoreOptions.newBuilder().setProjectId("99999999999").setCredentials(googleCredentials).build();
Datastore datastore = dsOptions.getService();
......then, here is the result......
The database (default) does not exist for project
Edit:
I printed some attributes of the DatastoreOption object and this is what I got:
DEFAULT 2024-02-01T17:29:04.600154Z lib name: gcloud-java
DEFAULT 2024-02-01T17:29:04.600138Z google api client lib name: gccl
DEFAULT 2024-02-01T17:29:04.600118Z def proj id: myprojectname
DEFAULT 2024-02-01T17:29:04.600114Z app engine app id: myprojectname
DEFAULT 2024-02-01T17:29:04.600037Z user agent: null
DEFAULT 2024-02-01T17:29:04.600020Z project id: 99999999999
DEFAULT 2024-02-01T17:29:04.599992Z namespace:
DEFAULT 2024-02-01T17:29:04.599973Z lib version: 2.18.2
DEFAULT 2024-02-01T17:29:04.599947Z host: https://datastore.googleapis.com
DEFAULT 2024-02-01T17:29:04.599926Z database id:
DEFAULT 2024-02-01T17:29:04.599915Z appl name: gcloud-java/2.18.2
By now, I am pulling my hair out!!!!
Edit:
And when I change my code to this....
dsOptions=DatastoreOptions.newBuilder().setProjectId("99999999999").setDatabaseId("clinixdemods").setCredentials(googleCredentials).build();
Datastore datastore = dsOptions.getService();
this is what is printed in the log
DEFAULT 2024-02-01T17:59:28.192557Z lib name: gcloud-java
DEFAULT 2024-02-01T17:59:28.192538Z google api client lib name: gccl
DEFAULT 2024-02-01T17:59:28.192522Z def proj id: clinixdemo
DEFAULT 2024-02-01T17:59:28.192490Z app engine app id: clinixdemo
DEFAULT 2024-02-01T17:59:28.192482Z user agent: null
DEFAULT 2024-02-01T17:59:28.192447Z project id: 515812274702
DEFAULT 2024-02-01T17:59:28.192439Z namespace:
DEFAULT 2024-02-01T17:59:28.192423Z lib version: 2.18.2
DEFAULT 2024-02-01T17:59:28.192417Z host: https://datastore.googleapis.com
DEFAULT 2024-02-01T17:59:28.192410Z database id: clinixdemods
DEFAULT 2024-02-01T17:59:28.192397Z appl name: gcloud-java/2.18.2
and this is the exception....
The database clinixdemods does not exist for project 9999999999
But the DB indeed exists.....



If you are using the
(default)database with Datastore client libraries, then you can access the database by not setting the database id.