Debugging Google Datastore: IllegalArgumentException: projectId must match the following pattern

41 views
Skip to first unread message

Javier Delgado via StackOverflow

unread,
Nov 28, 2016, 3:40:06 AM11/28/16
to google-appengin...@googlegroups.com

I am developing an appengine project and storing my data using Google Datastore. I would like to debug it but I still have the exception java.lang.IllegalArgumentException: projectId must match the following pattern: ([a-z\d\-]{1,100}~)?([a-z\d][a-z\d\-\.]{0,99}:)?([a-z\d][a-z\d\-]{0,99})

I am able to debug my appengine project and I can deploy it and store data in Google Datastore correclty.

To allow debugging, I have followed the instructions of the Datastore Emulator:

https://cloud.google.com/datastore/docs/tools/datastore-emulator

I have installed the Google Cloud SDK and I start it correcty using the defaults of my project id (and I have also tried setting my own project id by command line).

This is the command that I am executing in gcloud:

gcloud beta emulators datastore start --project="my-project-name"

WARNING: Reusing existing data in [C:\gcloud\emulators\datastore]. Executing: cmd /c C:\google-cloud-sdk\platform\cloud-datastore-emulator\cloud_datastore_emulator.cmd start --host=localhost --port=8806 --store_on_disk=True --consistency=0.9 --allow_remote_shutdown C:\gcloud\emulators\datastore [datastore] nov 27, 2016 5:08:17 PM com.google.cloud.datastore.emulator.CloudDatastore$FakeDatastoreAction$7 apply [datastore] INFORMACI?N: Provided --allow_remote_shutdown to start command which is no longer necessary. [datastore] nov 27, 2016 5:08:17 PM com.google.cloud.datastore.emulator.impl.LocalDatastoreFileStub [datastore] INFORMACI?N: Local Datastore initialized: [datastore]
Type: High Replication [datastore] Storage: C:\gcloud\emulators\datastore\WEB-INF\appengine-generated\local_db.bin [datastore] nov 27, 2016 5:08:17 PM io.grpc.internal.ManagedChannelImpl [datastore] INFORMACI?N: [ManagedChannelImpl@5f71c76a] Created with target localhost:8806 [datastore] nov 27, 2016 5:08:17 PM com.google.cloud.datastore.emulator.impl.LocalDatastoreFileStub load [datastore] INFORMACI?N: The backing store, C:\gcloud\emulators\datastore\WEB-INF\appengine-generated\local_db.bin, does not exist. It will be created. [datastore] nov 27, 2016 5:08:17 PM io.gapi.emulators.netty.NettyUtil applyJava7LongHostnameWorkaround [datastore] INFORMACI?N: Unable to apply Java 7 long hostname workaround. [datastore] API endpoint: http://localhost:8806 [datastore] If you are using a library that supports the DATASTORE_EMULATOR_HOST environment variable, run: [datastore] [datastore] export DATASTORE_EMULATOR_HOST=localhost:8806 [datastore] [datastore] Dev App Server is now running. [datastore]

And this is the stacktrace when I try to use it:

java.lang.IllegalArgumentException: projectId must match the following pattern: ([a-z\d-]{1,100}~)?([a-z\d][a-z\d-.]{0,99}:)?([a-z\d][a-z\d-]{0,99}) at com.google.common.base.Preconditions.checkArgument(Preconditions.java:122) at com.google.cloud.datastore.Validator.validateDatabase(Validator.java:42) at com.google.cloud.datastore.BaseKey$Builder.(BaseKey.java:58) at com.google.cloud.datastore.KeyFactory.(KeyFactory.java:35) at com.google.cloud.datastore.DatastoreHelper.newKeyFactory(DatastoreHelper.java:59) at com.google.cloud.datastore.DatastoreImpl.newKeyFactory(DatastoreImpl.java:371) at com.myproject.api.CronServlet.saveToDataStore(CronServlet.java:187) at com.myproject.api.CronServlet.doGet(CronServlet.java:105) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166) at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:128) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:50) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366) at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349) at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:511) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

EDIT: Code (I didn´t include because I think the issue is not in the code, as I deploy and it works perfectly). The exception is raised in 'datastore.newKeyFactory()'

Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
Key taskKey = datastore.newKeyFactory().setKind(ENTITY_TYPE_MEDIDAS).
    newKey(Calendar.getInstance(TimeZone.getTimeZone("CET")).getTimeInMillis());
Entity.Builder task = Entity.newBuilder(taskKey)
        .set(PROPERTY_COMPUESTO, DATA_COMPUESTO)
        .set(PROPERTY_AVISO,DATA_AVISO);
Entity entity = task.build();
datastore.put(entity);


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/40831513/debugging-google-datastore-illegalargumentexception-projectid-must-match-the-f

Sai Pullabhotla via StackOverflow

unread,
Nov 29, 2016, 12:30:05 PM11/29/16
to google-appengin...@googlegroups.com

It is not clear if you are looking for an example to connect to the Datastore Emulator (for development) or the real Datastore on Google Cloud Platform. It looks like your primary objective is to debug your code from an IDE, which you can do either way.

For connecting to the Datastore Emulator - see the below post:

Google Datastore Emulator using Java (Not using GAE)

For connecting to the Datastore on GCP -

If you have not run the glcoud init command, run it and follow the on-screen instructions to set up default project and auth credentials. Then you can use the below code to access the real Datastore:

Datastore datastore = DatastoreOptions.getDefaultInstance().getService();

Other option is to set project ID and Auth Credentials within your code using the DatastoreOptions.Builder. You need to get/download the JSON Credentials from your Google Cloud Console.

See the below links for more info/sample code:

https://github.com/GoogleCloudPlatform/google-cloud-java#specifying-a-project-id

https://github.com/GoogleCloudPlatform/google-cloud-java#authentication



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/40831513/debugging-google-datastore-illegalargumentexception-projectid-must-match-the-f/40872085#40872085

Javier Delgado via StackOverflow

unread,
Nov 29, 2016, 5:35:04 PM11/29/16
to google-appengin...@googlegroups.com

I had an issue in my project I was using both com.google.appengine.api.datastore and com.google.cloud.datastore

Actually, the issue was only in the methods depending on the second library, so I deleted if from my project and now I can run my datastore locally.

Maybe someone finds this information useful. If you want to use com.google.cloud.datastore you can follow @Sai Pullabhotla's answer.

In the other hand, if you use com.google.appengine.api.datastore you don't event need to launch the gcloud tools. You just need to use appengine's datastore.

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/40831513/debugging-google-datastore-illegalargumentexception-projectid-must-match-the-f/40876996#40876996
Reply all
Reply to author
Forward
0 new messages