Database Reference
In-Depth Information
Local secondary index
While creating the secondary index, the hash and range key of the table and index will be
inserted into the index; optionally, the user can specify what other attributes need to be ad-
ded. There are three kinds of projection possible in DynamoDB:
•
KEYS_ONLY
: Using this, the index consists of the hash and range key values of
the table and index
•
INCLUDE
: Using this, the index consists of attributes in
KEYS_ONLY
plus other
non-key attributes that we specify
•
ALL
: Using this, the index consists of all of the attributes from the source table
The following code shows the creation of a local secondary index named
Idx_PubDate
with
BookTitle
as the hash key (which is a must in the case of a local secondary index),
PubDate
as the range key, and using the
KEYS_ONLY
projection:
private static LocalSecondaryIndex getLocalSecondaryIndex() {
ArrayList<KeySchemaElement> indexKeySchema =
newArrayList<KeySchemaElement>();
indexKeySchema.add(new KeySchemaElement()
.withAttributeName("BookTitle")
.withKeyType(KeyType.HASH));
indexKeySchema.add(new KeySchemaElement()
.withAttributeName("PubDate")
.withKeyType(KeyType.RANGE));
LocalSecondaryIndex lsi = new LocalSecondaryIndex()
.withIndexName("Idx_PubDate")
.withKeySchema(indexKeySchema)
.withProjection(new Projection()
.withProjectionType("KEYS_ONLY"));
return lsi;
}
The usage of the
KEYS_ONLY
index type will create the smallest possible index and the
usage of
ALL
will create the biggest possible index. We will discuss the trade-offs between
these index types a little later.