Javier Delgado Javier Delgado - 9 days ago 4
Java Question

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

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);

Answer

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();