sangheestyle
sangheestyle

Reputation: 1077

After inserting data into Cassandra with node.js, the program seems like it is still waiting

I am really new to Cassandra and node.js now. I am trying to insert 200 data and it seems they are inserted into DB.

var client = new cassandra.Client({contactPoints: ['127.0.0.1'], keyspace: 'big'});
var insertRepo = 'INSERT INTO big.repo_info (id, name, full_name) '
    + 'VALUES(?, ?, ?);';

for (var i = 0, len = items.length; i < len; i++) {
    console.log(items[i].id, items[i].name, items[i].full_name);
    client.execute(insertRepo,
                   [items[i].id, items[i].name, items[i].full_name],
                   { prepare : true },
                   function (err) {
                     if (err) {
                       console.log(err);
                     } else {
                       console.log('done')
                     }
                   });
  };

However, after this transaction, it seems that the program is waiting for something without really done. So, I just press ctrl-c to exit the program. Is it normal or do I need to do something for this?

I think I missed something.

enter image description here

Upvotes: 0

Views: 854

Answers (1)

jorgebg
jorgebg

Reputation: 6600

Node.js event loop will stay active until open connections are closed.

In this case, it means you need to call client.shutdown().

Also, consider that client.execute() is an async operation, so invoking it from a sync loop it is generally not a good idea. You can use a workflow library instead, like the async library.

async.timesSeries(items.length, function (n, next) {
  var params = [ items[i].id, items[i].name, items[i].full_name];
  client.execute(insertQuery, params, { prepare: true }, next);
}, function allFinished(err) {
  client.shutdown();
});

Check async.timesSeries() method for more information.

Upvotes: 1

Related Questions