Cassandra 计数器counter类型和它的限制

文档基础

  1. Cassandra 2.*
  2. CQL3.1 翻译多数来自这个文档
    更新于2015年9月7日,最后有参考资料

作为Cassandra的一种类型之一,Counter类型算是限制最多的一个。Counter就是计数器,在CQL中可以定义一个Counter类型。Cassandra和mysql不同,普通的int,bigint类型是不能执行类似于:

/* 在CQL中,如果view不是counter类型,这样的语句是要失败的 */
update user set view = view + 1 where uid = 123456;

这种类型的操作只能在view为Counter类型的情况下执行。

文档解析

counter是一种特殊的存储自增数字的字段。
比如你可能希望使用counter计算页面访问的次数。

Cassandra 2.1 的counter 字段改进了实现方式,提供了许多选项来配置counters。在Cassandra2.1和更新的版本中,你可以配置代理服务器在counter写入的时候等待多久,counter在内存中缓存的体积,在Cassandra保存缓存key前等待多久,保存多少个key,concurrent_count_writes。你可以在cassandra.yaml中配置。

在Cassandra 2.0.* 中使用的replicate_on_write表在2.1版本后移除了。

定义一个counter专用表(后面会解析)并且使用counter类型。你不能在counter列上面使用索引,不能delete,不能反复添加一个counter字段。

基本操作:

    CREATE TABLE counterks.page_view_counts
    (
        counter_value counter,
        url_name varchar,
        page_name varchar,
        PRIMARY KEY (url_name, page_name)
    );

    UPDATE counterks.page_view_counts
    SET counter_value = counter_value + 1
    WHERE url_name='www.datastax.com' AND page_name='home';

限制:

  1. 包含有counter字段的表,非counter字段必须在主键中,所以一个counter表就只能用来做计数器,基本不能做别的事情。
  2. counter字段不能set counter_value = 10,你可以set counter_value = counter_value + 10
  3. counter数据一旦建立,不要进行任何形式的删除操作,否则操作结果不可以预期,目前没有找到恢复的方法,除了truncate table。或者备份还原。另外服务器在清理墓碑(cassandra用墓碑标记删除数据)以后,可以继续正常使用。
    • Cassandra rejects USING TIMESTAMP or USING TTL in the command to update a counter column,and now generates an error message when you attempt such an operation.
    • 因为设置ttl(过期时间,会在数据都过期后产生类似删除的行为,所以无法给计数器设置超时时间。
  4. 如果写入失败,客户端是不会知道的,比如超时,如果重新发送请求,有可能导致一次额外计数。
  5. 如果你想重置计数器,一个可选的办法是:读取计数器的值,然后加上这个值的负数值。
  6. insert语句也是会失败的,你可以update set counter_value = counter_value + 0 where *= 来初始化一个counter表。

未翻译内容

基本上就是上面的2,3,4,5的限制。

Technical limitations

If a write fails unexpectedly (timeout or loss of connection to the coordinator node) the client will not know if the operation has been performed. A retry can result in an over count CASSANDRA-2495.
Counter removal is intrinsically limited. For instance, if you issue very quickly the sequence "increment, remove, increment" it is possible for the removal to be lost (if for some reason the remove happens to be the last received messages). Hence, removal of counters is provided for definitive removal only, that is when the deleted counter is not increment afterwards. This holds for row deletion too: if you delete a row of counters, incrementing any counter in that row (that existed before the deletion) will result in an undetermined behavior. Note that if you need to reset a counter, one option (that is unfortunately not concurrent safe) could be to read its value and add -value.
CounterColumnType may only be set in the default_validation_class. A column family either contains only counters, or no counters at all.

参考资料

http://wiki.apache.org/cassandra/Counters
http://docs.datastax.com/en/cql/3.1/cql/cql_using/use_counter_t.htm

posted @ 2015-09-07 17:23  wang#  阅读(3643)  评论(0编辑  收藏  举报