I wanted to import production environment data locally with GAE / J, so I tried using the Remote API. This section describes how to retrieve data by remote access from GAE in the local environment to production and save the retrieved data in the local Datastore.
It is implemented in the following versions.
Add the following to the project web.xml.
web.xml
<servlet>
<display-name>Remote API Servlet</display-name>
<servlet-name>RemoteApiServlet</servlet-name>
<servlet-class>com.google.apphosting.utils.remoteapi.RemoteApiServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RemoteApiServlet</servlet-name>
<url-pattern>/remote_api</url-pattern>
</servlet-mapping>
Add $ {JAVA-SDK-ROOT} /lib/appengine-remote-api.jar
to the WEB-INF / lib
directory and put it in your classpath to take advantage of the Remote API client components.
Import the data of Kind name "book" locally.
import com.google.appengine.tools.remoteapi.RemoteApiInstaller;
import com.google.appengine.tools.remoteapi.RemoteApiOptions;
// ...
RemoteApiOptions options = new RemoteApiOptions()
.server("your_app_id.appspot.com", 443)
.useApplicationDefaultCredential();
RemoteApiInstaller installer = new RemoteApiInstaller();
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
List<Entity> books;
try {
installer.install(options);
Query q = new Query("book");
books = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
} finally {
installer.uninstall();
}
//Migrate data for local storage
List<Entity> devBooks = new ArrayList<>();
for (Entity book : books) {
Entity devBook = new Entity(book.getKind(), book.getKey().getName());
devBook.setPropertiesFrom(book);
devBooks.add(devBook);
}
datastore.put(devBooks);
It didn't work if I just used datastore.put ()
to get the data from production, but I was able to get it locally using the above method.
Recommended Posts