1

Here is my scenario, consider my table has cloumns id primary key and clusterkey cluster key. I want to delete one record falling under primary key and insert new record with same primary key but different cluster key. In this case, all the time it looks like insert is not happening due to some concurrency? Tried using Timestamp, but same issue.

const query1 = 'delete from table where id=? and clusterkey=?';
const query2 = 'INSERT INTO table (id, clusterkey, name) VALUES (?, ?, ?)';
const queries = [
   { query: query1, params: ['1', '3'] },
   { query: query2, params: ['1', '8', 'GoodName'] } 
];
client.batch(queries, { prepare: true }, function (err) {
   console.log(err, 'done')
});

Using Timestamp

const query1 = 'delete from table where id=? and clusterkey=? using timestamp ?';
const query2 = 'INSERT INTO table (id, clusterkey, name) VALUES (?, ?, ?) using timestamp ?';
const queries = [
   { query: query1, params: ['1', '3', Date.now() * 1000] },
   { query: query2, params: ['1', '8', 'GoodName', Date.now() * 1000] } 
];
client.batch(queries, { prepare: true }, function (err) {
   console.log(err, 'done')
});

Table Info:

create table sample (id text, group text, name text, PRIMARY KEY (id, group))

Query Results:

select result before batch

    id  |clusterkey |name 
----------------------------- 
    1   |3       |wrongname 
    2   |2       |Hello

After batch

    id  |clusterkey |name 
----------------------------- 
    2   |2       |Hello 
Pasupathi Rajamanickam
  • 1,982
  • 1
  • 24
  • 48

2 Answers2

0

You added those queries into a batch but possibly forget to execute these queries.

try using,

client.execute( queries, function (err, result) { 
     if (!err) {
         console.log("executed.");
     }
} );

Follow this links for more details: Cassandra executing a query

MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37
  • 1
    I'm not sure who is wrong here. http://datastax.github.io/nodejs-driver/features/batch/ says just `client.batch` will execute all. The one you mentioned to execute single query. – Pasupathi Rajamanickam Mar 04 '18 at 17:28
0

In addition to the answer of @ruhul, if you have not executed batch then the delete also should not work. But you mentioned about insert only.

If not the mentioned case by @ruhul, then check if your cluster is consistent. It happens when cluster loses its consistency.

Run repair on your cluster (nodetool repair) and check again.

Chaity
  • 1,348
  • 13
  • 20
  • I'm not sure who is wrong here. datastax.github.io/nodejs-driver/features/batch says just `client.batch` will execute all. The one you mentioned to execute single query. Ans yes, the above my code executed delete successfully. – Pasupathi Rajamanickam Mar 04 '18 at 17:29