What is a valid dynamodb key-condition-expression for the cli

10,058

Solution 1

Here's a command-line-only approach you can use with no intermediate files.

First, use value placeholders to construct your key condition expression, e.g.,

--key-condition-expression "Id = :idValue"

(Don't forget the colon prefix for placeholders!)

Next, construct an expression-attribute-values argument. Note that it expects a JSON format. The tricky bit I always try to forget with this is that you can't just plug in 42 for a number or "foo" for a string. You have to tell DynamoDb the type and value. Ref AWS docs for the complete breakdown of how you can format the value specification, which can be quite complex if you need it to be.

For Windows you can escape quotation marks in it by doubling them, e.g.,

--expression-attribute-values "{"":idValue"":{""N"":""42""}}"

For MacOS/Linux, single quote is required around the JSON:

--expression-attribute-values '{":idValue":{"N":"42"}}'

Solution 2

create a file containing your keys: test.json

{
    "yourHashKeyName": {"S": "abc"},
    "YourRangeKey": {"S": "xyz"}  //optional
}

Run

aws dynamodb query --table-name "your table name" --key-conditions file://test.json

refer: http://docs.aws.amazon.com/cli/latest/reference/dynamodb/query.html

For scanning the table

aws dynamodb scan --table-name "you table name"

No need to pass any keys as we scan the whole table (Note: It will get max 1MB of data)

refer:http://docs.aws.amazon.com/cli/latest/reference/dynamodb/scan.html

Share:
10,058

Related videos on Youtube

Mark Dickinson
Author by

Mark Dickinson

I am a freelance developer based in the north of England. I help teams to build great software, working with agile techniques to get the best results. I am also responsible for the behaviour testing project NaughtyStep. An open source library, which aims to make it easy to test the behaviour of .Net software. I'm currently building RESTful APIs, sites that act as clients for them, and using NaughtyStep to test that they are behaving properly. Perhaps I can help you with BDD, testing in general, or any other software projects you may have. Check out my blog which you can reach via my site

Updated on June 04, 2022

Comments

  • Mark Dickinson
    Mark Dickinson over 1 year

    Could somebody please tell me what a valid key condition expression would be. I am trying to run a query on a simple table called MyKeyTable. It has two "columns," namely Id and AnotherNumberThatICareAbout which is of type Long.

    I would like to see all the values I put in. So I tried:

    aws dynamodb query --select ALL_ATTRIBUTES --table-name MyKeyTable
    --endpoint http://localhost:8000 
    --key-condition-expression "WHAT DO I PUT IN HERE?"
    

    What hash do I need to put in? The docs are a bit lame on this imho. Any help appreciated, even if it's just a link to a good doc.