【发布时间】:2016-02-06 12:49:16
【问题描述】:
在为具有汇款功能的简单应用程序编写一些概念证明时,我在使用 Cassandra(2.2.3 版)数据库和使用静态列时遇到了奇怪的问题。
我的桌子是:
CREATE TABLE transactions (
profile text,
timestamp timestamp,
amount text,
balance text,
lock int static,
PRIMARY KEY (profile, timestamp)) WITH CLUSTERING ORDER BY (timestamp ASC);
第一步我添加新记录
INSERT INTO transactions (profile, timestamp, amount) VALUES ( 'test_profile', '2015-11-05 15:20:01+0000', '10USD');
然后我想“锁定”当前用户交易以对他的余额进行一些操作。我尝试执行这个请求:
UPDATE transactions SET lock = 1 WHERE profile = 'test_profile' IF lock = null;
但结果是 cqlsh 我看到了
[applied]
-----------
False
我不明白为什么是“假”,因为个人资料的当前数据是:
profile | timestamp | lock | amount | balance
--------------+--------------------------+------+--------+---------
test_profile | 2015-11-05 15:20:01+0000 | null | 10USD | null
知道我做错了什么吗?
更新
阅读 Nenad Bozic 的回答后,我修改了我的示例以阐明为什么我需要更新条件。完整代码示例
CREATE TABLE transactions (
profile text,
timestamp timestamp,
amount text,
balance text,
lock int static,
balances map<text,text> static,
PRIMARY KEY (profile, timestamp)
) WITH CLUSTERING ORDER BY (timestamp ASC);
INSERT INTO transactions (profile, timestamp, amount) VALUES ( 'test_profile', '2015-11-05 15:20:01+0000', '1USD');
INSERT INTO transactions (profile, lock) VALUES ('test_profile', 1) IF NOT EXISTS;
BEGIN BATCH
UPDATE transactions SET balances={'USD':'1USD'} WHERE profile='test_profile';
UPDATE transactions SET balance='1USD' WHERE profile='test_profile' AND timestamp='2015-11-05 15:20:01+0000';
DELETE lock FROM transactions WHERE profile='test_profile';
APPLY BATCH;
如果我再次尝试获取锁,我会得到
INSERT INTO transactions (profile, lock) VALUES ('test_profile', 1) IF NOT EXISTS;
[applied] | profile | timestamp | balances | lock | amount | balance
-----------+--------------+-----------+-----------------+------+--------+---------
False | test_profile | null | {'USD': '1USD'} | null | null | null
【问题讨论】:
-
我在
cassandra 3.0.0-rc2中测试并且它可以工作...但在其他版本中不起作用...
标签: cassandra cassandra-2.0 cql3 cqlsh nosql