简体   繁体   English

无法在 Kotlin SDK v2 中的 DynamoDb 中查询 GSI

[英]Cannot query GSI in DynamoDb in Kotlin SDK v2

I am working with DynamoDb using Kotlin.我正在使用 Kotlin 使用 DynamoDb。

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 .我有一个简单的表,其中包含一个 GSI 名称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:表和索引都有一个复合主键:

  • Table: PartitionKey and SortKey表:PartitionKey 和 SortKey
  • GSI: AuthorId and SortKey GSI:AuthorId 和 SortKey

The problem is that I am trying to query the GSI by the following pseudo-expression:问题是我试图通过以下伪表达式查询 GSI:

AuthorId="author_id" and SortKey BETWEEN "value_1" and "value_2".

Translating it to Koltin.将其翻译成 Koltin。 I am using the DynamoDbEnhancedAsyncClient :我正在使用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).我在谷歌上搜索了一下,发现它与不向索引的主键 (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.我也尝试做一个FilterExpression但没有成功。

Any clue what I am doing wrong?知道我做错了什么吗?

You never pass the value of the partition author_id key to the function:您永远不会将分区author_id键的值传递给 function:

QueryConditional betweenCondition = QueryConditional
                                      .keyEqualTo(Key.builder().partitionValue(authorIdValue)
                                      .sortBetween(value1Key, value2Key)
                                      .build());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM