【问题标题】:Querying DynamoDb with Global Secondary Index Error查询带有全局二级索引错误的 DynamoDb
【发布时间】:2023-01-09 20:56:12
【问题描述】:

我是 DynamoDb 的新手以及查询它的复杂性 - 我理解(希望是正确的)我需要有分区键或全局二级索引 (GSI) 才能查询表中的该值。

我知道我可以通过设置解析器使用 Appsync 在 GSI 上进行查询 - 这很有效。但是,我有一个使用 Java AWS CDK 的设置(我在 Kotlin 中编写),我在其中使用 Appsync 并将我的查询路由到 lambda 解析器(这样一旦它起作用,我以后可以做更复杂的事情)。

问题的症结在于,当我设置 Lambda 来解决我的查询时,我最终收到此错误消息:com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: Query condition missed key schema element: testName 从 Lambda 返回。

我认为这些应该是关键的 sn-ps..

我的 DynamoDbBean..

@DynamoDbBean
data class Test(
    @get:DynamoDbPartitionKey var id: String = "",
    @get:DynamoDbSecondaryPartitionKey(indexNames = ["testNameIndex"])
    var testName: String = "",
)

使用我创建 GSI 的 CDK

        testTable.addGlobalSecondaryIndex(
        GlobalSecondaryIndexProps.builder()
            .indexName("testNameIndex")
            .partitionKey(
                Attribute.builder()
                    .name("testName")
                    .type(AttributeType.STRING)
                    .build()
            )
            .projectionType(ProjectionType.ALL)
            .build())

然后,在我的 Lambda 中,我尝试查询我的 DynamoDb 表,使用此处的固定值 testName = A

我在 Test 表中的样本数据就像这样......

{
"id" : "SomeUUID",
"testName" : "A"
}
    private var client: AmazonDynamoDB = AmazonDynamoDBClientBuilder.standard().build()
    private var dynamoDB: DynamoDB = DynamoDB(client)

Lambda 解析器片段...

        val table: Table = dynamoDB.getTable(TABLE_NAME)
        val index: Index = table.getIndex("testNameIndex")
...
        QuerySpec().withKeyConditionExpression("testNameIndex = :testName")
            .withValueMap(ValueMap().withString(":testName", "A"))
        val iterator: Iterator<Item> = index.query(querySpec).iterator()

        while (iterator.hasNext()) {
            logger.info(iterator.next().toJSONPretty())
        }

这就是导致此错误消息的原因:com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: Query condition missed key schema element: testName

我在这里走错线了吗?我知道“增强型”Dynamo sdk 和 dynamodbv2 sdk 之间混合了一些库 - 所以如果有更好的方法来执行此查询,我很想知道!

谢谢!

【问题讨论】:

    标签: kotlin aws-lambda amazon-dynamodb aws-cdk aws-appsync


    【解决方案1】:

    你的QuerySpecvalueMap初始化错误。你需要像这样初始化它。

    请在ValueMap中查看testName

    QuerySpec().withKeyConditionExpression("testNameIndex = :testName")
                .withValueMap(ValueMap().withString("testName", "A"))
    

    【讨论】:

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