douhai9043 2019-06-12 19:30
浏览 237
已采纳

在Golang中查询DynamoDB上的二级索引

My primary key is a field called "id"

I've added a secondary index to my table on the field "group_number"

enter image description here

I query via the secondary index like so:

// 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),
                },
            },
        },
    },
}

However; I get the error "validationexception: query condition missed key schema element: id"

Does DynamoDB only allow the querying of primary keys? I was under the impression you use "GetItem" for primary key being that only one record can come back if you use a primary key. To search via secondary indexes you use "Query", and to search by non-index keys, you use "Scan".

Please let me know what I'm doing incorrectly here.

  • 写回答

1条回答 默认 最新

  • duanjuelu8874 2019-06-13 02:21
    关注

    In addition to the TableName, You need to also specify the IndexName property when creating the QueryInput to query the index.

    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"`
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛