70

So, I have a DynamoDB table Users and I want to return all the contents of this table. Or maybe even some.

I tried

aws dynamodb query --table-name Users 

and it says I have to specify key-condition or key-condition-expression, so I added the following:

aws dynamodb query --table-name Users --key-condition-expression Username = "test"

and it returns an error message " Unknown options: test ".

Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
beejm
  • 2,381
  • 1
  • 10
  • 19

5 Answers5

104

If you want to dump the whole table, just use

aws dynamodb scan --table-name Users
ataylor
  • 64,891
  • 24
  • 161
  • 189
  • 1
    It appears that, for all but the smallest of tables, this yields the error, "An error occurred (ProvisionedThroughputExceededException) when calling the Scan operation (reached max retries: 2): The level of configured provisioned throughput for the table was exceeded. Consider increasing your provisioning level with the UpdateTable API." – kloddant Feb 03 '21 at 14:46
  • 1
    Apparently to fix this error, you can change the read-write capacity mode of the table from provisioned to on-demand. – kloddant Feb 04 '21 at 14:24
50

Try this format:

aws dynamodb get-item --table-name Users --key '{"Username": {"S": "test"}}'
Akavall
  • 82,592
  • 51
  • 207
  • 251
  • If you're getting a `An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema` error when running this, check out this [question](https://stackoverflow.com/questions/42757872/the-provided-key-element-does-not-match-the-schema-error-when-getting-an-item). – D Malan Jan 25 '21 at 10:30
  • 1
    Unknown options: test}}', {S: – kloddant Feb 03 '21 at 14:41
17

Since the question is about using the query operation, here it goes.

As the AWS cli documentation explains, you should separate the attribute values from the condition, by using the --expression-attribute-values parameter:

aws dynamodb query --table-name Users 
    --key-condition-expression "Username = :v1" 
    --expression-attribute-values "{ \":v1\" : { \"S\" : \"test\" } }"

Additionally, you may combine more attributes in the filter (in my case I have a Datetime sort key I want to filter by):

aws dynamodb query 
  --table-name Users
  --key-condition-expression "Username = :v1 AND #Datetime BETWEEN :v2 AND :v3" 
  --expression-attribute-names "{ \"#Datetime\": \"Datetime\" }" 
  --expression-attribute-values "{ \":v1\" : { \"S\" : \"test\" }, \":v2\" : { \"S\" : \"2019-06-06\" }, \":v3\" : { \"S\" : \"2019-06-07\" } }" 

Here the #Datetime mapping is done through the --expression-attribute-names parameter, because Datetime is a reserved keyword, so I cannot use it inside the key condition expression.

SebaGra
  • 2,801
  • 2
  • 33
  • 43
  • Thanks a ton! I was forgot to escape the query params :) – KTM Jun 05 '20 at 08:01
  • The first query here yields the error, "An error occurred (ValidationException) when calling the Query operation: Query condition missed key schema element: id". – kloddant Feb 03 '21 at 14:49
  • 1
    Hey @kloddant, I think you're trying to run a query using a condition that does not include your primary key (`id`). Without knowing how your query looks like, I guess you need to add a secondary index with the attributes you are querying. Or, you may run a `Scan` instead of a `Query`. http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html – SebaGra Feb 04 '21 at 13:00
  • @SebaGra Thanks, yup, had to run a scan instead. – kloddant Feb 04 '21 at 14:23
  • Please @SebaGra correct your syntax mistake: "by using the --expression-atribute-values" an additional t is needed on the attribute – Gonzalo Mar 25 '22 at 21:20
2

As per my understanding you are not passing "key"(hash or hash/range) properly

create a file containing your keys: test.json

{
    "userName": {"S": "abc"},
    "anyRangeKey": {"S": "xyz"}  //optional
}

Run

aws dynamodb get-item --table-name users --key file://test.json

refer:http://docs.aws.amazon.com/cli/latest/reference/dynamodb/get-item.html
Hope that helps

Harshal Bulsara
  • 7,898
  • 4
  • 43
  • 61
1
aws dynamodb get-item --table-name ProductCatalog --key "{""Id"":{""N"":""205""}}" --no-verify-ssl
Vijay
  • 46
  • 6