【问题标题】:Relations between dynamo db tablesdynamodb 表之间的关系
【发布时间】:2015-05-15 18:46:02
【问题描述】:

我有两个名为 Country 和 State 的表。 如何通过映射关系获取国家信息。 这是我的代码:

表格:

@DynamoDBTable(tableName = "cd_country2")
public class Country implements Serializable {
    private static final long serialVersionUID = 5698425418072128936L;

    @DynamoDBAutoGeneratedKey
    @DynamoDBHashKey
    private String countryId;

    private String countryCode;
    private String countryName;
    private Long isActive;
}

@DynamoDBTable(tableName = "cd_state2")
public class State implements Serializable {

    private static final long serialVersionUID = -7289597915417184960L;

    @DynamoDBAutoGeneratedKey
    @DynamoDBHashKey
    private String stateId;

    @DynamoDBRangeKey
    private String countryId;

    private String stateCode;

    private String stateName;

    private Long isActive;
}

【问题讨论】:

    标签: java amazon amazon-dynamodb


    【解决方案1】:

    在您的申请中,一个州可以位于多个国家/地区吗?如果不是,则无需将 countryId 设为 rangeKey。像这样的东西应该可以工作:

    @DynamoDBHashKey
    private String stateId;
    
    // attributes    
    private String countryId;
    
    private String stateCode;
    
    private String stateName;
    
    private Long isActive;
    

    给定一个 stateId,您可以从“cd_state2”表中找到它所属的 countryId。

    另一方面,我建议使用这样的架构:

    HashKey: countryId
    RangeKey: stateId
    Attributes: stateCode, stateName, isActive
    

    然后在此表之上构建一个 GSI:

    HashKey: stateId
    

    因此假设 stateId 在国家/地区是唯一的,给定一个州,您可以通过在 GSI 表上执行 GET 来找到它的国家/地区。此外,给定一个 countryId,您可以通过查询基表找到其中的所有州。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-07
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多