Cassandra cql comparator type counter

删除回忆录丶 提交于 2019-12-02 15:10:17

问题


i want to use the following code for updating a field...

@@db.execute("UPDATE user_count SET counters = counters + #{val} WHERE cid = 1 ")

First time i tried that i got the following fail:
CassandraCQL::Error::InvalidRequestException: invalid operation for non commutative columnfamily user_count
I found out that i have to use the comparator counter, but i cant find how i can setup that with the cassandra-cql gem... does anybody know how i can get this to work? below there is my code that does not work ...

@@db.execute("CREATE COLUMNFAMILY user_count(cid varchar PRIMARY KEY, counters counter) with comparator = counter " )
@@db.execute("INSERT INTO user_count (cid, counters) VALUES (?,?)", 1, 0)

回答1:


You need to set default_validation=CounterColumnType instead of comparator.

@@db.execute("CREATE COLUMNFAMILY user_count(cid varchar PRIMARY KEY, counters counter) with default_validation=CounterColumnType")
@@db.execute("update user_count set counters = counters + 1 where cid = 1")

You must use 'update' to change the counter value, there is no insert syntax for counters (in CQL update and insert do the same thing so you can create new rows using update).

Currently you cannot have counters and non-counters in the same column family (from the wiki: "A column family either contains only counters, or no counters at all.")



来源:https://stackoverflow.com/questions/8813211/cassandra-cql-comparator-type-counter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!