2017-06-05 56 views
1

據:https://cloud.google.com/datastore/docs/concepts/entities#embedded_entity谷歌數據存儲1500字節的屬性限制嵌入實體

在嵌入式實體設置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。儘管我沒有看到任何解決方法或解決方法

有什麼建議是什麼導致這種情況以及如何解決/解決方法?

回答

1

解決方法是爲至少需要支持超過1500字節的那些屬性設置excludeFromIndexes=true。您的嵌入式實體的JSON應如下所示。請注意0​​屬性,我們明確地設置了excludeFromIndexes

{ 
    "properties": { 
    "date": { 
     "timestampValue": "2017-06-05T18:53:23.106Z" 
    }, 
    "text": { 
     "stringValue": "long text that exceeds 1500 bytes", 
     "excludeFromIndexes": true 
    } 
    } 
} 
+0

我的問題是嵌入式屬性的一個大特性,afaik你不能在嵌入式實體的屬性上放置'excludeFromIndexes:true'(或者如果可以的話,你會怎麼做?) –

+0

我的例子的確是來自GCD控制檯的嵌入式實體。我使用了非常簡單的Java API來排除任何屬性(頂級或嵌套)。不太熟悉NodeJS。 –

0

固定的版本我使用此修復程序來設置{excludeFromIndexes:所有屬性遞歸地除外,其中三個除外。 entity.entityToEntityProto方法在屬性發送到Datastore之前被調用,但只有當你的entity.data是一個Object(它也可以傳遞一個Array)時才被調用。

您可以在節點中的任何位置包含此腳本,最好是在數據存儲啓動之前。它將覆蓋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; 
} 

所以只要確保你保存實體{數據:{屬性: '值'}鍵:...}。如果要爲深層屬性編制索引,請用點分隔它們,忽略數組。 我在@ google-cloud/datastore v1.1.0中使用腳本。

相關問題