We use machine learning technology to do auto-translation. Click "English" on top navigation bar to check Chinese version.
Query data with DynamoDB Shell – a command line interface for Amazon DynamoDB
For newcomers to DynamoDB, the learning curve can be steep, so we developed DynamoDB Shell (ddbsh), an open-source command line interface for
For example, you can use ddbsh to create a table by running a command such as:
You can get a quick introduction to ddbsh from
This post covers two topics. First, you learn how connect to DynamoDB and then to DynamoDB local. Then, you learn a method to filter data using the SQL WHERE
clause.
To get started, download the ddbsh source code from
Connect to an Amazon Web Services Region
ddbsh is uses the
To connect to a Region
- Review the default Region, Amazon Web Services Access Key, and Amazon Web Services Secret Key in the Amazon Web Services configuration file. Refer to the
documentation for the exact location of the file on your operating system. The documentation provides specific guidance forDynamoDB local andDynamoDB (web service ). You can also use theaws configure
command or set environment variables todetermine how to connect to DynamoDB . - Check whether you are able to connect to DynamoDB using the Amazon Web Services CLI. For example, you could run:
- Resolve any errors. After you’ve run the preceding command without any errors, you will be able to connect to your default Region as shown in the interactive session that follows.
- At the prompt, you can run commands. For a list of commands, run the HELP command. Commands are terminated with a semicolon (;)
Connect to DynamoDB local.
Before you start this step, make sure that you have ddbsh version 0.2 or higher and Amazon Web Services SDK version 1.11 or higher.
To connect to DynamoDB local
- Follow the instructions to
install DynamoDB local on your computer . - In a terminal session, set up the environment variables to connect to the DynamoDB local instance.
- In a second terminal session, launch DynamoDB local.
- Return to the first terminal session. You should be able to connect with DynamoDB local by using an Amazon Web Services CLI command similar to the following:
- If you’re able to do this, then you can connect to the local instance with
ddbsh
.
The asterisk in parentheses (*) after the region name indicates that a non-standard endpoint is being used. You’re now connected to DynamoDB local. Note that when connected to DynamoDB local, the unit tests will not succeed.
Filter data with the WHERE clause
You can use a WHERE clause with SELECT
, UPDATE
, DELETE
, and UPSERT
. In the WHERE
clause, you can include multiple logical expressions that can be joined with the logical AND
and logical OR
. In addition, you can negate a logical expression with the NOT
operator.
The following logical expressions are supported.
- Logical comparison of the form
attribute op value
where attribute is a table attribute, op is one of <, >, <=, >=, =, or !=. -
attribute_exists(attr)
evaluates to true if attr exists in an item. -
begins_with(attr, prefix)
evaluates to true if the attr begins with prefix. -
attr between value and value
evaluates to true if attr is between the two values provided. -
attribute_type (attr, type)
evaluates to true if attr is of the specified type.
Consider this sample data:
Here are some sample queries:
You can write more complex queries by using using parenthesis to group items.
You can use the EXPLAIN
command to see how ddbsh will run your query.
You can use a WHERE
clause in an UPDATE
as well.
You can delete an attribute. The following example deletes the attribute c
.
Cleanup
Once you’re completed your experiments, remember to remove any tables you might have created to avoid incurring costs. You can use DROP TABLE
in ddbsh
to do that. You can stop DynamoDB local by going to the terminal where you launched it (the second session), and shut it down with Ctrl-C.
Conclusion
So, there you have it—with ddbsh
you can read, modify, or delete data from a table and you can filter which items are acted on by using the WHERE
clause, just as you would in standard SQL.
We’ve heard from customers and prospects that they want resources that will help them learn the DynamoDB API. We’re experimenting with some solutions to this such as the EXPLAIN
command in ddbsh. We would appreciate your feedback on this tool. What can we add to ddbsh that would make it easier for you to use DynamoDB?
If there are specific functions or functionality that you would like to see implemented in ddbsh, let us know by entering a support ticket or by emailing me directly at amrithie (at) amazon (dot) com. We’re also looking for contributors who are interested in helping extend and maintain ddbsh. Specifically, if you would like to submit pull requests that will make it easier for users to install and use ddbsh (such as by providing packaging scripts) that would be most appreciated. A
A final call to action for all of you. If you would like a homebrew solution to install ddbsh on MacOS, help us by forking, starring, and watching the ddbsh GitHub repository. The pull request to the
About the author
Amrith Kumar is a Senior Principal Engineer in Amazon Web Services and works on Amazon DynamoDB.
The mentioned AWS GenAI Services service names relating to generative AI are only available or previewed in the Global Regions. Amazon Web Services China promotes AWS GenAI Services relating to generative AI solely for China-to-global business purposes and/or advanced technology introduction.