Wednesday, July 22, 2009

Google app engine - dynamic entities with Java

Google app engine is a super cool platform to cheaply and easily develop scalable applications. Forget about complicated deployments, expensive maintenance, OS updates, web servers configurations – it just works!

Beside all the sweets Google are providing (Memcache, fetching of URLs, mail, signing using Google accounts) – you get a direct access to Google’s bigtable datastore (Google’s column store scalable database). Under their current limitations you can use up to 10M daily! calls to the datastore for free. Nice honey trap.

Google app engine supports two developing languages – Python and Java. Python is too hard to debug, thus I used Java on Ecplise. works like magic.

While Python is a dynamic, untyped, interpreter base language – Java is a very strict; I dare you to add a member variable to a class in runtime. On the other hand it keeps you nice and cozy and prevents you from doing stupid things.

As I wrote above – Google Bigtable is a column store database. Column store database (in the application perspective) means that there is no fix/predefine number of columns per table. Just add a new columns when needed, and the system will take care of it.

So, how to use this dynamic columns that bigtable offers in Java – simple, don’t use JDO (what Google suggest you to do). Instead go use the low-level APIs.

How does it work ? here:
Entity entity = new Entity("EntityName");

entity.setProperty(“Col1”, “Col1Value”);

entity.setProperty(“Col2”, “Col2Value”);

DatastoreService dsService = DatastoreServiceFactory.getDatastoreService();

dsService.put(entity);

Well, there’s more then that – you can query, delete, update, use indexes and more. Simple.