Google App Engine – Low level API for storing objects

Google App Engine for Java permits three ways of storing your data: JDO, JPA and the low-level API. All data is ultimately stored in Bigtable. This blog post will give a little example how you can store data via the low-level API.

The low-level API allows to store entities with their properties directly into the application datastore. Your object which is the database entry will be stored in a row of Bigtable. The fields in your object are represented as properties of the row.

In the low-level API your object is represented by a key, a type (called kind) and a list of key value pairs for the properties. The low-level API allows you use the methods get(), put() and query() on an object called DatastoreService to save and retrieve entities. DatastoreService can be created by the DatastoreServiceFactory.

An entry is identified by its key. To retrieve saved entries you can recreate your key via the method KeyFactory.createKey(kind, value) there kind represent your data type and value the key of this object.

Lets have a look at an example. The following assumes that you have already basic knowledge about web development on the Google App Engine .

Create a new project “de.vogella.gae.java.persistence.lowlevel” and create the following two servlets. The first will create one entry in the datastore the second will retrieve the data.

package de.vogella.gae.java.persistence.lowlevel;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;

@SuppressWarnings("serial")
public class SaveDataServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		Entity entity = new Entity("Todo", "Todo1");
		// Alternatively use
		// Key todoKey = KeyFactory.createKey("Todo", "Todo1");
		// Entity entity = new Entity(todoKey);
		entity.setProperty("summary", "This is my summary");
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		datastore.put(entity);

		resp.setContentType("text/plain");
		resp.getWriter().println("Saved data");
	}
}
package de.vogella.gae.java.persistence.lowlevel;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class GetDataServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		Key todoKey = KeyFactory.createKey("Todo", "Todo1");
		Entity entity;
		try {
			entity = datastore.get(todoKey);
			String summary = (String) entity.getProperty("summary");

			resp.setContentType("text/plain");
			resp.getWriter()
					.println("Got the todo with the summary " + summary);
		} catch (EntityNotFoundException e) {
			resp.setContentType("text/plain");
			resp.getWriter().println("Entry not found.");
		}

	}
}

If you run this project and hit the URL you mapped the Save servlet to then an entry will get created. If you hit the URL of the get servlet this entries will be read from the datastore.

For another example have a look at my Google App Engine Tutorial. You can also follow me on Twitter here.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Web and tagged , , , . Bookmark the permalink.