Search code examples
node.jscassandradatastaxdatastax-enterprisedatastax-node-driver

Cassandra nodejs eachRow return before db query completed


I'm using Cassandra with nodejs to fetch big table with eachRow.

I need to insert data on each row, but for some reason it's not waiting for the query and it finish before it's done.

client.eachRow(query, [], { prepare: true, autoPage : true, fetchSize: 500 }, function(index, row) {
     // DB query / insert or update
, function(err, result) {
    // Finish all rows.
});

Any suggestions?


Solution

  • simply go through official cassandra-driver doc https://www.npmjs.com/package/cassandra-driver and figure out this line

    The #stream() method works in the same way but instead of callback it returns a Readable Streams2 object in objectMode that emits instances of Row.

    client.stream(query, [ 'abc' ])
      .on('readable', function () {
        // 'readable' is emitted as soon a row is received and parsed
        var row;
        while (row = this.read()) {
          console.log('time %s and value %s', row.time, row.val);
        }
      })
      .on('end', function () {
        // Stream ended, there aren't any more rows
      })
      .on('error', function (err) {
        // Something went wrong: err is a response error from Cassandra
      });