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

2

15

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.

Churchman answered 15/2, 2016 at 14:4 Comment(0)
B
12

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"}}'
Buddy answered 2/9, 2016 at 20:59 Comment(4)
Thanks for this! Using single quotes around the JSON might make it a little easier to read: --expression-attribute-values '{":idValue":{"N":"42"}}'Tysontyumen
Hmm.. definitely agree that is more readable, just tried it though in preparation to update this answer, and at least on Windows it doesn't like it -- breaks after it hits the first double-quoteBuddy
Oh bummer, that was on OS X and Linux.Tysontyumen
single quote saved me.Showery
I
5

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

Issuable answered 15/2, 2016 at 14:14 Comment(8)
MyHashKey - would be your hash key value and here I have taken as String for reference and "abc" will be the value of hash keyIssuable
In your question you have taken query that why I had used that, I will update my question for scan as wellIssuable
For that you can use scan, I have updated the answerIssuable
Thanks, scan does get me some data, but to get a green tick, I want to know how to get my query to work ;-)Churchman
@MarkDickinson I have already specified that just replace get-item in my example to query, I have update the answer :)Issuable
--key isn't a valid argument for queryChurchman
@MarkDickinson You are right, its "--key-conditions" instead of "key"Issuable
Note that --key-conditions is now considered a "legacy" parameter. I've added an answer that conforms to the latest and greatest. Enjoy!Buddy

© 2022 - 2024 — McMap. All rights reserved.