| by Arround The Web | No comments

DynamoDB Query Examples

A database is a collection of data—DynamoDB is no exception. Databases contain lots of data that may be difficult to retrieve without a pre-defined mechanism. This is where the query command plays an important role.

Like any database management system, DynamoDB offers various querying methods through which you can interact with the system to access and manipulate your data. Querying in DynamoDB is a very powerful tool that allows you to retrieve an item or a group of items related to the query command.

This article outlines the main examples of the DynamoDB Query Operations.

Common DynamoDB Query Examples

The default behavior for the query operation in DynamoDB is to return all items associated with the query items. Interestingly, the query command in DynamoDB is useable with tables or secondary indexes.

Whichever the case, always ensure that you specify the equality condition for your partition key’s value. Again, you may need to provide a different condition for the sort key in case you use one in the command.

Other parameters that you will encounter when querying DynamoDB also include KeyConditionExpression and FilterExpression. The KeyConditionExpression specifies the key values that you intend to query. On the other hand, the FilterExpression removes items from the query results before you get a response. You will use the ExpressionAttributeValues as placeholders for the expression parameters mentioned.

DynamoDB Query Examples include:

Finding a Single Item from a Table Based on Primary Keys

You can use the Query utility in DynmoDB to find a single item by relying on a combination of the item’s partition key and sort key values. The syntax for such an operation is as follows:

aws dynamodb query \

--table-name MyTableName \

--key-condition-expression 'PartitionKey = :pk AND SortKey = :sk' \

--expression-attribute-values '{":pk":{"S":"a123b"},":sk":{"S":"def456b"}}'

The above query utility aims at retrieving an item with a partition key value a1234b and a sort key value odef456b from the MyTableName table. For example, we can use the above utility to find an item in our ‘Orders’ table. The item’s partition key value can be wr546gg representing the Customer_ID, while its sort key value can be 24536433 representing the order number.

The result can be as follows:

The above result brings back Holiday Books as the only item that has a partition key value of wr546gg and a sort key value of 24536433. It also brings you all the other additional attributes associated with the item. In our illustration, it brings back the invoice number and the amount paid.

Notably, the query command will bring back a blank list if there is no item that matches the specified primary key values.

Retrieve All Items From a DynamoDB Table Matching Specific Attribute Values

Use a filter expression to retrieve all items that have a similar attribute value in a specific DynamoDB table.

The command for this query operation is as shown below.

aws dynamodb query \

--table-name MyTableName \

--filter-expression 'OtherAttribute1 = :val' \

--expression-attribute-values '{":val":{"S":"value1"}}'

For instance, we can use the above query command to retrieve songs with more that go for over 5 minutes in our Music table. To achieve this, we will set our OtherAttribute1 value to 5.00 and MyTableName to Music.

The result can be as shown below:

It is necessary to truncate the list for brevity. But from the results, our query command retrieved 11 items with a filterexpression value of 5.00 from our DynamoDB Music table.

Retrieving all Items With a Particular Range of Attribute Values

The below command comes in handy when retrieving items within a particular table:

aws dynamodb query \

--table-name <table name> \

--key-condition-expression "attribute_name BETWEEN :val1 AND :val2" \

--expression-attribute-values '{":val1":{"N":"<value1>"},":val2":{"N":"<value2>"}}'

Of course, you will need to replace all the attributes with your customized credentials like you should with any other command line. For example, we will use our Employee table with a range key attribute christened “age.” We will aim to retrieve employers aged between 30 and 42.

Our new command line will be as follows:

aws dynamodb query \

--table-name Users \

--key-condition-expression "age BETWEEN :val1 AND :val2" \

--expression-attribute-values '{":val1":{"N":"30"},":val2":{"N":"42"}}'

Running the above utility will bring a response similar to the one in the figure below;

The illustration above shows that the query brought back 6 items, indicating the attribute value for each query. The ScannedCount is the number of items scanned in the table while the CapacityUnits is the amount of units consumed during the operation.

Conclusion

Since DynamoDB is a NoSQL database, its query operation does not behave like that of your ordinary AQL database. But once you do, you will find out that the operation is pretty powerful and will make your interaction with the database abreeze.

Share Button

Source: linuxhint.com

Leave a Reply