从 DynamoDB 导出数据 [英] Export data from DynamoDB
问题描述
是否可以以某种格式从 DynamoDB 表中导出数据?
Is it possible to export data from DynamoDB table in some format?
具体用例是,我想从我的生产 dynamodb 数据库中导出数据并将该数据导入我的本地 dynamodb 实例,以便我的应用程序可以使用本地数据副本而不是生产数据.
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.
我使用 link 作为DynamoDB 的本地实例.
I use link as a local instance of DynamoDB.
推荐答案
这会将所有项目导出为 jsons 文档
This will export all items as jsons documents
aws dynamodb scan --table-name TABLE_NAME > export.json
此脚本将从远程 dynamodb 表中读取并将完整表导入本地.
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
这篇关于从 DynamoDB 导出数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!