2017-04-11 94 views
3

我需要通過與其主鍵不同的鍵來查詢DynamoDB表。我試圖爲它創建一個全球二級索引。但是我得到這個錯誤:「查詢鍵條件不支持dynamodb」。通過查看一些示例,它看起來像我不能通過二級索引進行查詢,除非我還包含主索引/鍵,這是正確的嗎?假設我需要查詢在某個城市工作的所有員工,我可以在沒有員工ID的情況下執行此操作嗎?查詢沒有主鍵的DynamoDB

更新信息 也許我的索引沒有創建,因爲它應該?

表信息:

  • ID - >主分區鍵
  • 主要排序鍵 - >名

GSI:

  • 分區鍵/主鍵 - - >城市
  • 預計 - >全部

當我從節點I作爲參數發送的城市,指數名稱查詢:

const filter = { city: city}; 
    return this.getRecordsFromDb(filter, { IndexName: "myIndexName" }) 
     .then(records => __.head(records)); 
+0

您可以創建並查詢GSI,而不需要主ta的分區鍵BLE。你可以在你想要查詢GSI的地方顯示你的代碼嗎?請提及GSI的關鍵屬性。 – notionquest

+0

謝謝!我會用我的代碼更新這個問題 – eagleEye

回答

7

注: -由於您沒有提供完整的代碼,它是很難模擬和找出問題。但是,我創建了類似的表和索引。這對我來說可以。你可以參考下面的代碼獲取更多細節。

這是表創建腳本和查詢索引。

如果需要,您可以更改表名稱和索引名稱。我遵循了您在帖子中提到的相同關鍵屬性結構。

這已經過測試,工作正常。

1)與索引 'city_index' 創建表 '城市': -

var params = { 
     TableName: 'city', 
     KeySchema: [ // The type of of schema. Must start with a HASH type, with an optional second RANGE. 
      { // Required HASH type attribute 
       AttributeName: 'id', 
       KeyType: 'HASH', 
      }, 
      { // Required HASH type attribute 
       AttributeName: 'name', 
       KeyType: 'RANGE', 
      }    

     ], 
     AttributeDefinitions: [ // The names and types of all primary and index key attributes only 
      { 
       AttributeName: 'id', 
       AttributeType: 'S', // (S | N | B) for string, number, binary 
      }, 
      { 
       AttributeName: 'name', 
       AttributeType: 'S', // (S | N | B) for string, number, binary 
      }, 
      { 
       AttributeName: 'city', 
       AttributeType: 'S', // (S | N | B) for string, number, binary 
      }, 

     ], 
     ProvisionedThroughput: { // required provisioned throughput for the table 
      ReadCapacityUnits: 400, 
      WriteCapacityUnits: 400, 
     }, 
     GlobalSecondaryIndexes: [ // optional (list of GlobalSecondaryIndex) 
      { 
       IndexName: 'city_index', 
       KeySchema: [ 
        { // Required HASH type attribute 
         AttributeName: 'city', 
         KeyType: 'HASH', 
        } 
       ], 
       Projection: { // attributes to project into the index 
        ProjectionType: 'ALL' // (ALL | KEYS_ONLY | INCLUDE) 
       }, 
       ProvisionedThroughput: { // throughput to provision to the index 
        ReadCapacityUnits: 400, 
        WriteCapacityUnits: 400, 
       }, 
      }, 
      // ... more global secondary indexes ... 
     ], 

    }; 
    dynamodb.createTable(params, function(err, data) { 
     if (err){ console.log("error :" +JSON.stringify(err));} // an error occurred 
     else console.log("success :" +JSON.stringify(data)); // successful response 

    }); 

2)將一些數據,以城市表

3)的查詢使用索引: -

var docClient = new AWS.DynamoDB.DocumentClient(); 
var table = "city"; 
var params = { 
    TableName : table, 
    IndexName : 'city_index', 
    KeyConditionExpression : 'city = :cityVal', 
    ExpressionAttributeValues : { 
     ':cityVal' : 'london'   
    } 
}; 

docClient.query(params, function(err, data) { 
    if (err) { 
     console.error("Unable to read item. Error JSON:", JSON.stringify(err, 
       null, 2)); 
    } else { 
     console.log("GetItem succeeded:", JSON.stringify(data, null, 2)); 
    } 
}); 
+1

'IndexName:'是使用排序鍵的主索引生命保護程序。 –