Database Reference
In-Depth Information
Once the QueryRequest instance is configured, we can invoke the query method avail-
able with DynamoDBClient (just as we invoked the createTable method to create
the table) by enclosing the QueryRequest instance. The query operation will return an
instance of the
com.amazonaws.services.dynamodbv2.model.QueryResult class. We can
use the getItems() method available with the QueryResult instance, which will re-
turn a List<Map<String, AttributeValue>> . We can iterate this list to fetch
the attribute name and value.
Tip
Querying a secondary index is not much different from the earlier code. All we need to do
is add .withIndexName("Idx_Pub_Edtn") to the QueryRequest instance,
where Idx_Pub_edtn is the name of the index.
The previous code will return all the items whose hash key attribute value BookTitle
equals SCJP. Since the range key is also part of the primary key, we can add the range key
condition to the QueryRequest instance (along with the hash key condition). We need
to add the following lines to the previous code block:
//Creates a RANGE condition equivalent to PubDate >
1989-12-28
Condition rangeKeyCondition = new Condition()
.withComparisonOperator(ComparisonOperator.GT.toString())
.withAttributeValueList(new AttributeValue()
.withS("1989-12-28"));
keyConditions.put("PubDate", rangeKeyCondition);
We can put (or add) this range key condition to the key condition map, which is already
added to the QueryRequest instance (in the first code block). Now, the query will re-
turn the items matching both conditions. Since the date data type is not there in Dy-
namoDB, the comparison will be performed as a string comparison. That is the reason
why we chose the date format as yyyy-MM-dd instead of other formats.
Tip
If we store the PubDate attribute in the same dd-MMM-yyyy format (used in the earlier
chapters), then the GT and LT operations will not yield the predicted output because the
comparison operation here is performed as a string comparison (and not as a date string).
Search WWH ::




Custom Search