6

I want to use a Cassandra counters in CQL3 like this

create table count1 (id int PRIMARY KEY , c1 counter );

I know that in order to update the counter I need to do something of the form:

update count1 set c1 = c1+1 where ..

but before this I need to insert a value in my key (id); but I get:

cqlsh:test2> insert into count1 (id) values (4);
Bad Request: INSERT statement are not allowed on counter tables, use UPDATE instead

what gives ?

4

1 に答える 1

8

Cassandra には、主キーが存在するかどうかという概念はありません。特定の主キーで存在するのはセルのみです。

したがって、通常の列ファミリー (つまり、非カウンター) の場合、その主キーで何かが以前に挿入されたかどうかに関係なく、insertまたは(意味的に同一です) ことができます。update

カウンター テーブルに関しては、CQL では、値を設定するのではなく、インクリメントしていることを明確にするために、updateではなくを使用する必要があります。insertCassandra でカウンター値を設定することはできません。inc/dec のみです。以前のカウンターがなかった場合、値が 0 であると見なされます。したがって、実行できます

update count1 set c1 = c1 + 1 where id = 2;

カウンターの値は 1 になります。

select * from count1;

 id | c1
----+----
  2 |  1
于 2013-07-18T08:43:00.500 に答える