java 检查 Entity 是否存在于谷歌应用引擎数据存储区。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10176033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:56:12  来源:igfitidea点击:

Checking if Entity exists in google app engine datastore.

javagoogle-app-engineentitygoogle-cloud-datastore

提问by Victor

What is the best/fastest way to check if an Entity exists in a google-app-engine datastore? For now I'm trying to get the entity by key and checking if the get() returns an error.

检查 google-app-engine 数据存储区中是否存在实体的最佳/最快方法是什么?现在我正在尝试通过键获取实体并检查 get() 是否返回错误。

I don't know the process of getting an Entity on the datastore. Is there a faster way for doing only this check?

我不知道在数据存储上获取实体的过程。有没有更快的方法来做这个检查?

采纳答案by Eric Leschinski

com.google.appengine.apihas been deprecated in favor of the App Engine GCS client.

com.google.appengine.api已弃用,取而代之的是 App Engine GCS 客户端。

Have you considered using a query? Guess-and-check is not a scalable way to find out of an entity exists in a data store. A query can be created to retrieve entities from the datastore that meet a specified set of conditions:

您是否考虑过使用查询?猜测和检查不是找出数据存储中存在的实体的可扩展方式。可以创建查询以从满足一组指定条件的数据存储中检索实体:

https://developers.google.com/appengine/docs/java/datastore/queries

https://developers.google.com/appengine/docs/java/datastore/queries

EDIT:

编辑:

What about the key-only query? Key-only queries run faster than queries that return complete entities. To return only the keys, use the Query.setKeysOnly() method.

仅键查询呢?仅键查询比返回完整实体的查询运行得更快。要仅返回键,请使用 Query.setKeysOnly() 方法。

new Query("Kind").addFilter(Entity.KEY_RESERVED_PROPERTY, FilterOperator.EQUAL, key).setKeysOnly();

Source: [1]: http://groups.google.com/group/google-appengine-java/browse_thread/thread/b1d1bb69f0635d46/0e2ba938fad3a543?pli=1

来源:[1]:http: //groups.google.com/group/google-appengine-java/browse_thread/thread/b1d1bb69f0635d46/0e2ba938fad3a543?pli=1

回答by dragonx

What you proposed would indeed be the fastest way to know if your entity exists. The only thing slowing you down is the time it takes to fetch and deserialize your entity. If your entity is large, this can slow you down.

您提出的确实是了解您的实体是否存在的最快方法。唯一让您慢下来的是获取和反序列化实体所需的时间。如果您的实体很大,这可能会减慢您的速度。

IF this action (checking for existence) is a major bottleneck for you and you have large entities, you may want to roll your own system of checking by using two entities - first you would have your existing entity with data, and a second entity that either stores the reference to the real entity, or perhaps an empty entity where the key is just a variation on the original entity key that you can compute. You can check for existence quickly using the 2nd entity, and then fetch the first entity only if the data is necessary.

如果此操作(检查是否存在)是您的主要瓶颈并且您拥有大型实体,则您可能希望通过使用两个实体来推出自己的检查系统 - 首先,您将拥有包含数据的现有实体,以及第二个实体要么存储对真实实体的引用,要么存储一个空实体,其中键只是您可以计算的原始实体键的变体。您可以使用第二个实体快速检查是否存在,然后仅在需要数据时才获取第一个实体。

The better way I think would just be to design your keys such they you know there would not be duplicates, or that your operations are idempotent, so that even if an old entity was overwritten, it wouldn't matter.

我认为更好的方法是设计你的密钥,让他们知道不会有重复,或者你的操作是幂等的,这样即使旧实体被覆盖,也没关系。

回答by Jason Hall

You could fetch using a List<Key>containing only one Key, that methodreturns a Map<Key, Entity>which you can check if it contains an actual value or null, for example:

您可以使用List<Key>仅包含 one 的fetch Key该方法返回 aMap<Key, Entity>您可以检查它是否包含实际值或null,例如:

Entity e = datastoreService.get(Arrays.asList(key)).get(key);

In general though I think it'd be easier to wrap the get()in a try/catch that returns nullif the EntityNotFoundExceptionis caught.

一般来说,虽然我认为将 包装get()在 try/catch 中会更容易,null如果捕获 则返回EntityNotFoundException