繁体   English   中英

在 Spring 上,如何使用 JPA 和复合键(分区键和排序键)查询 DynamoDB 表?

[英]On Spring, how to query DynamoDB table using JPA and a composite key (Partition Key and Sort Key)?

我有一个使用 JPA 和 Spring Data DynamoDB 设置的 Spring 项目。 它工作正常。 我可以通过分区键和排序键(称为DynamoDBHashKeyDynamoDBRangeKey )读取它从 DynamoDB 表中获取项目。

我的问题是我的存储库的设置方式是使用queryscan操作读取表,而不是使用get-item操作,这应该更有效。

这是我的实体:

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.Id;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@DynamoDBTable(tableName = "my-entity-table")
public class MyEntity {

    @Id
    @DynamoDBHashKey 
    @DynamoDBAttribute(attributeName = "partition_key")
    private String partitionKey;

    @Id
    @DynamoDBRangeKey
    @DynamoDBAttribute(attributeName = "sort_key")
    private String sortKey;

    ...
}

这是我的存储库:

import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@EnableScan
@Repository
public interface MyEntityRepository extends CrudRepository<MyEntity, String> {
    List<MyEntity> findByPartitionKeyAndSortKey(String partitionKey, String sortKey);
}

当我的表同时具有分区键和排序键时,如何配置我的实体和存储库以使用get-item操作从表中读取项目?

做了一些研究后,我偶然发现了这两篇文章:

  1. 复合主键 Kotlin 示例
  2. 带有哈希和范围键 DynamoDB 表的 Spring Data JPA

第一个解释了如何在 Kotlin 中做我想做的事。 不错,但这不是我正在寻找的。

第二个完美命中目标,基本上它说的是我需要为我的实体对象创建一个主键对象,如下所示:

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIgnore;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.Id;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@DynamoDBTable(tableName = "my-entity-table")
public class MyEntity {

    @Id
    @DynamoDBIgnore
    private PrimaryKey primaryKey;

    ...

    @DynamoDBHashKey
    @DynamoDBAttribute(attributeName = "partition_key")
    public String getPartitionKey() {
        return primaryKey != null ? primaryKey.getPartitionKey() : null;
    }

    public void setPartitionKey(final String partitionKey) {
        if (primaryKey == null) {
            primaryKey = new PrimaryKey();
        }
        primaryKey.setPartitionKey(partitionKey);
    }

    @DynamoDBRangeKey
    @DynamoDBAttribute(attributeName = "sort_key")
    public String getSortKey() {
        return primaryKey != null ? primaryKey.getSortKey() : null;
    }

    public void setSortKey(final String sortKey) {
        if (primaryKey == null) {
            primaryKey = new PrimaryKey();
        }
        primaryKey.setSortKey(sortKey);
    }

    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    @DynamoDBDocument
    public static class PrimaryKey {
        @DynamoDBHashKey 
        @DynamoDBAttribute(attributeName = "partition_key")
        private String partitionKey;

        @DynamoDBRangeKey
        @DynamoDBAttribute(attributeName = "sort_key")
        private String sortKey;
    }
}

然后,我不需要在我的存储库类上创建任何自定义查询方法:

@EnableScan
@Repository
public interface MyEntityRepository extends 
        CrudRepository<MyEntity, MyEntity.PrimaryKey> {

}

之后,只需使用 JPA 的CrudRepository方法来获取项目,就像这样:

final MyEntity.PrimaryKey myEntityPK 
    = new MyEntity.PrimaryKey("partitionKey", "sortKey");

final MyEntity myEntity = myEntityRepository.findById(myEntityPK)
    .orElseThrow(() -> ... );

为了验证它实际上是在使用get-item操作而不是scanquery操作,可以在以下类上放置几个断点(从spring-data-dynamodb-5.1.0 ):

  • org.socialsignin.spring.data.dynamodb.core.DynamoDBTemplate
  • org.socialsignin.spring.data.dynamodb.repository.support.SimpleDynamoDBCrudRepository

暂无
暂无

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

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