Difference between local and global indexes in DynamoDB

前端 未结 7 2022
轻奢々
轻奢々 2020-12-22 15:42

I\'m curious about these two secondary indexes and differences between them. It is hard to imagine how this looks like. And I think, this will help more people than just me.

7条回答
  •  感动是毒
    2020-12-22 16:26

    These are the possible searches by index:

    • By Hash
    • By Hash + Range
    • By Hash + Local Index
    • By Global index
    • By Global index + Range Index

    Hash and Range indexes of a table: These are the usual indexes of previous versions of the Amazon AWS SDK.

    Global and Local indexes: These are 'additional' indexes created on a table, in addition to existing hash and range indexes of the table. Global index is similar to a hash. Range index behave similarly to the range index used with the hash of the table. In you entity model in your code, the getter must be annotated in this way:

    • For global indexes:

      @DynamoDBIndexHashKey(globalSecondaryIndexName = INDEX_GLOBAL_RANGE_US_TS)
      @DynamoDBAttribute(attributeName = PROPERTY_USER)
      public String getUser() {
          return user;
      }
      
    • For range index associated to the global index:

      @DynamoDBIndexRangeKey(globalSecondaryIndexName = INDEX_GLOBAL_RANGE_US_TS)
      @DynamoDBAttribute(attributeName = PROPERTY_TIMESTAMP)
      public String getTimestamp() {
          return timestamp;
      }
      

    Besides, if you read a table by a Global index, it must be an Eventual read (not Consistent read):

    queryExpression.setConsistentRead(false);
    

提交回复
热议问题