【问题标题】:PYTHON-The value of property "{field_name}" is longer than 1048487 bytes for TextProperty in datastorePYTHON - 数据存储区中 TextProperty 的属性“{field_name}”的值超过 1048487 个字节
【发布时间】:2017-11-27 09:17:59
【问题描述】:

根据字段类型TextProperty 的云数据存储文档,不应该对大小有任何限制,但在我的情况下,字符串大小等于

属性“{my_field_name}”的值超过 1048487 字节

下面是我的实体类

class History(ndb.Model):
    user_mail = ndb.StringProperty()
    json_string = ndb.TextProperty(indexed=False)
    updated_date = ndb.DateTimeProperty()

这是我试图存储实体的函数

def store_user_mail(user_email, json_object):

    # string size for which this is creating problem is around 1.56Mb
    print(sys.getsizeof(json.dumps(json_object)))

    user_mail_obj = History()
    user_mail_obj.user_mail = user_email
    user_mail_obj.json_string = json.dumps(json_object)
    user_mail_obj.updated_date = datetime.datetime.utcnow()

    user_mail_obj.put()

我还尝试在存储之前压缩 json 字符串,但这会给我们带来问题,因为我们希望将其存储为文本属性而不是二进制数据。

还有一点是因为我的 json_string 字段是 TextProperty 类型,无论字符串大小如何,它都应该毫无问题地存储。已经花了很多时间找出解决方案。我在这里做错了什么,或者 NDBdatastore 是否存在此错误,如果是这种情况,有什么好的解决方案?

【问题讨论】:

  • my_field_name的值是多少?
  • 另外,为什么不使用ndb.JsonPropertycloud.google.com/appengine/docs/standard/python/ndb/…
  • my_field_name 的@hjpotter92 值为json_string
  • @hjpotter92 尝试过,但在数据存储中,它将 json 存储为二进制而不是文本字符串,如上所述,我们希望将其存储为字符串
  • @DanCornilescu 我的错!其实云数据存储就是我想说的。下面还有文档链接TextProperty Documentation Link For Datastore。我想也许我明白了,现在我们可以使用TextProperty 获得无限长度的字符串,但仍然存在 1500 字节的大小限制,而这实际上在 doc 中缺失。

标签: python google-app-engine google-cloud-datastore app-engine-ndb


【解决方案1】:

这不是 TextProperty 的问题,这是因为 Datastore 实体的最大大小可以是 1,048,572。 https://cloud.google.com/datastore/docs/concepts/limits

我正在为您查看 BlobProperty.... 看起来可能仍然是 1MB 的限制(我无法确认),但您可以使用 compress 参数来获得很多超过 1MB 的数据。

【讨论】:

  • 如何使用压缩参数?
【解决方案2】:

实体的大小限制为 1MB。如果您尝试存储在实体/属性中的内容可能比这更大,那么您需要使用 CloudStorage。我建议将该文本作为文件存储在 Cloud Storage 中,并在对象的属性中保留一个链接。它可能看起来像这样:

class History(ndb.Model):
    user_mail = ndb.StringProperty()
    url = ndb.StringProperty() #https://storage.googleapis.com/{app_id}/{file_name}
    updated_date = ndb.DateTimeProperty()

然后,当您需要该文本时,您只需读取存储在该 url 中的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    相关资源
    最近更新 更多