DynamoDB - Query Table

DynamoDB - Query Table

To query a table, you first select a table, specify a partition key, and run the query; with the ability to use secondary indexes and perform deeper filtering using scans.

Use the GUI Console, Java, or another option to complete the task.

Query table using GUI console

Run some simple queries using the tables you created earlier. First, open the console at https://console.aws.amazon.com/dynamodb.

Select Tables in the navigation bar and select Reply from the list of tables. Then select the Elements tab to see the uploaded data.

Select the data filtering link ("Scan: [Table] Reply") below the Create Item button .

Query table using GUI console

On the filtering screen, select Request for the operation. Enter the appropriate partition key value and click Start .

The response table then returns the matching items.

Answer table

Query table using Java

Use the query method in Java to perform data lookup operations. A partition key value is required, and a sort key is optional.

Encode a Java query by first creating a querySpec object with parameter descriptions. Then pass the object to the request method. We are using the partition key from the previous examples.

You can view the following example −

import java . util . HashMap ; import java . util . Iterator ;


import com . amazonaws . services . dynamodbv2 . AmazonDynamoDBClient ; import com . amazonaws . services . dynamodbv2 . document . DynamoDB ; import com . amazonaws . services . dynamodbv2 . document . Item ; import com . amazonaws . services . dynamodbv2 . document .


ItemCollection ; import com . amazonaws . services . dynamodbv2 . document . QueryOutcome ; import com . amazonaws . services . dynamodbv2 . document . table ; import com . amazonaws . services . dynamodbv2 . document . spec . QuerySpec ; import com . amazonaws .



services . dynamodbv2 . document . utilities . NameMap ;

public class ProductsQuery { public static void main ( String [] args ) throws Exception { AmazonDynamoDBClient client = new AmazonDynamoDBClient () . withEndpoint ( "http://localhost:8000" );     
          
         
           
      
      DynamoDB dynamoDB = new DynamoDB ( client ); Table table = dynamoDB . getTable ( "Products" ); HashMap < String , String > nameMap = new HashMap < String , String >();  
      nameMap . put ( "#ID" , "ID" ); HashMap < String , Object > valueMap = new    
        
             
         HashMap < String , Object >();  
      valueMap . put ( ":xxx" , 122 ); QuerySpec querySpec = new QuerySpec () . withKeyConditionExpression ( "#ID = :xxx" ) . withNameMap ( new NameMap (). with ( "#ID" , "ID" )) . withValueMap ( valueMap );  
         
          
            
           
      
      ItemCollection < QueryOutcome > items = null ; Iterator < Item > iterator = null ; Item item = null ; try { System . out . println ( "Product with ID 122" );  
         items = table . query ( querySpec );   
         iterator = items . iterator ();  
        
         
        
          
         
         while ( iterator . hasNext ()) {  
            item = iterator . next (); System . out . println ( item . getNumber ( "ID" ) + ": " + item . getString ( "Nomenclature" )); } } catch ( Exception e ) { System . err . println (   
               
                
          
          
         "Cannot find products with the ID number 122" ); System . err . println ( e.getMessage ( ) ); } } } 
          
       
    

Note that the query uses the partition key, however secondary indexes provide another option for queries. Their flexibility allows you to query for non-key attributes, a topic that will be discussed later in this tutorial.

The scan method also supports lookup operations by collecting all table data. The optional .withFilterExpression prevents elements outside the specified criteria from appearing in the results.

Later in this tutorial, we'll discuss scanning in detail . Now let's look at the following example: