如何使用CQL更新Cassandra整数列? [英] How do you update a Cassandra integer column using CQL?
问题描述
我在Cassandra中有一个表格评论",如下所述:
I have a table 'reviews' in Cassandra as mentioned below:
CREATE TABLE reviews (
review_id text,
downcount int,
upcount int,
creation_date timestamp,
PRIMARY KEY (review_id)
)
让我们说我想将 upcount
值增加1,如何在Cassandra CQL中做到这一点?
Lets say I want to increase the upcount
value by 1, how can I do that in Cassandra CQL?
当我这样做时:
UPDATE reviews SET upcount = upcount +1 WHERE review_id = '123456';
它给了我以下错误:
错误请求:对非计数器列
upcount
无效的操作(upcount = upcount + 1
).
推荐答案
您可以使用Cassandra的计数器列数据类型.请记住,如果非计数器列不属于复合 PRIMARY KEY
的一部分,则不能将计数器列与同一列族中的非计数器列混合使用.因此,您必须将计数器分成另一个列族,例如 reviews_counts
.
You can use Cassandra's counter columns data type. Keep in mind you cannot mix counter columns with non-counter columns in the same column family if the non-counter columns are not part of the composite PRIMARY KEY
. So you have to separate the counters into another column family, say reviews_counts
.
CREATE TABLE reviews (
review_id text,
creation_date timestamp,
PRIMARY KEY (review_id)
)
CREATE TABLE reviews_counts (
review_id text,
downcount counter,
upcount counter,
PRIMARY KEY (review_id)
)
现在增量语句应该可以工作了.
Now the increment statement should work.
UPDATE keyspace.reviews_counts
SET upcount = upcount + 1
WHERE review_id = '123456'
这是有关Cassandra计数器列的更多文档. http://www.datastax.com/documentation/cql/3.0/cql/cql_using/use_counter_t.html
Here is some more documentation about Cassandra counter columns. http://www.datastax.com/documentation/cql/3.0/cql/cql_using/use_counter_t.html
这篇关于如何使用CQL更新Cassandra整数列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!