【问题标题】:GetItem from Secondary Index with DynamoDB使用 DynamoDB 从二级索引中获取项目
【发布时间】:2017-09-29 16:17:46
【问题描述】:

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

【问题讨论】:

    标签: amazon-dynamodb


    【解决方案1】:

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

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

    即

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

    即

    // 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
    

    即

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多