插入不插入相同主键后删除Cassandra批处理语句

问题描述 投票:1回答:2

这是我的方案,考虑我的表有cloumns id主键和clusterkey群集键。我想删除一个属于主键的记录,并插入具有相同主键但不同群集密钥的新记录。在这种情况下,由于某些并发性,它似乎一直没有发生插入?尝试使用时间戳,但同样的问题。

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')
});

使用时间戳

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')
});

表信息:

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

查询结果:

批量前选择结果

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

批量后

    id  |clusterkey |name 
----------------------------- 
    2   |2       |Hello 
node.js cassandra cassandra-3.0 cassandra-driver
2个回答
0
投票

您将这些查询添加到批处理中,但可能忘记执行这些查询。

尝试使用,

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

请点击此链接了解更多详情:Cassandra executing a query


0
投票

除了@ruhul的答案之外,如果你还没有执行批处理,那么删除也应该不起作用。但你提到的只是插入。

如果不是@ruhul提到的案例,那么检查你的集群是否一致。当集群失去一致性时会发生这种情况。

在群集上运行修复(nodetool repair)并再次检查。

© www.soinside.com 2019 - 2024. All rights reserved.