我刚刚开始使用 DynamoDB 并设置了一个“帐户”表。
我已经设置了一个二级索引,所以我可以查询一个 api 用户和用户 key 。
这些值都不是主键,因为它们都是易变的并且可以更改。
该表是用
TableName: "Accounts",
KeySchema: [
{ AttributeName: "id", KeyType: "HASH" },
{ AttributeName: "email", KeyType: "RANGE" }
],
AttributeDefinitions: [
{ AttributeName: "id", AttributeType: "S" },
{ AttributeName: "email", AttributeType: "S" }
]
并且索引是
TableName: 'Accounts',
AttributeDefinitions: [
{AttributeName: 'name', AttributeType: 'S'},
{AttributeName: 'apiKey', AttributeType: 'S'}
],
GlobalSecondaryIndexUpdates: [
{
Create: {
IndexName: "ApiAccounts",
ProvisionedThroughput: {
ReadCapacityUnits: 1, WriteCapacityUnits: 1
},
KeySchema: [
{AttributeName: 'name', KeyType: "HASH"},
{AttributeName: 'apiKey', KeyType: "STRING"}
],
Projection: {
ProjectionType: "KEYS_ONLY"
},
我现在正在尝试通过查询
ApiAccounts
来获得使用帐户。指数。我正在努力
dynamoClient.get({
TableName: 'Accounts',
IndexName: 'ApiAccounts',
Key: {
name: nameKeyArray[0],
apiKey: nameKeyArray[1]
}, callback)
但我收到一个错误
One of the required keys was not given a value
,这使我相信我无法在索引上“获取”?或者我没有正确引用索引。有人可以为我澄清吗?名称和 API key 是唯一的,所以我想我想尽可能避免查询或扫描
最佳答案
我想从官方文档中看不太清楚。您可以执行Scan
或 Query
对 GSI
进行操作索引,但不是 GetItem
手术。
对于 Table
中的每条记录/项目,它们必须具有唯一的 HASH
和 RANGE
键。
IE。
// assume dummy api putItem(id, email, name, apiKey)
account.putItem("1", "abc@email.com", "john", "key1") // OK
account.putItem("1", "abc@email.com", "john", "key1") // NOT OK, id and email are table HASH and RANGE keys, must be unique
但是对于
Index
'es, Hash
和 Range
键不是唯一的,它们可能包含重复的记录/项目。IE。
// assume dummy api putItem(id, email, name, apiKey)
account.putItem("1", "abc@email.com", "john", "key1") // OK
account.putItem("1", "bcd@email.com", "john", "key1") // OK
IE。
// assume dummy api putItem(id, email, name, apiKey)
account.putItem("1", "abc@email.com", "john", "key1") // OK
account.putItem("2", "abc@email.com", "john", "key1") // OK
java
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/document/Index.html
Index
实现 QueryApi
和 ScanApi
但不是 GetItemApi
.JavaScript
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#getItem-property
GetItem
不是 接受IndexName
作为参数。http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#query-property
Query
接受 IndexName
作为参数。http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#scan-property
Scan
接受 IndexName
作为参数。
关于amazon-dynamodb - 使用 DynamoDB 从二级索引中获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43732835/