我刚刚开始使用DynamoDB,并设置了一个“帐户”表。

我已经设置了二级索引,以便可以查询api用户和用户密钥。
这些值都不是主键,因为它们都是易变的并且可以更改。

该表是用

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密钥是唯一的,所以我想避免查询或扫描(如果可能)

最佳答案

我想从官方文档来看还不太清楚。您可以对Scan索引执行QueryGSI操作,但不能对GetItem操作执行。

对于Table中的每个记录/项目,它们必须具有唯一的HASHRANGE键。


// assume dummy api putItem(id, email, name, apiKey)
account.putItem("1", "[email protected]", "john", "key1") // OK
account.putItem("1", "[email protected]", "john", "key1") // NOT OK, id and email are table HASH and RANGE keys, must be unique

但是对于Index来说,HashRange键不是唯一的,它们可能包含重复的记录/项目。


// assume dummy api putItem(id, email, name, apiKey)
account.putItem("1", "[email protected]", "john", "key1") // OK
account.putItem("1", "[email protected]", "john", "key1") // OK


// assume dummy api putItem(id, email, name, apiKey)
account.putItem("1", "[email protected]", "john", "key1") // OK
account.putItem("2", "[email protected]", "john", "key1") // OK

Java

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/document/Index.html
Index实现QueryApiScanApi,但不实现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从二级索引中获取GetItem,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43732835/

10-16 15:47