Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export data from DynamoDB

Is it possible to export data from DynamoDB table in some format?

The concrete use case is that I want to export data from my production dynamodb database and import that data into my local dynamodb instance so my application can work with local copy of data instead of production data.

I use link as a local instance of DynamoDB.

like image 401
Kristian Ačkar Avatar asked Sep 19 '13 13:09

Kristian Ačkar


People also ask

How do I export data from DynamoDB?

To export a DynamoDB table, you use the AWS Data Pipeline console to create a new pipeline. The pipeline launches an Amazon EMR cluster to perform the actual export. Amazon EMR reads the data from DynamoDB, and writes the data to an export file in an Amazon S3 bucket.


1 Answers

This will export all items as jsons documents

aws dynamodb scan --table-name TABLE_NAME > export.json 

This script will read from remote dynamodb table and import into the local the full table.

TABLE=YOURTABLE maxItems=25 index=0  DATA=$(aws dynamodb scan --table-name $TABLE --max-items $maxItems) ((index+=1))  echo $DATA | jq ".Items | {\"$TABLE\": [{\"PutRequest\": { \"Item\": .[]}}]}" > inserts.jsons aws dynamodb batch-write-item --request-items file://inserts.jsons --endpoint-url http://localhost:8000   nextToken=$(echo $DATA | jq '.NextToken') while [[ "${nextToken}" != "" ]] do   DATA=$(aws dynamodb scan --table-name $TABLE --max-items $maxItems --starting-token $nextToken)   ((index+=1))   echo $DATA | jq ".Items | {\"$TABLE\": [{\"PutRequest\": { \"Item\": .[]}}]}" > inserts.jsons   aws dynamodb batch-write-item --request-items file://inserts.jsons --endpoint-url http://localhost:8000   nextToken=$(echo $DATA | jq '.NextToken') done 

Here are a version of the script using files to keep the exported data on disk.

TABLE=YOURTABLE maxItems=25 index=0 DATA=$(aws dynamodb scan --table-name $TABLE --max-items $maxItems) ((index+=1)) echo $DATA | cat > "$TABLE-$index.json"  nextToken=$(echo $DATA | jq '.NextToken') while [[ "${nextToken}" != "" ]] do   DATA=$(aws dynamodb scan --table-name $TABLE --max-items $maxItems --starting-token $nextToken)   ((index+=1))   echo $DATA | cat > "$TABLE-$index.json"   nextToken=$(echo $DATA | jq '.NextToken') done  for x in `ls *$TABLE*.json`; do   cat $x | jq ".Items | {\"$TABLE\": [{\"PutRequest\": { \"Item\": .[]}}]}" > inserts.jsons   aws dynamodb batch-write-item --request-items file://inserts.jsons --endpoint-url http://localhost:8000 done 
like image 167
Ivailo Bardarov Avatar answered Sep 22 '22 21:09

Ivailo Bardarov