注: -あなたは完全なコードを提供していないとして、シミュレートすることは困難であると問題を特定する。しかし、私は同様のテーブルとインデックスを作成しました。それは私のためにうまく動作します。詳細については、以下のコードを参照してください。
ここにテーブル作成スクリプトがあり、インデックスを照会します。
必要に応じてテーブル名とインデックス名を変更することができます。あなたが投稿したのと同じ主要な属性の構造に従ってきました。
これはテスト済みで正常です。
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));
}
});
メインtaのパーティションキーなしでGSIを作成してクエリできますble。 GSIに照会しようとしている場所でコードを表示できますか? GSIの主要な属性についても言及してください。 – notionquest
ありがとう!私のコードで質問を更新します – eagleEye