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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM