Tuesday, September 20, 2011

Lessons Learnt Using Objectify with Google App Engine's Bigtable

1. Import the correct @Embedded library

If you are getting errors like "... is not a supported property type" when trying to save an entity, check that the your import line is:
import javax.persistence.Embedded
and not
import javax.jdo.annotations.Embedded.
(This is one of those things that took me 3 hours to debug before realising the suggested import Eclipse gave me was wrong).

2. Example of how to Register Your Classes Using a DAO
According to Objectify's Best Practices, you should be using a Data Access Object (DAO) to register your classes, however the example they give may be a bit limited.

David M. Chandler has an excellent example here which you can use.

I'm using RPC in my code so in your RpcServiceImpl.java so to add a new class to the datastore you can use code like the following:

@Override
public void addTemplate(Template template)
throws IllegalArgumentException {
ObjectifyDao<Template> objDao = new ObjectifyDao<Template>(Template.class);
objDao.put(template);
}

and substitute Template for the class that you have defined.

3. Think in Bigtable and not in Relational Database mode
This is a bit of a tricky one to explain (and more of a Bigtable lesson learnt), but basically the strength of BigTable is how it manages denormalized data.  So instead of multiple tables to store information (RDBMS), Bigtable uses well ... one bigtable to store the information.  

This affects how you define your classes, because Objectify doesn't have inner joins (select from table A values matching those of in table B).  Instead data is retrieved and then filtered.  You should review the Query Class information that Google provides when creating your classes for more information, but as long as you remember denormalised data you should find designing your classes pretty easy.

If you have any other lessons learnt from using Objectify, I'd be glad to hear from you.



No comments:

Post a Comment