LoginSignup
2
1

More than 5 years have passed since last update.

awscliからDynamoDBのインデックスへQueryするコマンドと引数

Posted at

例えば user_id と date カラムで検索したい場合は下記のようなコマンドになる

$ aws dynamodb query \
     --table-name {{ テーブルの名前 }} \
     --index-name {{ インデックスの名前 }} \
     --key-condition-expression '#p = :1 AND #s > :2' \
     --expression-attribute-values '{":1": {"N": "12345"}, ":2": {"S": "2018-06-05"}}' \
     --expression-attribute-names '{"#h": "user_id", "#s": "date"}' \
     --return-consumed-capacity INDEXES

--key-condition-expression

#p = :1 AND #s > :2

等号や不等号でクエリの条件関係を書くところ。
#p:2 などのカラム名、変数名はこの後の引数で埋めていく

--expression-attribute-values

{":1": {"N": "12345"}, ":2": {"S": "2018-06-05"}}

jsonでクエリのバインド変数みたいなところを埋める
数値型 ("N") のカラムの条件値でもjson上は文字列として渡す必要があるようだ

--expression-attribute-names

{"#h": "site_id", "#s": "date"}

カラム名を側を埋めるための指定をjsonで記述する
date が予約語だそうで、今回はそのワークアラウンドで使用した

ちなみに #h は ハッシュキーで #s はソートキー

--return-consumed-capacity INDEXES

キャパシティユニットの使用量をインデックスごとテーブルごとに詳細に出してくれる

参考

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1