how to select first entity from a BigTable

14 views
Skip to first unread message

YF CAO

unread,
Dec 4, 2010, 10:43:10 AM12/4/10
to google-appengine
Hi.
in google app engine, how to select first entity from a bigtable.
like this sql:
select AGE from TABLENAME where order by AGE desc limit 1.
or like:
select max(AGE) from TABLENAME.

Erick Fleming

unread,
Dec 4, 2010, 11:04:02 AM12/4/10
to google-a...@googlegroups.com
are you using python or java?

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.



--
Erick Fleming

Tim Hoffman

unread,
Dec 4, 2010, 11:08:19 AM12/4/10
to Google App Engine
Remember there are no tables. Just kinds representing models.

first_entity = MyModel.all().order('age').get()
last_entity = MYModel.all().order('-age').get()

Rgds

T

Zeynel

unread,
Dec 4, 2010, 11:16:56 AM12/4/10
to Google App Engine
On Dec 4, 11:08 am, Tim Hoffman <zutes...@gmail.com> wrote:
> Remember there are no tables. Just kinds representing models.

I am confused by this statement. When I look at the Development
Console - Datastore Viewer; I see a table named after the model. In my
case, I have a table called User with columns "userEmail", "userName"
and so on. Each row in this table is what the documentation calls an
"instance" of the class. So what is the justification for the
statement that there are no tables?

YF CAO

unread,
Dec 4, 2010, 11:26:14 AM12/4/10
to google-a...@googlegroups.com
Java

2010/12/5 Erick Fleming <eflem...@gmail.com>

Jeff Schwartz

unread,
Dec 4, 2010, 11:31:15 AM12/4/10
to google-a...@googlegroups.com
Conceptually they are tables, but not in the same sense as SQL tables. They are more like a hash map and if you think of them in those terms you will be better served.

Are you coding in Python or Java. If you are coding in Java then here's an example:

query(Some.class).order('age').limit(1).get()

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.




--
Jeff Schwartz

Erick Fleming

unread,
Dec 4, 2010, 11:38:11 AM12/4/10
to google-a...@googlegroups.com
In Java you can use Query cursors to [1] limit your results.  As far as aggregate functions (like MIN and MAX) you can use [2] indexes to achieve the similar result (ie. order by age and get the top result) 

Erick Fleming

YF CAO

unread,
Dec 4, 2010, 11:43:56 AM12/4/10
to google-a...@googlegroups.com
Hey all.
i am coding in java.

To: Jeff Schwartz
Instance of Query has not order method.
i am using Google App Engine for Java 1.3.7


2010/12/5 Jeff Schwartz <jeffts...@gmail.com>

YF CAO

unread,
Dec 4, 2010, 11:53:37 AM12/4/10
to google-a...@googlegroups.com
Thanks to Erick Fleming.
the way is tortuous.

2010/12/5 YF CAO <caoyong...@gmail.com>

Jeff Schwartz

unread,
Dec 4, 2010, 11:54:27 AM12/4/10
to google-a...@googlegroups.com
Sorry, I am using Objectify so here's the low level api examples to accomplish the same thing:

Max age...
Query query = new Query("YourEntityType");
query.addSort('age',Query.SortDirection.DESCENDING);
List<YourEntityType> list = DatastoreServiceFactory.getDatastoreService().prepare(query).asList(withLimit(1));

Least age...
Query query = new Query("YourEntityType");
query.addSort('age',Query.SortDirection.ASCENDING);
List<YourEntityType> list = DatastoreServiceFactory.getDatastoreService().prepare(query).asList(withLimit(1));


Jeff

Jeff Schwartz

unread,
Dec 4, 2010, 11:59:57 AM12/4/10
to google-a...@googlegroups.com
Odd but I don't see him needing either cursors or 2 indexes to achieve his required result and it doesn't even need a filter condition. IMO it just requires a descending ordering with a limit of 1.

YF CAO

unread,
Dec 4, 2010, 12:05:13 PM12/4/10
to google-a...@googlegroups.com
thanks to Jeff Schwartz.
Objectify is very good !

thanks to Erick Fleming too.

2010/12/5 Jeff Schwartz <jeffts...@gmail.com>

Erick Fleming

unread,
Dec 4, 2010, 12:11:55 PM12/4/10
to google-a...@googlegroups.com
Jeff, I interpreted the meat of his question to be related to advanced queries.  As such I was just pointing him to the two areas of documentation that spell out how these queries worked (which also has an example of withLimit).  You had already answered his question with a valid response, so I was just providing the more info.

Jeff Schwartz

unread,
Dec 4, 2010, 12:12:29 PM12/4/10
to google-a...@googlegroups.com
Objectify is a thin wrapper library around the native low level java datastore api. It adds numerous convenience methods but doesn't stray from the low level api. In addition, it allows you to define your models as pojos using java annotations for things like numeric and string key ids, properties which are keys of a parent, etc. I wouldn't think of using anything else. The low level api examples I just gave you map very closely to how you would accomplish the same thing in Objectify.

You can read more about Objectify on their wiki at http://code.google.com/p/objectify-appengine/wiki/IntroductionToObjectify.

Jeff

Jeff Schwartz

unread,
Dec 4, 2010, 12:21:02 PM12/4/10
to google-a...@googlegroups.com
Thanks, Eric. I am sure he will appreciate everyone's effort here to provide help. I know I sure did when I was starting out. The basics were tough, coming from a pure SQL background as I did (though I did have BDAM, VSAM and hierarchial DB expertise from previous generations  - yes, I am an old fart lol and I still have a few COBOL and 360/370 Assembler applications on 80 column punch card decks hidden somewhere. Oh gosh! I am really dating myself here lol), and it if it wasn't for this group's great members and the Google team I'd have gone bonkers quite early in the game.

Jeff

YF CAO

unread,
Dec 4, 2010, 12:27:21 PM12/4/10
to google-a...@googlegroups.com
thanks.
i find objectify project on google code.
in learing, bigtable is most difficult.


2010/12/5 Jeff Schwartz <jeffts...@gmail.com>

Jeff Schwartz

unread,
Dec 4, 2010, 12:32:34 PM12/4/10
to google-a...@googlegroups.com
Just forget everything you know about SQL and you will do just fine.

Jeff
Reply all
Reply to author
Forward
0 new messages