LoginSignup
0
0

More than 5 years have passed since last update.

DynamoDBの特定テーブルをtruncateするためのシェルスクリプト

Last updated at Posted at 2018-01-22

概要

DynamoDBの指定したテーブルをtruncateします。

注意事項

  • batchWriteItemを使用していないためとても遅いです(要改善)。

前提条件

  • aws cliがcredentialを含めて設定済みである必要があります。
  • jqがインストール済みである必要があります。

パラメータ

  • $1: テーブル名
  • $2: パーティションキーのAttribute name
  • $3: ソートキーのAttribute name(オプション)

シェルスクリプト

以下を任意のディレクトリにtruncate_dynamodb.shとして保存してください。

#!/bin/sh

TABLE_NAME=$1
P_KEY=$2
S_KEY=$3
REGION=ap-northeast-1
TRUNCATE_LIST=/tmp/truncate.lst_$(date +%s)

if [ -z "$TABLE_NAME" ] || [ -z "$P_KEY" ]; then
        echo "truncate_dynamodb.sh [Table Name] [Partition Key] [Sort Key(Optional)]"
        exit 1;
fi

KEY_LIST="$P_KEY: .$P_KEY"

if [ -n "$S_KEY" ]; then
        KEY_LIST="$KEY_LIST, $S_KEY: .$S_KEY"
fi

echo "Key is '$KEY_LIST'"

aws --region ap-northeast-1 dynamodb scan --table-name $TABLE_NAME | jq -c ".Items[] | { $KEY_LIST  }" > $TRUNCATE_LIST

while read LINE
do
        COMMAND="aws --region "$REGION" dynamodb delete-item --table-name $TABLE_NAME --key "$LINE""
        $COMMAND
done < $TRUNCATE_LIST

rm $TRUNCATE_LIST
0
0
1

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
0
0