【问题标题】:Is it possible to insert a Set of Objects to DynamoDB in one go - Java是否可以一次性将一组对象插入 DynamoDB - Java
【发布时间】:2018-09-11 14:46:12
【问题描述】:

我可以使用以下代码在 DynamoDB 中插入一个对象(使用 @DynamoDBTable 注释)

@Autowired
private SdnInformationRepository sdnInformationRepository;

SdnInformation inf = new SdnInformation();
inf.setFirstName("firstname");
inf.setLastName("lastname");
sdnInformationRepository.save(inf);

这是我的仓库

public interface SdnInformationRepository extends 
CrudRepository<SdnInformation, String> { 
}

还有我的模特

@DynamoDBTable(tableName = "SdnList")
public class SdnInformation {

@DynamoDBHashKey(attributeName = "Id")
@DynamoDBAutoGeneratedKey
private String id;

private String firstName;

private String lastName;

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

}

这里一切正常。我想知道是否可以一次插入一个列表/一组 SdnInformation 对象?如果我单独插入大量此类对象,则需要花费太多时间。 所以我想要类似的东西

Set<SdnInformation> listToInsert = new HashSet<SdnInformation>();
... some code to fill my set with thousands of objects ...
sdnInformationRepository.save(listToInsert);

【问题讨论】:

  • CrudRepository 接口有一个 saveAll 方法...
  • 哇!我错过了。 saveAll 方法完成了这项工作!

标签: java database insert save amazon-dynamodb


【解决方案1】:

DynamoDB 不支持同时插入多个项目。批量处理多个请求(最多 25 个)是可能的,但 Dynamo 仍将它们视为单独的请求(即,有的可能成功,有的可能失败)。

当然,在高层次上,映射器可以假装它正在插入一个项目列表,但底层仍然会有单独的操作。

更新(基于评论)

如果您追求的是速度,您可以并行化插入。只要您有可用的写入容量,您就可以通过这种方式获得很多速度提升。但它可能会很快变得昂贵。并且每个分区的插入次数限制为 1000 次/秒。

【讨论】:

  • 是的,我可以验证使用 saveAll 函数可以将多条记录保存在一起,但总时间几乎与插入单个记录相同。
猜你喜欢
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 2019-10-13
  • 1970-01-01
  • 2013-06-10
  • 2016-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多