LoginSignup
3
1

More than 5 years have passed since last update.

AWS CLIでDynamoDBのScanをやりたいが、ドキュメント読んでもよくわからない。

Last updated at Posted at 2019-03-05

前々から思っているんだけど、AWSの日本語ドキュメントってわかりにくいよね…。
AWS CLIを利用してたDynamoDBのfilterexpressionの使い方探してたんだけど、これでわかるのかな。

次の AWS CLI の例では Thread テーブルをスキャンして、特定のユーザーによって最後に投稿された項目のみを返します。

aws dynamodb scan \
     --table-name Thread \
     --filter-expression "LastPostedBy = :name" \
     --expression-attribute-values '{":name":{"S":"User A"}}'

こんなこと言われてもねえ。TBL定義もわかんないし、何が何やら。勿論、英語版読めばいいんだろうけど。
それでもパッと検索して出てくる英語版ページでわかるかといわれるとやっぱりわからない。

[root@~]$ aws dynamodb scan --table-name server \
{
    "Count": 20, 
    "Items": [
        {
            "ID": {
                "N": "1"
            }, 
            "ip_address": {
                "S": "192.168.1.2"
            }, 
            "start_time": {
                "S": "2018-12-14 01:20:37"
            }, 
            "server_type": {
                "S": "online"
            }
        }, 
(省略)

例えば、↑のようなTBLでserver_typeをベースにfilterかけたいということであれば、↓だよね。

[root@~]$ aws dynamodb scan --table-name server \
>     --filter-expression "server_type = :name" \
>      --expression-attribute-values '{":name":{"S":"online"}}'

--filter-expressionserver_typeを定義。
:nameとして、別名を割り当て、それを--expression-attribute-valuesで引く、みたいな感じと思えばよい(はず)。

で、例えば、これをip_addressでフィルタリングかけたいとかであれば、jq使った方が楽なはず。応答項目をCLIでフィルタリングする方法がどうしてもわからなかった。

[root@~]$ aws dynamodb scan --table-name server \
>     --filter-expression "server_type = :name" \
>      --expression-attribute-values '{":name":{"S":"online"}}' \
>     | jq ".Items[].ip_address"
{
  "S": "192.168.1.2"
}
{
  "S": "192.168.1.5"
}
{
  "S": "192.168.11.1"
}
{
  "S": "192.168.11.10"
}
3
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
3
1