谷歌数据存储获取功能

huangapple go评论79阅读模式
英文:

Google Datastore Get Functionality

问题

从Google Datastore使用Java库(com.google.cloud.datastore.Datastore)读取实体时,当调用get(key)方法时,是否会从数据库中检索出整个实体及其所有属性?还是在引用时是否会单独加载实体的每个单独属性?或者是在首次访问实体属性时是否会一次性检索整个实体?

Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
Entity entity = datastore.get(key); //在这里是否加载整个实体及其属性?
entity.getString("name"); //还是在访问时加载实体/属性?

另外,如果在我获取实体的同时对实体进行更新,那么我收到的实体是否保证是一致的。(即是否有可能收到部分写入的实体,其中一些属性是写入的更新值,而另一些是尚未反映更新的旧值?)

英文:

When I read an entity from Google Datastore using the Java library (com.google.cloud.datastore.Datastore), is the entire entity and all of its properties pulled from the database when the get(key) method is called, is each individual property of the entity loaded separately at the time of reference, or is the entire entity pulled when I first access an entity property?

Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
Entity entity = datastore.get(key); //Is the entire entity and its properties loaded here?
entity.getString("name"); //or is the entity/property loaded upon access?

Also, if the entity is being updated at the same time that I get the entity, is the entity that I receive guaranteed to be consistent. (i.e. Is it possible to receive a partially written entity, where some of the properties are the updated values from the write and some are old values that do not reflect the update yet?)

答案1

得分: 1

让我们检查来自接口com.google.cloud.datastore.DatastoreReader继承的官方库文档Datastore.get(key)方法,并返回给定键的实体。实体是持久数据对象,在这里您有来自类com.google.cloud.datastore.BaseEntity继承的方法getString(String name),它将属性值作为字符串返回,就像过滤您所需的属性一样。
根据前面所述和面向对象编程概念,'Entity entity = datastore.get(key);' 会将具有所有属性的整个实体返回,并将其保存在 'entity' 变量中。

关于数据一致性,Datastore查询可以在两种一致性级别(强一致性和最终一致性)中的任一级别上交付其结果,您需要在一致性和速度之间取得平衡,具体取决于应用程序的需求。我诚恳地建议阅读12 文档,以获取有关在Datastore中如何运作的更多澄清。

英文:

Let's inspect the official library documentation Datastore.get(key) method inherited from interface com.google.cloud.datastore.DatastoreReader and return Entity for the given key. An entity is a persistent data object and you have here method getString(String name) inherited from class com.google.cloud.datastore.BaseEntity which returns the property value as a string, like filter what properties you need.
According to the foregoing and OOP concepts 'Entity entity = datastore.get(key);' returns you whole entity with all properties and saves it in 'entity' variable.

Regarding the data consistency Datastore queries can deliver their results at either of two consistency levels strong and eventually and you need to balance between consistency and speed what depending your app needs. I kindly recommend reading 1,2 documentation to get more clarification on how it works in Datastore.

huangapple
  • 本文由 发表于 2020年3月16日 03:52:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/60697008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定