注: -由於您沒有提供完整的代碼,它是很難模擬和找出問題。但是,我創建了類似的表和索引。這對我來說可以。你可以參考下面的代碼獲取更多細節。
這是表創建腳本和查詢索引。
如果需要,您可以更改表名稱和索引名稱。我遵循了您在帖子中提到的相同關鍵屬性結構。
這已經過測試,工作正常。
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));
}
});
您可以創建並查詢GSI,而不需要主ta的分區鍵BLE。你可以在你想要查詢GSI的地方顯示你的代碼嗎?請提及GSI的關鍵屬性。 – notionquest
謝謝!我會用我的代碼更新這個問題 – eagleEye