0

我正在尝试将此 Python 代码从演示文稿 Building Scalable, Complex Apps on App Engine翻译成 Java ,

GAEJ 是否支持这样做?

indexes = db.GqlQuery( "SELECT __key__ FROM MessageIndex " "WHERE receivers = :1", me)
keys = [k.parent() for k in indexes] 
messages = db.get(keys)

下面是我在 Java 中的实现。这些功能和性能是否等效?如果我有大量密钥会怎样?

Query query1 = new Query("MessageIndex");
query1.setKeysOnly();
query1.addFilter("receivers", FilterOperator.EQUAL, me);

PreparedQuery pq2 = datastore.prepare(query1);

Iterable<Entity> entities = pq2.asIterable();
HashSet<Key> keys = new HashSet<Key>();
    for (Entity en : entities)
       keys.add(en.getParent());

Query query2 = new Query("Messages");
query2.addFilter("__key__", FilterOperator.IN, keys);
4

2 回答 2

1

正如@Rick 已经指出的那样,获取比查询更快、更便宜。您只需要更改代码的最后一部分。代替:

Query query2 = new Query("Messages");
query2.addFilter("__key__", FilterOperator.IN, keys);

做就是了:

Map<Key,Entity> messages = datastore.get(keys);
于 2012-04-08T07:59:13.727 回答
0

查看 DatastoreService API https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/DatastoreService

DatastoreService.get(keys) 比使用键查询更有效。

于 2012-04-08T02:03:32.213 回答