【问题标题】:Delete records from Google Datastore using simple GQL使用简单的 GQL 从 Google Datastore 中删除记录
【发布时间】:2019-07-11 20:16:54
【问题描述】:

我有一个继承的 Google DataStore 这个数据存储区有大约 9M 条记录。 我想删除所有旧记录(比如超过 1 个月的所有记录)。 一般来说,当我使用 google cloud console Datastore->query by kind 时,我可以放 SQL like 语句:

select * from table limit 5

但是,当我尝试使用“gql 查询”来执行删除行之类的操作时:

delete from table where date<"2019-01-01" 

它不起作用 - 我收到错误消息:“GQL 查询错误:在第 1 行第 1 列遇到“删除”。期待:“选择”... "

是否有一种简单的方法可以对所有旧记录运行删除?

【问题讨论】:

  • 最简单的方法是编写一个脚本检查所有条件然后删除。

标签: google-app-engine google-cloud-datastore gql


【解决方案1】:

您不能使用 GQL 来删除。如果你想删除你应该使用客户端libraries之一。

【讨论】:

    【解决方案2】:

    一种简单的方法是编写一个脚本并在需要时运行它。

    代码(Python 2.7):

    from google.appengine.ext import ndb
    from datetime import date
    
    kind = ModelX  #kind whose entries you want to delete
    check = date(2019,1,1)   # Date to be used to check records 
    
    # GQL Query to fetch keys of all older records
    all_keys = kind.gql("WHERE date <= :1",check).fetch(keys_only = True)
    
    # Deleting all entries at once
    ndb.delete_multi(all_keys)
    

    希望这能回答你的问题!!!

    【讨论】:

      【解决方案3】:

      您不能使用 GQL 删除实体。

      您可以使用 UI 删除按钮手动删除实体或使用 datastore.delete() 方法删除实体

      【讨论】:

        猜你喜欢
        • 2012-02-02
        • 2011-02-14
        • 2023-03-08
        • 2011-06-26
        • 2017-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多