【问题标题】:Cassandra batch statement delete after insert not inserting for same primay keyCassandra批处理语句删除后插入不插入相同的主键
【发布时间】:2018-08-11 04:41:49
【问题描述】:

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

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 

【问题讨论】:

  • 能否提供第一种情况(id, clusterkey, name)的表结构。
  • create table sample (id text, group text, name text, PRIMARY KEY (id, group))
  • @PasupathiRajamanickam 你能在这个批处理执行后做一个选择并提供结果吗?
  • @dilsingi 更新了我的问题。

标签: node.js cassandra cassandra-3.0 cassandra-driver


【解决方案1】:

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

尝试使用,

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

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

【讨论】:

【解决方案2】:

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

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

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

【讨论】:

  • 我不确定谁错了。 datastax.github.io/nodejs-driver/features/batch 说只有client.batch 将执行所有。您提到的执行单个查询的那个。是的,上面我的代码执行删除成功。
猜你喜欢
  • 2016-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
相关资源
最近更新 更多