Home > Software engineering >  Querying DynamoDb with Global Secondary Index Error
Querying DynamoDb with Global Secondary Index Error

Time:01-10

I'm new to DynamoDb and the intricacies of querying it - I understand (hopefully correctly) that I need to either have a partition Key or Global Secondary Index (GSI) in order to query against that value in the table.

I know I can use Appsync to query on a GSI by setting up a resolver - and this works. However, I have a setup using the Java AWS CDK (I'm writing in Kotlin) where I'm using Appsync and routing my queries into lambda Resolvers (so that once this works, I can do more complicated things later).

The Crux of the issue is that when I setup a Lambda to resolve my query, I end up with this Error msg: com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: Query condition missed key schema element: testName returned from the Lambda.

I think these should be the key snippets..

My DynamoDbBean..

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

Using the CDK I Created GSI on

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

enter image description here

Then, within my Lambda I am trying to query my DynamoDb table, using a fixed value here testName = A.

My Sample data in the Test table would be like so..

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

Lambda Resolver Snippets...

        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())
        }

This is what results in this Error msg: com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: Query condition missed key schema element: testName

Am I on the wrong lines here? I know there is some mixing of libs between the 'enhanced' Dynamo sdk and the dynamodbv2 sdk - so if there is a better way of doing this query, I'd love to know!

Thanks!

CodePudding user response:

Your QuerySpec's withKeyConditionExpression is initialized wrong. You need to init it like.

not testNameIndex it should be testName

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