I am working with DynamoDb using Kotlin.
I am currently using:
implementation platform("software.amazon.awssdk:bom:2.17.91")
implementation 'software.amazon.awssdk:dynamodb'
implementation 'software.amazon.awssdk:dynamodb-enhanced'
implementation 'software.amazon.awssdk:sts'
I have a simple table with one GSI name by-users-gsi
.
aws dynamodb describe-table --table-name forum --region us-east-1 --endpoint-url http://localhost:800
{
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "AuthorId",
"AttributeType": "S"
},
{
"AttributeName": "PartitionKey",
"AttributeType": "S"
},
{
"AttributeName": "SortKey",
"AttributeType": "S"
}
],
"TableName": "table-name",
"KeySchema": [
{
"AttributeName": "PartitionKey",
"KeyType": "HASH"
},
{
"AttributeName": "SortKey",
"KeyType": "RANGE"
}
],
"TableStatus": "ACTIVE",
......
"GlobalSecondaryIndexes": [
{
"IndexName": "by-users-gsi",
"KeySchema": [
{
"AttributeName": "AuthorId",
"KeyType": "HASH"
},
{
"AttributeName": "SortKey",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"IndexStatus": "ACTIVE",
......
}
]
}
}
Both the table and the index have a composite primary key:
The problem is that I am trying to query the GSI by the following pseudo-expression:
AuthorId="author_id" and SortKey BETWEEN "value_1" and "value_2".
Translating it to Koltin. I am using the DynamoDbEnhancedAsyncClient
:
val table = client.table('table-name', TableSchema.fromBean(AnyItem::class.java))
val authorIdValue = "author_id"
val value1 = "value_1"
val value2 = "value_2"
val value1Key = Key.builder()
.partitionValue(authorIdValue)
.sortValue(value1)
.build()
val value2Key = Key.builder()
.partitionValue(authorIdValue)
.sortValue(value2)
.build()
val betweenCondition = QueryConditional.sortBetween(value1Key, value2Key)
val query = QueryEnhancedRequest
.builder()
.queryConditional(betweenCondition)
.build()
val results = table
.index("index-name")
.query(betweenCondition)
.awaitFirst()
// Whatever ......
But it is throwing me the following error:
software.amazon.awssdk.services.dynamodb.model.DynamoDbException: Query condition missed key schema element
I googled it and is related to not being adding conditions to the Index's PrimaryKey (PK + SK). However, both conditions are defining:
.partitionValue(authorIdValue)
I understand it should infer the Index's Partition Value, not the table's. I also tried to do a FilterExpression
but didn't work too.
Any clue what I am doing wrong?
You never pass the value of the partition author_id
key to the function:
QueryConditional betweenCondition = QueryConditional
.keyEqualTo(Key.builder().partitionValue(authorIdValue)
.sortBetween(value1Key, value2Key)
.build());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.