Discussion, code samples and video demos of new technologies; including Web 2.0 startups, Google AppEngine, Ruby on Rails, PHP, Visual Studio Team System, Team Foundation Server and .NET.

Tuesday, April 15, 2008

Google AppEngine Data model

Hey guys,

I've done a little experimenting with the data model that is part of Google AppEngine so far I've been pretty impressed. As you probably know, the underlying storage is done with Google's BigTable.

From a programming model perspective, they have done a really nice job of making it easy to work with BigTable. For example, the following declaration will create your table and columns.


There are a couple of really interesting featuring in that simple bit of code. First, notice that my string property description does not have a size associated with it. The StringProperty data type is good for data under 500K. They have a TextProperty for anything bigger than that. All of their data types are described here. The noticeable one that I'm not using here is their key property; this enables you to reference another table (or class, same thing really).

Also notice the required = True statements. When these are added, the constructor for your class, TimeEntry, in this case is parameterized with these values. This is an easy way to do some validation. Since these fields are required, you can't construct an instance of this class unless you specify these values.

Getting at your data is really simple. They support a structured query language called GQL (Google Query Language), or you can use methods. I like the latter method. The following code will give me a list of all my DeepTimeEntry rows (or objects, same thing really) that match the user that I have specified, ordered by date.


I was a bit leery of using this at first because if you read the order of the methods, it looks like you would get all the results, then filter them and then sort them. But in reading Google's documentation about this it looks like the data is not actually retrieved until you try to access it. So the underlying Google objects essentially optimize your query based on that constructs you have used. At least that is how I read the documentation – if that is not the case, I hope someone lets me know!

I won't admit how long I was stuck on this J but the whitespace around 'user = ' is important. You have to have the spaces between the 'user' and the '=' , otherwise the filter is misinterpreted. For some reason or another, that wasn't immediately obvious to me.

Saving and deleting your data is pretty trivial – you basically call put() and delete() respectively.

One thing that I could not find in the methods or query approach is a way to do a 'like' query. That seemed odd that Google wouldn't provide this.

I'm still getting used to Python, but the programming model that Google has for their data in AppEngine seems pretty easy to use; this makes AppEngine a really attractive platform choice.

Thanks!

Eric.

2 Comments:

Anonymous Geetha said...

Eric,This information is very useful..However i am wondering is there any way i can declare my field as an auto number generated automatically when a new record is created??

May 29, 2008 4:55 PM

 
Blogger ericlee said...

Hi Geetha,

Thanks, I'm glad you found it helpful!

Automatically incrementing numbers are kind of a touchy subject in the Google AppEngine Data Model. It is mostly because their data model is highly distributed, so there are lots of synchronization concerns.

There is a really good thread on this in the Google AppEngine discussion forums (http://groups.google.com/group/google-appengine/browse_thread/thread/7dedb7d65bdf4f/9dae33280edf8aea?lnk=gst&q=data+increment+value#9dae33280edf8aea)

There is a link to some sample code that does an auto-incrementing value, but you may want to read through the entire thread to see what potential issues there may be.

Thanks for reading!

Eric.

May 29, 2008 8:19 PM

 

Post a Comment

Links to this post:

Create a Link

<< Home