如何增加Cassandra中的计数器? [英] How to increment a counter in Cassandra?

查看:350
本文介绍了如何增加Cassandra中的计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Cassandra存储计数器。例如,某个网页的浏览次数。
计数器不会递减。计数器的值不需要是精确的,但它应该是准确的。



我的第一想法是将值存储为列,只是读取当前计数,增加一,然后把它放回来。但是如果另一个操作也试图增加计数器,我认为最终的值将只是一个具有最新的时间戳。



另一种想法是将每个页面加载存储为CF中的新列。然后我可以在该键上运行 get_count()并获取列数。通过阅读文档,似乎这不是一个非常有效的操作。

解决方案

计数器已添加到Cassandra 0.8



使用incr方法将列的值增加1。

  [default @ app] incr counterCF [ascii('a')] [ascii('x')]; 
增加的值。
[default @ app] incr counterCF [ascii('a')] [ascii('x')];
增加的值。

请在这里描述:



或者可以通过编程方式完成

  CounterColumn counter = new CounterColumn(); 
ColumnParent cp = new ColumnParent(page_counts_by_minute);
counter.setName(ByteBufferUtil.bytes(bucketByMinute.format(r.date))));
counter.setValue(1);
c.add(ByteBufferUtil.bytes(bucketByDay.format(r.date)+ - + r.url)
,cp,counter,ConsistencyLevel.ONE);

此处描述: http://www.jointhegrid.com/highperfcassandra/?cat=7


I'd like to use Cassandra to store a counter. For example how many times a given page has been viewed. The counter will never decrement. The value of the counter does not need to be exact but it should be accurate over time.

My first thought was to store the value as a column and just read the current count, increment it by one and then put it back in. However if another operation is also trying to increment the counter, I think the final value would just be the one with the latest timestamp.

Another thought would be to store each page load as a new column in a CF. Then I could just run get_count() on that key and get the number of columns. Reading through the documentation, it appears that it is not a very efficient operation at all.

Am I approaching the problem incorrectly?

解决方案

Counters have been added to Cassandra 0.8

Use the incr method increment the value of a column by 1.

[default@app] incr counterCF [ascii('a')][ascii('x')];
Value incremented.
[default@app] incr counterCF [ascii('a')][ascii('x')];
Value incremented.

Describe here: http://www.jointhegrid.com/highperfcassandra/?p=79

Or it can be done programatically

CounterColumn counter = new CounterColumn();
ColumnParent cp = new ColumnParent("page_counts_by_minute");
counter.setName(ByteBufferUtil.bytes(bucketByMinute.format(r.date)));
counter.setValue(1);
c.add(ByteBufferUtil.bytes( bucketByDay.format(r.date)+"-"+r.url)
            , cp, counter, ConsistencyLevel.ONE);

Described here: http://www.jointhegrid.com/highperfcassandra/?cat=7

这篇关于如何增加Cassandra中的计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆