amazon-dynamodb - 使用 DynamoDB 从二级索引中获取项目

标签 amazon-dynamodb

我刚刚开始使用 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 是唯一的,所以我想我想尽可能避免查询或扫描

最佳答案

我想从官方文档中看不太清楚。您可以执行ScanQueryGSI 进行操作索引,但不是 GetItem手术。

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

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, HashRange键不是唯一的,它们可能包含重复的记录/项目。

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实现 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 从二级索引中获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43732835/

相关文章:

java - DynamoDB Mapper 映射集合数据类型

php - 动态数据库;获取表中的所有数据 - PHP

amazon-web-services - S3 存储桶的速率限制

amazon-web-services - 如何解决 AWS CloudFormation 不一致的属性 AttributeDefinitions?

java - 在 Java 中查询具有动态属性的 Dynamo 表

node.js - 用户无权执行 : dynamodb:PutItem on resource

php - 使用 AWS DynamoDB 和 PHP 进行分页

amazon-web-services - Dynamodb 一次批量扫描与多次单次获取

node.js - 为什么Amazon DynamoDB的Delete方法不返回删除的结果?

ios - 使用 swift 提取 AWS DynamoDB 中的数据