嵌入式实体的Google Datastore 1500字节属性限制

时间:2017-06-05 16:06:42

标签: google-cloud-datastore

根据:https://cloud.google.com/datastore/docs/concepts/entities#embedded_entity

在嵌入式实体上设置excludeFromIndexes: true应该删除它及其属性的索引,因此应该允许该嵌入式实体的属性大于1500字节。

我正在尝试编写一个嵌入式实体,其中包含一些超过1500字节的属性,并且我收到错误:

“Error: The value of property “additionalAttributes” is longer than 1500 bytes. at /node_modules/grpc/src/node/src/client.js:434:17"

即使我在嵌入式实体上设置excludeFromIndexes: true(我可以在云控制台中看到嵌入式实体正确添加而没有编制索引)。

我看到有一个已知问题:https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1916。虽然我没有看到任何修复或解决方法

有关导致此问题的原因以及如何解决/解决方法的任何建议?

4 个答案:

答案 0 :(得分:2)

我的问题/问题真的是这个: https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1916

并且答案/解决方案是通过此PR https://github.com/GoogleCloudPlatform/google-cloud-node/pull/2497

的固定版本

答案 1 :(得分:1)

解决方法是至少为那些需要支持超过1500字节的属性设置excludeFromIndexes=true。嵌入式实体的JSON应如下所示。请注意我们明确设置text的{​​{1}}属性。

excludeFromIndexes

答案 2 :(得分:0)

我使用此修复程序以递归方式在所有属性上设置{excludeFromIndexes:false},除了其中三个属性。在将属性发送到数据存储区之前调用方法entity.entityToEntityProto,但前提是您的entity.data是一个Object(它也可以传递一个数组)。

您可以在节点中的任何位置包含此脚本,最好在启动数据存储之前。它将覆盖entity.entityToEntityProto方法。

const INCLUDE_ATTRIBUTES = ['_index','_audit','_unique'];

function entitySetIndexes(properties, path){
    for(let key in properties){
        let keypath = (path ? path+'.' : '') + key
            , prop = properties[key];

        if(prop.entityValue)
            entitySetIndexes(prop.entityValue.properties, keypath);
        else if(prop.arrayValue)
            prop.arrayValue.values.forEach(item=>{
                if(item.entityValue)
                    entitySetIndexes(item.entityValue.properties, keypath)
            });

        // excludeFromIndex cannot be set on arrays, they're always indexed
        if(!prop.arrayValue)
            prop.excludeFromIndexes = INCLUDE_ATTRIBUTES.indexOf(keypath) === -1;
    }
}

const entity = require('@google-cloud/datastore/src/entity.js')
    , entityToEntityProto = entity.entityToEntityProto;
entity.entityToEntityProto = function(entityObject){
    entityObject = entityToEntityProto(entityObject);
    entitySetIndexes(entityObject.properties);
    return entityObject;
}

因此,请确保使用{data:{attribute:' value'},key:...}保存实体。如果要索引深层属性,请指定它们用点分隔,忽略数组。 我在@ google-cloud / datastore v1.1.0中使用了该脚本。

答案 3 :(得分:0)

最简单的方法是添加@Unindexed批注,该批注将删除该属性的索引,并允许您插入超过1500个字节的字符串属性。

相关问题