{DynamoDB}DynamoDB data import from Amazon S3: how it works

 

https://dev.classmethod.jp/articles/amazon-dynamodb-import-amazon-s3-data-into-a-new-table/
https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/S3DataImport.HowItWorks.html


During the Amazon S3 import process, DynamoDB creates a new target table that will be imported into.
DynamoDB can import data in three formats: CSV, DynamoDB JSON, and Amazon Ion.

 

-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version


-- 1.2 jqインストール
sudo yum -y install jq

 


-- 2. S3バケット作成

aws s3 mb s3://bucket123


-- 3. テストファイルのアップロード

vim test01.csv

col1,col2,col3
val11,val12,100
val21,val22,200
val31,val32,300

vim test02.csv

col1,col2,col3
val41,val42,400
val51,val52,500
val61,val62,600

aws s3api put-object --bucket bucket123 --key test/test01.csv --body test01.csv --content-type text/csv
aws s3api put-object --bucket bucket123 --key test/test02.csv --body test02.csv --content-type text/csv


aws s3 ls s3://bucket123 --recursive


-- 4. インポートをリクエス

指定したフォルダ以下のファイルがインポートされる

aws dynamodb import-table \
--s3-bucket-source S3Bucket=bucket123,S3KeyPrefix=test \
--input-format CSV \
--table-creation-parameters '{
  "TableName": "tab1",
  "AttributeDefinitions": [
    {
      "AttributeName": "col1",
      "AttributeType": "S"
    },
    {
      "AttributeName": "col2",
      "AttributeType": "S"
    }
  ],
  "KeySchema": [
    {
      "AttributeName": "col1",
      "KeyType": "HASH"
    },
    {
      "AttributeName": "col2",
      "KeyType": "RANGE"
    }  
  ],
  "BillingMode": "PAY_PER_REQUEST"
}'

 


aws dynamodb list-imports

aws dynamodb describe-import \
--import-arn arn:aws:dynamodb:ap-northeast-1:999999999999:table/tab1/import/11111111111111-11111111

 

-- 5. インポート結果確認

aws dynamodb list-tables
aws dynamodb describe-table --table-name tab1

aws dynamodb scan --table-name tab1

 

-- 6. クリーンアップ

-- テーブル削除
aws dynamodb list-tables
aws dynamodb delete-table --table-name tab1


-- バケット削除
aws s3 ls
aws s3 rb s3://bucket123 --force