cassandra可以用于点赞/观看次数 [英] Can cassandra be used for likes / view counts

查看:75
本文介绍了cassandra可以用于点赞/观看次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您存储的数据包含喜欢或观看次数类似的数据时,cassandra可以使用吗?如我所见,问题可能是:

Would cassandra be usable when you are storing data that contains something similar to likes or view counts? As I see it the issues would be:

1)不断更新行(墓碑)

1) Constant updates to rows (tombstones)

2)使用计数器或LWT CAS操作会增加延迟,并且很复杂。如果更新视图计数是比较麻烦的,因为这将是最频繁的操作。

2) Using a counter or LWT CAS operations would increase latency and be complicated. Which is troublesome if updating view counts since that would be the most frequent operation.

使用计数器将是对计数器表的写操作,而读取的记录为100条将需要100次读取或在计数器表上进行选择。

Using a counter would be a write to the counter 'table' while a read of 100 records would require 100 reads or a select in on the counter table.

使用LWT将是读取该行并进行比较和设置,直到更新成功为止。读取100条记录将是正常的

Using LWT would be a read of the row and a compare and set until your update sticks. Reads of 100 records would be as normal

推荐答案

为此类作业创建计数器。


计数器是一个特殊的列,用于存储递增更改的数字。例如,您可以使用一个计数器列来计算页面被浏览的次数。

Apache Cassandra™2.1计数器列改进了计数器的实现并提供了许多配置选项来调整计数器

A counter is a special column used to store a number that is changed in increments. For example, you might use a counter column to count the number of times a page is viewed.
Apache Cassandra™ 2.1 counter column improves the implementation of counters and provides a number of configuration options to tune counters

您可以创建如下所示的计数器表:

You can create a counter table like below :

CREATE TABLE video_counter (
    video_id uuid PRIMARY KEY,
    like_count counter,
    view_count counter
);

每当收到喜欢或观看视频的请求时,请增加计数。

Whenever a like or view request received for a video, increment the count.

//Like
UPDATE video_counter SET like_count = like_count + 1 WHERE video_id = ? ;

//View
UPDATE video_counter SET view_count = view_count + 1 WHERE video_id = ? ;

现在您可以得到喜欢的图片并非常有效地观看视频计数

Now you can get the like and view count of a video very efficiently

SELECT * FROM video_counter WHERE video_id = ? ;

来源: http://docs.datastax.com/en/cql/3.1/cql/cql_using/use_counter_t.html

这篇关于cassandra可以用于点赞/观看次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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