如何在更改属性DynamoDB的数据类型时确保向后兼容

发布于 2025-02-02 23:51:01 字数 1130 浏览 1 评论 0 原文

我正在尝试在我的一个DDB表中更改属性的数据类型,但是由于这些数据已从并写入并写入到属性的数据类型时会导致读取旧记录时随后的读取失败,看起来像这样:

could not unconvert attribute
DynamoDBMappingException: expected M in value {N: 1000,}

我的问题是如何更改表中属性的数据类型,并构建更改,以便我仍然可以阅读以前记录中存在的双重值。这是所讨论的课程:

@DynamoDBTable(tableName = "Sections")
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SectionRecord {
    @DynamoDBHashKey
    private String id;
    private Map<String, Double> sectionTarget; //previous definition: private Double sectionTarget;
    
    public void setSectionTarget(Double sectionTarget, String key) {
       if (this.sectionTarget == null) {
          this.sectionTarget = new HashMap<Double, String>();
       }
       this.sectionTarget.put(key, sectionTarget);
    }
    
    public void getSectionTarget(String key) {
       return this.sectionTarget.get(key);
    }

}

最终,我尝试阅读这样的唱片:

mapper.load(SectionRecord.class, id);

大概是问题所在的地方 - 我试图阅读双重(当前存在于DDB中)作为地图(更改我已经对属性做出了)。

我很想听听如何最好地构建这种变化的指导,以便可以缓解这些向后兼容的问题。

I am attempting to change the data type of an attribute in one of my DDB tables, but because this data is read from and written to, altering the data type of the attribute causes subsequent read failures when reading old records, which look like this:

could not unconvert attribute
DynamoDBMappingException: expected M in value {N: 1000,}

My question is about how I can change the data type of an attribute in my table, and architect the change such that I can still read the Double value that exists in previous records. Here is the class in question:

@DynamoDBTable(tableName = "Sections")
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SectionRecord {
    @DynamoDBHashKey
    private String id;
    private Map<String, Double> sectionTarget; //previous definition: private Double sectionTarget;
    
    public void setSectionTarget(Double sectionTarget, String key) {
       if (this.sectionTarget == null) {
          this.sectionTarget = new HashMap<Double, String>();
       }
       this.sectionTarget.put(key, sectionTarget);
    }
    
    public void getSectionTarget(String key) {
       return this.sectionTarget.get(key);
    }

}

And eventually, I try to read a record like this:

mapper.load(SectionRecord.class, id);

Which is presumably where the issue comes from - I'm trying to read a Double (which exists in the ddb currently) as a map (the changes I've made to the attribute).

I'd love to hear some guidance on how best to architect such a change such that these backwards compatibility issues could be mitigated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空气里的味道 2025-02-09 23:51:01

必须

  • 类型创建一个新的
  • 属性
  • 使用新 旧字段,逻辑现在只能依靠新字段

欢迎来到您没有数据库迁移的Dynamo :(

You have to

  • Create a new attribute with the new type, both in dynamo and in SectionRecord
  • Your code should be able to read and work with both
  • deploy it on production and wait for the old data to disappear (or create a custom migration logic)
  • Delete the old field, the logic can now rely only on the new field

Welcome to dynamo where you don't have DB migrations :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文