DynamoDB - Query

Queries locate items or secondary indexes through primary keys. Executing a query requires a partition key and a specific value, or a sort key and value; with the ability to filter comparisons. The default query behavior is to return every attribute for the elements associated with the provided primary key. However, you can specify the desired attributes using the ProjectionExpression parameter .
The query uses KeyConditionExpression parameters to select elements, which requires the name and value of the section key to be provided in the form of an equality condition. You also have the option to provide an additional condition for any existing sort keys.
Here are some examples of key sort conditions:
Sr.No | Condition and Description |
---|---|
one |
x = y It evaluates to true if the x attribute is equal to y. |
2 |
x<y It evaluates to true if x is less than y. |
3 |
x <= y It evaluates to true if x is less than or equal to y. |
4 |
x > y It evaluates to true if x is greater than y. |
five |
x>=y It evaluates to true if x is greater than or equal to y. |
6 |
x between y and z It evaluates to true if x is both >= y and <= z. |
x = y
It evaluates to true if the x attribute is equal to y.
x<y
It evaluates to true if x is less than y.
x <= y
It evaluates to true if x is less than or equal to y.
x > y
It evaluates to true if x is greater than y.
x>=y
It evaluates to true if x is greater than or equal to y.
x between y and z
It evaluates to true if x is both >= y and <= z.
DynamoDB also supports the following functions: begin_with(x, substr)
true if the x attribute starts with the specified string.
The following conditions must meet certain requirements −
-
Attribute names must begin with a character in the az or AZ set.
-
The second character of the attribute name must be in the set az, AZ, or 0-9.
-
Attribute names cannot use reserved words.
Attribute names must begin with a character in the az or AZ set.
The second character of the attribute name must be in the set az, AZ, or 0-9.
Attribute names cannot use reserved words.
Attribute names that do not conform to the above restrictions may define a placeholder.
The query is processed by searching in the order of the sort keys and using any available filter conditions and expressions. Queries always return a result set, and if there are no matches, it returns an empty one.
The results are always returned in sort key order and data type order, with a variable default in ascending order.
Requests from Java
Queries in Java allow you to query tables and secondary indexes. They require the specification of split keys and equality conditions, with the ability to specify keys and sort conditions.
The general required steps for querying in Java include creating an instance of the DynamoDB class, an instance of the Table class for the target table, and calling the query method of the Table instance to obtain the query object.
The response to the request contains an ItemCollection object that provides all the returned items.
The following example demonstrates verbose queries:
DynamoDB dynamoDB = new DynamoDB ( new AmazonDynamoDBClient ( new ProfileCredentialsProvider ())); Table table = dynamoDB . getTable ( "Response" ); QuerySpec spec = new QuerySpec () . withKeyConditionExpression ( "ID = :nn" ) . withValueMap ( new ValueMap () . withString ( ":nn" , "Product Line 1#P1 Thread 1" )); ItemCollection < QueryOutcome > items = table . query ( spec ); Iterator < Item > iterator = items . iterator (); Item item = null ; while ( iterator . hasNext ()) { item = iterator . next (); System . out . println ( item.toJSONPretty ( ) ); }
The request method supports many optional parameters. The following example shows how to use these options:
Table table = dynamoDB . getTable ( "Response" ); QuerySpec spec = new QuerySpec () . withKeyConditionExpression ( "ID = :nn and ResponseTM > :nn_responseTM" ) . withFilterExpression ( "Author = :nn_author" ) . withValueMap ( new ValueMap () . withString ( ":nn" , "Product Line 1#P1 Thread 1" ) . withString ( ":nn_responseTM" , twoWeeksAgoStr ) . withString ( ":nn_author" , "Member 123" )) . withConsistentRead ( true ); ItemCollection < QueryOutcome > items = table . query ( spec ); Iterator < Item > iterator = items . iterator (); while ( iterator . hasNext ()) { System . out . println ( iterator.next (). toJSONPretty ( ) ); }
You can also consider the following larger example.
Note. The following program can use a previously created data source. Before attempting to execute, acquire the supporting libraries and create the necessary data sources (tables with the required characteristics or other referenced sources).
This example also uses the Eclipse IDE, the AWS credential file, and the AWS toolkit in the Eclipse AWS Java project.