【问题标题】:list_value cannot contain a Value containing another list_valuelist_value 不能包含包含另一个 list_value 的值
【发布时间】:2021-09-21 20:56:22
【问题描述】:

我有一个想要存储到云数据存储的二维数值矩阵,

from google.cloud import datastore
client = datastore.Client(project='my-random-project')

complete_key = client.key("Task", "2185e0c5")
task = datastore.Entity(key=complete_key, exclude_from_indexes=['data'])

task.update({'data': {'input': [[1,2,3],[4,5,6],[7,8,9]], 'output': [[2,3,4],[5,6,7],[8,9,10]]}})

client.put(task)

所以当我运行上面的代码时,我得到以下错误

InvalidArgument: 400 list_value cannot contain a Value containing another list_value.

我怎样才能把这种数据放到cloud-datastore

【问题讨论】:

    标签: python google-cloud-platform google-cloud-datastore


    【解决方案1】:

    快速而肮脏的解决方案:转储到 json 并存储为字符串:

    import json
    data_raw = {
        'input': [[1,2,3],[4,5,6],[7,8,9]],
        'output': [[2,3,4],[5,6,7],[8,9,10]]
    }
    task.update({'data': json.dumps(data_raw)})
    

    您需要将数据配置为接受stringValue(不允许在数据库端过滤/查询中使用值)。

    更好的解决方案是将每个值作为索引存储在对象中:

    data_raw = {
        'input': {
            1: [1,2,3],
            2: [4,5,6],
            3: [7,8,9]
        },
        'output': {
            1: [2,3,4],
            2: [5,6,7],
            3: [8,9,10]
       }
    }
    

    我不是 100% 确定如何在此处映射实体类型,但您确实得到了一个兼容但高度嵌套的对象。 如果此处排序很重要,但您无法提前预期排序(例如“插入时设置索引”),请考虑使用一些哈希算法来存储有序索引。这可能是一个专门的用例,需要大量时间来开发。

    【讨论】:

    • 这能解决您的问题吗? @jayendra-parmar
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多