【问题标题】:Spring Data DynamoDB - Respository performing scan instead of querySpring Data DynamoDB - 存储库执行扫描而不是查询
【发布时间】:2020-11-11 23:07:13
【问题描述】:

预期行为

Repository 对只使用 hashKey 和 rangeKey 属性的方法执行查询,结果如下:

"{"TableName":"music","KeyConditionExpression":"artist = :_artist and begins_with(title, :_title)","ExpressionAttributeValues":{":_artist":{"S":"Djavan"},":_title":{"S":"Eu te devoro"}}}"

实际行为

Repository 对仅使用 hashKey 和 rangeKey 属性的方法执行 scanFilter,结果如下:

"{"TableName":"music","ScanFilter":{"artist":{"AttributeValueList":[{"S":"Djavan"}],"ComparisonOperator":"EQ"},"title":{"AttributeValueList":[{"S":"Eu te devoro"}],"ComparisonOperator":"BEGINS_WITH"}}}"

重现问题的步骤

使用名为 Music 的实体

@DynamoDBTable(tableName = "music")
data class Music(
        @field:Id
        @DynamoDBIgnore
        val id: MusicId = MusicId(),

        var genre: String? = null
) {
    @DynamoDBHashKey(attributeName = "artist")
    fun getArtist() = id.artist

    fun setArtist(artist: String) {
        id.artist = artist
    }

    @DynamoDBHashKey(attributeName = "title")
    fun getTitle() = id.title

    fun setTitle(title: String) {
        id.title = title
    }

}

@DynamoDBDocument
data class MusicId(
        @field:DynamoDBHashKey(attributeName = "artist")
        var artist: String? = null,

        @field:DynamoDBRangeKey(attributeName = "title")
        var title: String? = null
) : Serializable

还有一个仓库

@EnableScan //I know that if I remove this annotation, enables won't be permitted, but the problem is that the implementation code doesn't recognize my method as a key query and if I remove this annotation, the method falls on invocation
interface MusicRepository : CrudRepository<Music, MusicId> {

    fun findByArtistAndTitleStartingWith(artista: String, sortKey: String): List<Music>
}

当我调用时:

@PostConstruct
    fun init() {
        println(musicRepository.findByArtistAndTitleStartingWith("Djavan", "Eu te devoro").joinToString())
    }

如上所示,日志显示我对 AWS 的调用

规格

Github issue

我有什么问题吗?还是spring数据创建对aws的正确查询的另一种方法?

【问题讨论】:

    标签: java spring-data amazon-dynamodb


    【解决方案1】:

    尝试随机更改实体后。我在存储库方法中得到了预期的结果。

    使用复合键映射实体的正确方法

    @DynamoDBTable(tableName = "music")
    data class Music(
    
            @get:DynamoDBHashKey(attributeName = "artist")
            var artist: String? = null,
    
            @get:DynamoDBRangeKey(attributeName = "title")
            var title: String? = null,
    
            var genre: String? = null
    ) {
    
        @Id
        private var id: MusicId? = null
            get() = MusicId(artist, title)
    }
    
    @DynamoDBDocument
    data class MusicId(
    
            @field:DynamoDBHashKey(attributeName = "artist")
            var artist: String? = null,
    
            @field:DynamoDBRangeKey(attributeName = "title")
            var title: String? = null
    ) : Serializable
    
    

    更改实体并调用后:

    musicRepository.findByArtistAndTitleStartingWith("Djavan", "Eu te devoro")
    

    我得到了正确的 AWS api 调用。

    "{"TableName":"music","ConsistentRead":true,"KeyConditions":{"artist":{"AttributeValueList":[{"S":"Djavan"}],"ComparisonOperator":"EQ"},"title":{"AttributeValueList":[{"S":"Eu te devoro"}],"ComparisonOperator":"BEGINS_WITH"}},"ScanIndexForward":true}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 2015-08-09
      • 2021-04-01
      • 2018-11-03
      • 1970-01-01
      相关资源
      最近更新 更多