Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (5.6k points)

 I want to import a CSV file into DynamoDB, how can I achieve this?

Here's my CSV file:

first_name  last_name

sri ram

Rahul   Dravid

JetPay  Underwriter

Anil Kumar  Gurram

1 Answer

0 votes
by (12.4k points)

DynamoDB allows only writing up to 25 records at a time in "batchinsert", so you can do it like: 

 var fs = require('fs');

    var parse = require('csv-parse');

    var async = require('async');

    var csv_filename = "YOUR_CSV_FILENAME_WITH_ABSOLUTE_PATH";

    rs = fs.createReadStream(csv_filename);

    parser = parse({

        columns : true,

        delimiter : ','

    }, function(err, data) {

        var split_arrays = [], size = 25;

        while (data.length > 0) {

            split_arrays.push(data.splice(0, size));

        }

        data_imported = false;

        chunk_no = 1;

        async.each(split_arrays, function(item_data, callback) {

            ddb.batchWriteItem({

                "TABLE_NAME" : item_data

            }, {}, function(err, res, cap) {

                console.log('done going next');

                if (err == null) {

                    console.log('Success chunk #' + chunk_no);

                    data_imported = true;

                } else {

                    console.log(err);

                    console.log('Fail chunk #' + chunk_no);

                    data_imported = false;

                }

                chunk_no++;

                callback();

            });

        }, function() {

            // run after loops

            console.log('all data imported....');

        });

    });

    rs.pipe(parser);

Do Check out the AWS Certification Course offered by Intellipaat. 

Related questions

0 votes
1 answer

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 29, 2020 in AWS by devin (5.6k points)

Browse Categories

...