从数据存储中移除旧条目

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

Removing older entries from datastore

问题

我正在使用Java查询Google Cloud Datastore,以便判断一个条目是否已经存在超过一天,如果是,则删除该条目。我的代码类似于这样:

    if (requestUploaded == true) {

     datastore.delete(row_to_delete);

我想添加一个逻辑:

a)获取当前时间。

b)然后检查数据存储条目是否早于一天,如果是,则删除。

谢谢!

英文:

I am using java to query google cloud datastore in order to find out whether an entry has been there more than one day, and if yes then delete the entry. I have something like this:

    if (requestUploaded == true) {

     datastore.delete(row_to_delete);

I want to add a logic where:

a) I get the current time.

b) Then check if datastore entry is older than one day, if so, then delete.

Thanks!

答案1

得分: 1

根据您对问题的解释,我不知道 Cloud Datastore 实体的创建时间是否有任何内置方法,因此我的解释假设您的实体中有一个 created 属性。

获取当前时间

您可以构造一个新的 Date 对象,不传递任何参数,以获取当前日期和时间:

import java.util.Date;

Date d = new Date();

或者,根据 文档,在创建实体时可以使用 Timestamp.now()

Entity task = Entity.newBuilder(taskKey)
    .set("category", "Personal")
    .set("created", Timestamp.now())
    .set("description",
      StringValue.newBuilder("Learn Cloud Datastore").setExcludeFromIndexes(true).build())
    .build();

将实体的创建时间与当前日期和时间进行比较

您可以使用以下示例代码,根据 created 属性(表示实体创建的日期和时间)与当前时间(由 Timestamp.now() 定义)进行比较,从而获取所有实体,其中 created 属性小于当前时间:

Query<Entity> query = Query.newEntityQueryBuilder()
    .setKind("Person")
    .setFilter(PropertyFilter.lt("created", Timestamp.now()))
    .build();
QueryResults<Entity> results = datastore.run(query);
while (results.hasNext()) {
  Entity currentEntity = results.next();
  System.out.println(currentEntity.getString("name"));
}

您可以参考 PropertyFilter 操作符的文档,了解有关过滤的更多细节。

删除实体

最后,您可以按给定的键 删除 实体:

datastore.delete(taskKey);
英文:

Based on your explanation of the issue, I am not aware of any built-in method for creation time in Cloud Datastore entities, so my explanation assumes that you have a created property in your entity.

Getting current time

You can construct a new Date object without any arguments to get the current date and time:

import java.util.Date;

Date d = new Date();

Optionally, you can use Timestamp.now() when creating your Entity, as per the documentation:

Entity task = Entity.newBuilder(taskKey)
    .set(&quot;category&quot;, &quot;Personal&quot;)
    .set(&quot;created&quot;, Timestamp.now())
    .set(&quot;description&quot;,
      StringValue.newBuilder(&quot;Learn Cloud Datastore&quot;).setExcludeFromIndexes(true).build())
    .build();

Comparing the entity's creation time to the current date and time

You can leverage the following sample code to fetch all entities where created property, which is the date and time that the entity was created, is less than the current time, which is defined by Timestamp.now()

Query&lt;Entity&gt; query = Query.newEntityQueryBuilder()
    .setKind(&quot;Person&quot;)
    .setFilter(PropertyFilter.lt(&quot;created&quot;, Timestamp.now()))
    .build();
QueryResults&lt;Entity&gt; results = datastore.run(query);
while (results.hasNext()) {
  Entity currentEntity = results.next();
  System.out.println(currentEntity.getString(&quot;name&quot;));
}

You can refer to the PropertyFilter operator documentation for more details on filtering.

Deleting an entity

Lastly, you can delete an entity by its given key:

datastore.delete(taskKey);

huangapple
  • 本文由 发表于 2020年10月8日 05:26:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64252531.html
匿名

发表评论

匿名网友

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

确定