问题
I want to create a table with wide partitions (or, put another way, a table which has no value columns (non primary key columns)) that enables the number of rows in any of its partitions to be efficiently procured. Here is a simple definition of such a table
CREATE TABLE IF NOT EXISTS test_table
(
partitionKeyCol timestamp
clusteringCol timeuuid
partitionRowCountCol counter static
PRIMARY KEY (partitionKeyCol, clusteringCol)
)
The problem with this definition, and others structured like it, is that their validity cannot be clearly deduced from the information contained in the docs.
What the docs do state (with regards to counters):
A counter column can neither be specified as part of a table's
PRIMARY KEY
, nor used to create anINDEX
A counter column can only be defined in a dedicated counter table (which I take to be a table which solely has counter columns defined as its value columns)
What the docs do not state (with regards to counters):
The ability of a table to have a static counter column defined for it (given the unique write path of counters, I feel that this is worth mentioning)
The ability of a table, which has zero value columns defined for it (making it a dedicated counter table, given my understanding of the term), to also have a static counter column defined for it
Given the information on this subject that is present in (and absent from) the docs, such a definition appears to be valid. However, I'm not sure how that is possible, given that the updates to partitionRowCountCol
would require use of a write path different from that used to insert (partitionKeyCol, clusteringCol)
tuples.
Is this type of counter table definition valid? If so, how are writes to the table carried out?
回答1:
It looks like a table with this structure can be defined, but I'm struggling to find a good use case for it. It seems there is no way to actually write to that clustering column.
CREATE TABLE test.test_table (
a timestamp,
b timeuuid,
c counter static,
PRIMARY KEY (a, b)
);
cassandra@cqlsh:test> insert into test_table (a,b,c) VALUES (unixtimestampof(now()), now(), 3);
InvalidRequest: code=2200 [Invalid query] message="INSERT statements are not allowed on counter tables, use UPDATE instead"
cassandra@cqlsh:test> update test_table set c = c + 1 where a=unixtimestampof(now());
cassandra@cqlsh:test> update test_table set c = c + 1 where a=unixtimestampof(now());
cassandra@cqlsh:test> select * from test_table;
a | b | c
--------------------------+------+---
2016-03-24 15:04:31+0000 | null | 1
2016-03-24 15:04:37+0000 | null | 1
(2 rows)
cassandra@cqlsh:test> update test_table set c = c + 1 where a=unixtimestampof(now()) and b=now();
InvalidRequest: code=2200 [Invalid query] message="Invalid restrictions on clustering columns since the UPDATE statement modifies only static columns"
cassandra@cqlsh:test> insert into test_table (a,b) VALUES (unixtimestampof(now()), now());
InvalidRequest: code=2200 [Invalid query] message="INSERT statements are not allowed on counter tables, use UPDATE instead"
cassandra@cqlsh:test> update test_table set b = now(), c = c + 1 where a=unixtimestampof(now());
InvalidRequest: code=2200 [Invalid query] message="PRIMARY KEY part b found in SET part"
What is it you're trying to model?
来源:https://stackoverflow.com/questions/36181280/is-this-type-of-counter-table-definition-valid