Database Reference
In-Depth Information
Using local secondary indexes
We have already seen the local secondary indexes in Chapter 2 , Data Models . So, if
someone wants to query a DynamoDB table data using attributes other than the primary
key attributes, then we need to use local secondary index.
Suppose we modify our product table to accommodate one more attribute named
lastModifiedBy , this attribute would be updated every time some user makes changes
to it. Our requirement is to find the change made by a certain user for a given product ID.
To do so, we need to create a local secondary index on lastModifiedBy . Here is how
the table will look now:
productId (hash key)
recordId (range key)
Data
lastModifiedBy (Local Secondary Index)
123
productName
BMW Z
Alice
123
cost
$20000
Bob
456
productName
Hill range bicycle Alice
456
cost
$120
Alice
789
productName
Base ball bat
Bob
789
cost
$50
Bob
Android
Here is how we have to write queries to fetch an attribute modified by a certain user:
// create map for keys
Map keyConditions = new HashMap();
// Specify the key conditions , here product id = 123
Condition hashKeyCondition = new Condition()
.withComparisonOperator(ComparisonOperator.EQ.toString())
.withAttributeValueList(new
AttributeValue().withN("123"));
Search WWH ::




Custom Search