我有一个使用 JPA 和 Spring Data DynamoDB 设置的 Spring 项目。工作正常。我可以通过分区键和排序键(称为DynamoDBHashKey
and DynamoDBRangeKey
).
我的问题是我的存储库的设置方式是使用读取表query
and scan
操作,而不是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
当我的表同时具有分区键和排序键时操作?
做了一些研究后,我偶然发现了这两篇文章:
- 复合主键 Kotlin 示例 https://github.com/derjust/spring-data-dynamodb/wiki/Composite-Primary-Keys-Kotlin-Example
- 具有哈希和范围键 DynamoDB 表的 Spring Data JPA http://johnhunsley.blogspot.com/2016/11/spring-data-jpa-with-hash-range-key.html
第一个解释了如何在 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
操作而不是scan
and query
操作时,可以在以下类上放置几个断点(截至spring-data-dynamodb-5.1.0
):
org.socialsignin.spring.data.dynamodb.core.DynamoDBTemplate
org.socialsignin.spring.data.dynamodb.repository.support.SimpleDynamoDBCrudRepository
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)