LoginSignup
6
6

More than 5 years have passed since last update.

既存のDynamoDBのテーブルをjsonフォーマットに変換するワンライナー

Posted at

はじめに

  • AWS cliでテーブル作っていたのですが、jsonで作っておくべきだった・・・。と大変後悔しました。
  • 既存のDynamoDBのテーブルからjsonを出力するスクリプトの記事を見つけたのですが、GlobalSecondaryIndex、LocalSecondaryIndexが無いテーブルの場合はエラーになってしまいますので少し改良したのでご紹介します。
  • このスクリプトをうまく利用することでテーブルの差分をみるのに使えたりするのでオススメです。

スクリプト

  • ここではDynamoDBLocalにあるAssetというテーブルをAsset.jsonというファイルに出力しています。
  • describe-tableでは余計なものがいっぱいついてくるので要素を削除しています。
aws dynamodb describe-table --table-name Asset --endpoint-url http://localhost:8000 |
jq '.Table' |
jq 'del(.TableArn)' |
jq 'del(.GlobalSecondaryIndexes[]?.ItemCount)' |
jq 'del(.GlobalSecondaryIndexes[]?.IndexStatus)' |
jq 'del(.GlobalSecondaryIndexes[]?.IndexArn)' |
jq 'del(.GlobalSecondaryIndexes[]?.IndexSizeBytes)' |
jq 'del(.GlobalSecondaryIndexes[]?.ProvisionedThroughput.NumberOfDecreasesToday)' |
jq 'del(.GlobalSecondaryIndexes[]?.ProvisionedThroughput.LastIncreaseDateTime)' |
jq 'del(.GlobalSecondaryIndexes[]?.ProvisionedThroughput.LastDecreaseDateTime)' |
jq 'del(.LocalSecondaryIndexes[]?.IndexStatus)' |
jq 'del(.LocalSecondaryIndexes[]?.IndexArn)' |
jq 'del(.LocalSecondaryIndexes[]?.ItemCount)' |
jq 'del(.LocalSecondaryIndexes[]?.IndexSizeBytes)' |
jq 'del(.LocalSecondaryIndexes[]?.ProvisionedThroughput.NumberOfDecreasesToday)' |
jq 'del(.LocalSecondaryIndexes[]?.ProvisionedThroughput.LastIncreaseDateTime)' |
jq 'del(.LocalSecondaryIndexes[]?.ProvisionedThroughput.LastDecreaseDateTime)' |
jq 'del(.ProvisionedThroughput.NumberOfDecreasesToday)' |
jq 'del(.ProvisionedThroughput.LastIncreaseDateTime)' |
jq 'del(.ProvisionedThroughput.LastDecreaseDateTime)' |
jq 'del(.TableSizeBytes)' |
jq 'del(.TableStatus)' |
jq 'del(.ItemCount)' |
jq 'del(.IndexArn)' |
jq 'del(.CreationDateTime)' > Asset.json

出力されたスクリプトを利用してcreate-tableする

aws dynamodb create-table --endpoint-url http://localhost:8000 --table-name Asset --cli-input-json file://Asset.json
6
6
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
6
6