Naman Gupta
Naman Gupta

Reputation: 21

Cassandra read in loop

I am facing some difficulty in deciding how to implement a read operation using cassandra.

The case is that I have an array of id's, let's call it idArray. After making the read, I am pushing the result in a result array (resultArray)

Now my problem being that would such a code be efficient at all ?

`for(i;i<idAArray.length;i++)
{   
    let query = "SELECT * FROM table WHERE \"id\"=?idArray[i]" 
    client.execute(query)
    .then(result => resultArray.push(result));
}`

If running in parallel is an option, please specify how exactly ?

Thanks in advance !

Upvotes: 1

Views: 140

Answers (1)

Alex Ott
Alex Ott

Reputation: 87069

If you provide callback to the execute call, then the code will be asynchronous, and you can issue multiple requests in parallel:

client.execute(query, [ 'someone' ], function(err, result) {
  assert.ifError(err);
  console.log('User with email %s', result.rows[0].email);
});

Depending on number of queries that you need to execute, you may need to tune connection pooling to allow more in-fligh requests per connection. It's also recommended to prepare your query.

More information is in documentation.

Upvotes: 1

Related Questions