使用DynamoDB从辅助索引获取GetItem

ped*_*ete 13 amazon-dynamodb

我刚开始使用DynamoDB并设置了"帐户"表.

我已经设置了一个二级索引,因此我可以查询api用户和用户密钥.这些值都不是主键,因为它们既易变又可以更改.

表是用

TableName: "Accounts",
        KeySchema:  [
            { AttributeName: "id", KeyType: "HASH" },
            { AttributeName: "email", KeyType: "RANGE" }
        ],
        AttributeDefinitions: [
            { AttributeName: "id", AttributeType: "S" },
            { AttributeName: "email", AttributeType: "S" }
        ]
Run Code Online (Sandbox Code Playgroud)

索引是

 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"
                        },
Run Code Online (Sandbox Code Playgroud)

我现在正试图通过查询ApiAccounts索引获得一个使用帐户.

我尝试着

 dynamoClient.get({
            TableName: 'Accounts',
            IndexName: 'ApiAccounts',
            Key: {
                name: nameKeyArray[0],
                apiKey: nameKeyArray[1]
            }, callback)
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误One of the required keys was not given a value,这让我相信我不能对索引做"获取"?或者我没有正确引用索引.有人可以为我澄清一下吗?

名称和API密钥是唯一的,所以我想我想尽可能避免查询或扫描

Oh *_*oon 25

我想从官方文档中不是那么清楚,但你可以执行ScanQueryGSI索引上,但不是GetItem.

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

// 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
Run Code Online (Sandbox Code Playgroud)

但是对于Index'es,Hash并且Range密钥不是唯一的,它们可能具有重复的记录/项目.

// 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
Run Code Online (Sandbox Code Playgroud)

// 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
Run Code Online (Sandbox Code Playgroud)

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作为参数.

  • @pedalpete是的,但请注意,在查询索引时可能会返回重复的记录,因为索引的哈希和范围键不是唯一的,与表不同. (2认同)