Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export a dynamodb table as a csv through aws-cli ( without using pipeline)

I am new to aws-cli and I am trying to export my dynamodb table as a csv so that i can import it directly into postgresql. Is there a way to do that using aws-cli ?

So far i have came across this command aws dynamodb scan --table-name . But this does not provide an option of a csv export. Also, through this command I can get the output on my command prompt but I am not sure how to write it in a file.

like image 483
Vibhor Nigam Avatar asked Oct 27 '15 00:10

Vibhor Nigam


People also ask

How do I export a whole DynamoDB table?

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.

How do I export a DynamoDB table schema?

Hover your mouse over Export data model. In the dropdown list, choose whether to export your data model in NoSQL Workbench model format or CloudFormation JSON template format. Choose a location to save your model.


2 Answers

If all items have the same attributes, e.g. id and name both of which are strings, then run:

aws dynamodb scan \     --table-name mytable \     --query "Items[*].[id.S,name.S]" \     --output text 

That would give tab-separated output. You can redirect this to file using > output.txt, and you could then easily convert tabs into commas for csv.

Note that you may need to paginate per the scan documentation:

If the total number of scanned items exceeds the maximum dataset size limit of 1 MB, the scan stops and results are returned to the user as a LastEvaluatedKey value to continue the scan in a subsequent operation. The results also include the number of items exceeding the limit. A scan can result in no table data meeting the filter criteria.

Another option is the DynamoDBtoCSV project at github.

like image 149
jarmod Avatar answered Sep 21 '22 17:09

jarmod


For localhost dynamodb:

$aws dynamodb scan --table-name AOP --region us-east-1 --endpoint-url http://localhost:8000 --output json > /home/ohelig/Desktop/a.json 

For dynamodb:

$aws dynamodb scan --table-name AOP --region us-east-1 --output json > /home/ohelig/Desktop/a.json 

Then Convert JSON to CSV or whatever.

I have modified above answer to make it clear.

like image 21
Yanish Pradhananga Avatar answered Sep 17 '22 17:09

Yanish Pradhananga