【问题标题】:Query a Secondary Index on DynamoDB in Golang在 Golang 中查询 DynamoDB 上的二级索引
【发布时间】:2019-06-12 19:30:19
【问题描述】:

我的主键是一个名为“id”的字段

我在“group_number”字段的表中添加了二级索引

我通过二级索引查询如下:

// Query the secondary index
queryInput := &dynamodb.QueryInput{
    TableName: aws.String(c.GetDynamoDBTableName()),
    KeyConditions: map[string]*dynamodb.Condition{
        "group_number": {
            ComparisonOperator: aws.String("EQ"),
            AttributeValueList: []*dynamodb.AttributeValue{
                {
                    S: aws.String(c.GroupNumber),
                },
            },
        },
    },
}

但是;我收到错误“validationexception:查询条件缺少关键架构元素:id”

DynamoDB 是否只允许查询主键? 我的印象是您使用“GetItem”作为主键,因为如果您使用主键,则只能返回一条记录。要通过二级索引进行搜索,请使用“Query”,如果要通过非索引键进行搜索,请使用“Scan”。

请让我知道我在这里做错了什么。

【问题讨论】:

    标签: go amazon-dynamodb dynamodb-queries amazon-dynamodb-index


    【解决方案1】:

    除了TableName,还需要在创建QueryInput查询索引时指定IndexName属性。

    https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/#QueryInput

    // The name of an index to query. This index can be any local secondary index
    // or global secondary index on the table. Note that if you use the IndexName
    // parameter, you must also provide TableName.
    IndexName *string `min:"3" type:"string"`
    

    【讨论】:

    • 我不知道我怎么错过了那个领域。工作完美,谢谢!
    【解决方案2】:
    var queryInput, err2 = svc.Query(&dynamodb.QueryInput{
            TableName: aws.String(tableName),
            IndexName: aws.String(index_name),
            KeyConditions: map[string]*dynamodb.Condition{
                "Phone": {
                    ComparisonOperator: aws.String("EQ"),
                    AttributeValueList: []*dynamodb.AttributeValue{
                        {
                            S: aws.String(phone_no),
                        },
                    },
                },
            },
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多