Coder
Coder

Reputation: 3262

Cassandra with nodejs client is not working as expected

I have a node setup like below, DC1 (Node1, Node2) and DC2 (Node3 and Node4)

I am trying to connect to Cassandra via NodeJs Client like below,

const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['172.30.56.60','172.30.56.61','172.30.56.62','172.30.56.63'], keyspace: 'users' });

console.log(client);

const query = 'INSERT INTO user (user_id, user_name, user_phone) VALUES (?, ?, ?) IF NOT EXISTS';

console.log(query);
console.time('time');
for (var primaryKey = 0; primaryKey < 10000; primaryKey++) {
    const params = [primaryKey, 'hari', 12345678];
    console.log(primaryKey);
    client.execute(query, params, { prepare: true, consistency: cassandra.types.consistencies.quorum });
    console.log(primaryKey);
}
console.timeEnd('time');

I am trying to track the total time taken for inserting all the 10000 entries, But I observe that, It is updating the entries asynchronously (i.e), I get the execution time as '3.755 ms' but when I check in cqlsh shell it is updating slowly (using select count(*) from user;), NOTE : I have given the consistency as 'cassandra.types.consistencies.quorum'

Cassandra : 3.0

What is the change I need to make execute them synchronously?

Thanks, Harry

Upvotes: 0

Views: 595

Answers (1)

Jyotman Singh
Jyotman Singh

Reputation: 11330

Right now you're executing all your queries concurrently as you're not waiting for any query to finish.

The client.execute method returns a promise, so to execute each query synchronously you could wait for each promise to complete and then make the next query. You could do this with async/await -

async function insertBulk() {
    for (var primaryKey = 0; primaryKey < 10000; primaryKey++) {
        const params = [primaryKey, 'hari', 12345678];
        await client.execute(query, params, { prepare: true, consistency: cassandra.types.consistencies.quorum });
    }
}

async function getTime() {
    console.time('time');
    await insertBulk(); // wait till all the queries are executed sequentially.
    console.timeEnd('time');
}

getTime();

Also if your use case is to insert many documents then you could use the Batch query. Read about it here.

Upvotes: 2

Related Questions