Complete the code to define a Global Secondary Index (GSI) in a DynamoDB table.
GlobalSecondaryIndexes: [{
IndexName: "UserEmailIndex",
KeySchema: [{ AttributeName: "email", KeyType: [1] }],
Projection: { ProjectionType: "ALL" },
ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
}]The partition key for a GSI must be of type HASH.
Complete the code to add a Local Secondary Index (LSI) with a sort key.
LocalSecondaryIndexes: [{
IndexName: "StatusIndex",
KeySchema: [
{ AttributeName: "userId", KeyType: "HASH" },
{ AttributeName: [1], KeyType: "RANGE" }
],
Projection: { ProjectionType: "KEYS_ONLY" }
}]The sort key for the LSI is often an attribute like status to filter or sort items per partition.
Fix the error in the GSI definition by completing the missing attribute.
GlobalSecondaryIndexes: [{
IndexName: "OrderDateIndex",
KeySchema: [
{ AttributeName: "orderId", KeyType: "HASH" },
{ AttributeName: [1], KeyType: "RANGE" }
],
Projection: { ProjectionType: "INCLUDE", NonKeyAttributes: ["total"] },
ProvisionedThroughput: { ReadCapacityUnits: 10, WriteCapacityUnits: 5 }
}]The sort key for this GSI should be orderDate to allow queries sorted by date.
Fill both blanks to define a DynamoDB table with a primary key and a GSI.
TableName: "Products", AttributeDefinitions: [ { AttributeName: "productId", AttributeType: "S" }, { AttributeName: [1], AttributeType: "S" } ], KeySchema: [ { AttributeName: "productId", KeyType: "HASH" } ], GlobalSecondaryIndexes: [{ IndexName: "CategoryIndex", KeySchema: [ { AttributeName: [2], KeyType: "HASH" } ], Projection: { ProjectionType: "ALL" }, ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 } }]
The attribute 'category' is defined and used as the partition key for the GSI named 'CategoryIndex'.
Fill all three blanks to create a DynamoDB table with a composite primary key and a Local Secondary Index (LSI).
TableName: "Orders", AttributeDefinitions: [ { AttributeName: [1], AttributeType: "S" }, { AttributeName: [2], AttributeType: "S" }, { AttributeName: [3], AttributeType: "S" } ], KeySchema: [ { AttributeName: [1], KeyType: "HASH" }, { AttributeName: [2], KeyType: "RANGE" } ], LocalSecondaryIndexes: [{ IndexName: "StatusIndex", KeySchema: [ { AttributeName: [1], KeyType: "HASH" }, { AttributeName: [3], KeyType: "RANGE" } ], Projection: { ProjectionType: "ALL" } }]
The table uses 'orderId' as the partition key and 'createdAt' as the sort key. The LSI uses the same partition key 'orderId' and a different sort key 'status'.