开发者

How to do batch Google DataStore key lookup query in JDO

I have about 50k entities stored in appengine. I am able to look up an individual record via the GQL admin interf开发者_JAVA百科ace with a query like:

SELECT * FROM Pet where __key__ = KEY( 'Pet','Fido')

But I'm having trouble figuring out how to do a batch version of this via JDO. Right now I have this:

    PersistenceManager pm = ...;
    for(Pet pet : pets) {
        for(String k : getAllAliases(pet)) {
            keys.add(KeyFactory.createKeyString(Pet.class.getSimpleName(), k));
        }
    }
    Query q = pm.newQuery("select from " + Pet.class.getName() + " where id == :keys");
    List<Pet> petlist = (List<Pet>) q.execute(keys);

But though 'Fido' works in the GQL case, it returns nothing when I use that Java + JDO code. What am I doing wrong?


Don't use queries to fetch entities by key - they're inefficient, and require a query for each object retrieved! Instead, get the objects by key. Bulk fetches by key don't appear to be supported by JDO, but fetching them by key one at a time is still going to be a lot more efficient than doing queries for them.


Likely doesn't work in the JDOQL case since it isn't valid JDOQL :-P

Collection keys = ... (create your keys collection)
Query q = pm.newQuery("SELECT FROM " + Pet.class.getName() + " WHERE :keys.contains(id)");

i.e the filter uses Java syntax


http://gae-java-persistence.blogspot.com/2009/10/executing-batch-gets.html

I didn't try the JDO version, but the JPA version does work!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜