GAE persistence with Google API

Google App Engine (GAE) corresponds to a cloud platform (PAAS = Platform As A Service) that allows hosting Web applications. Java is supported here based on Java EE but the platform enforces some restrictions in order to guarantee performance and service quality. We will not provide here a full description of the platform but focuses on the persistence mechanisms it provides. GAE uses DataStore as persistence internally based BigTable, developed by Google. Some solutions are available to interact with it with the Java technology:

– A low-level API provided by Google
– JDO / JPA implementation if you want to use ORM (Object Relational Mapping)

We will focus in this post on the first option.

Using low-level DataStore Java API of GAE

From our point of view, GAE doesn’t provide a convenient tool to manage your data from the console ( This tool should display all metadata and corresponding values. Such tool is mandatory when developing GAE applications. As a matter of fact, it allows manipulating data independently from your application. Moreover, when using JDO / JPA, data structures are automagically created or updated and this can cause problems (null values…). The tool should make possible to make the data consistent.

The first step consists in getting instance to interact with the DatastoreService of GAE. Documentation related to this API is available at the address Such instance is reachable as described below:

DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

The obtained instance makes possible to execute both metadata and data requests on the datastore. The first step corresponds to get all data kinds present in the database. For this particular case, the API provides the KIND_METADATA_KIND constant on the Query class. The following code describes how to list all data kinds of the database for the application:

Query q = new Query(Query.KIND_METADATA_KIND);

List kinds = new ArrayList();
for (Entity e : datastore.prepare(q).asIterable()) {
  if (e.getKey().getId() != 0) {
    //doing nothing
  } else {

We can then get entities corresponding to a particular kind, for instance Client or User. The query in this case is very simple since it corresponds to the kind name itself, as described below:

Query q = new Query(kind);

List entities = new ArrayList();
for (Entity e : datastore.prepare(q).asIterable()) {

Manipulating entities is quite simple using the put and delete methods of the DatastoreService class. The put method adds an entity if it doesn’t exist and updates it otherwise. The following code describes how to create, update and delete an entity:

Key key = KeyFactory.createKey("<ENTITY-KIND", "PK-VALUE");
Entity entity = new Entity(key);


Since the database is of kind nosql, entities own a list of properties. Displaying related hints can be done with the following code:

Key key = KeyFactory.createKey("<ENTITY-KIND", "PK-VALUE");
Entity entity = datastore.get(key);
String entityId = entity.getKey().getId();
String entityName = entity.getKey().getName();
Map properties = entity.getProperties();
for (String propertyName : properties.keySet()) {
  Object propertyValue = properties.get(propertyName);
  System.out.println("- Property "+propertyName+": "+propertyValue);

Some methods of the Entity class allow manipulating properties, as described below:

Entity entity = (...);
//adding a property with name admin and value false
entity.setProperty("admin", false);
//removing the property field


In a next post, we will see how to expose data present in the data store of your GAE application using REST and Restlet.

This entry was posted in GAE, Persistence. Bookmark the permalink.

2 Responses to GAE persistence with Google API

  1. Jack says:

    Is the Datastore persistent? Does data stored in Datastore survive a restart of GAE application? Or should I use other methods like JDO?

    • templth says:

      Yes, the datastore is persistent across application restarts for both local and remote environments. Using the low-level API (described in this post) or a more high-level one (like JDO or JPA) doesn’t change rhis behavior. Hope it helps.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s