文档基础

  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

Cassandra 计数器counter类型和它的限制的更多相关文章

  1. 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)

    Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuq ...

  2. 028_MapReduce中的计数器Counter的使用

    一.分析运行wordcount程序屏幕上打印信息 ##运行wordcount单词频率统计程序,基于输出输出路径. [hadoop@hadoop-master hadoop-1.2.1]$ hadoop ...

  3. ComputeShader中Counter类型的使用

    接上一篇:https://www.cnblogs.com/hont/p/10122129.html 除了Append类型对应的Consume/AppendStructuredBuffer还有一个Cou ...

  4. JMeter 配置元件之计数器Counter

    配置元件之计数器Counter   by:授客 QQ:1033553122 测试环境 apache-jmeter-2.13 1.   计数器简介 允许用户创建一个在线程组范围之内都可以被引用的计数器. ...

  5. CSS计数器:counter

    最近的需求,明星字体销售排行榜中,需要对字体的销售情况进行排序. 在早期,只有ol和ul可以对子元素li进行排序:如果不使用这两个标签,就由前台开发去手动填写序号. 当然,在这个需求中,数据不是实时更 ...

  6. Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)

    Counter(计数器) 是一个字典的子类,存储形式同样为字典,其中存储的键为字典的元素,值为元素出现的次数,在使用之前我们需要先导入文件 import collections 初始化一个计数器 im ...

  7. 计数器counter

    今天就讲了2个属性:1.计数器 2.列规则 列规则很简单:column-count:3; (列的具体个数) column-width:30px;(列宽)N个浏览器不兼容column-gap:10px; ...

  8. Jmeter -----计数器(counter)

    计数器的定义 Allows the user to create a counter that can be referenced anywhere in the Thread Group. The ...

  9. jmeter 配置元件之计数器Counter

    用jmeter生成数据 我用过几种以下几种方法 1.CSV Data Set Config  参数化 2.${_Random} ${_Random}是jmeter函数助手里面自带的一个函数,作用是返回 ...

随机推荐

  1. [Flex] ButtonBar系列——labelFunction用户提供的函数,在每个项目上运行以确定其标签

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  2. (medium)LeetCode 221.Maximal Square

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...

  3. 通过反射获取父类中的泛型参数对应的Class对象

    假设有两个类:Dao 和 PersonDao,它们的代码如下: Dao: public class Dao<T> { private Class<T> clazz; T get ...

  4. HDU 4292 Food

    Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. Eclipse is running in a JRE, but a JDK is required 解决方法

    本文非原创,转自http://liguoliang.com/2010/eclipse-is-running-in-a-jre-but-a-jdk-is-required/ 安装Maven后每次启动出现 ...

  6. strtol函数

    今天做啦一个进制转换的题,改来改去最终倒是过啦,本来挺开心的,然后去翻啦一下题解,瞬间就有小情绪啦,哎,人家的代码辣么辣么短,实在是不开心,不过谁让咱是小渣渣呢,在此总结一下strtol 函数. 先来 ...

  7. windows server 2008 IP安全策略关闭端口,禁止ping,修改远程连接3389端口,开放指定端口

    windows server 2008 IP安全策略关闭端口:      Tomcat服务访问不了情况解决.    Windows默认情况下有很多端口是开放的,在你上网的时候,网络病毒和黑客可以通过这 ...

  8. iOS指纹识别代码

    1:添加LocalAuthentication.framework框架 2:实现过程 #import "ViewController.h" #import <LocalAut ...

  9. Sublime Text 2/3中Autoprefixer失效解决方法

    ###Sublime Text 2/3中Autoprefixer失效解决方法: 相信每个前端er都会使用Subl这款工具吧,因为它有上千款开源的插件,而且功能各异,这里给大家带来的是标题中Autopr ...

  10. 多线程——GCD

    一. GCD的基本概念 GCD:强大的中枢调度,纯C语言,提供了非常多强大的函数. 任务(block):执行什么操作. 队列(queue):用来存放任务. 同步函数dispatch_sync():不创 ...