如何在cassandra做轮询? [英] How to do polling in cassandra?
问题描述
我试图找到一种方法来查询一个cassandra数据库,但我是新的,我不知道如何。
I'm trying to find a way to do polling over a cassandra database, but I'm new at this and I don't know how.
说我有一个表用户像这样
Lets say I have a table "users" like this
-> users
-> user_name
-> gender
-> state
,我想不断轮询,以便知道是否有新用户添加到表。
and I want to do polling constantly so I know if a new user was added to the table. How can I do that?
推荐答案
关系数据库中的标准方法将涉及执行SELECT, ID降序,所以最新的行总是首先返回,所以你可以看到是否匹配你的最后一个最新行并识别更改 - 在cassandra,这将不工作,因为没有WHERE子句,结果是有序的通过分区的令牌,这是(几乎肯定)随机。
The standard approach in a relational DB would involve doing a SELECT, ordering by some time-related ID descending, so that the newest row would always be returned first, so you could see if that matched your last 'newest row' and identify change - in cassandra, that won't work, because without a WHERE clause, the results are ordered by the partition's token, which is (almost certainly) random.
然后,解决方案是创建一个表有一个分区,划分。例如:
The solution, then, is to create a table that has a partition, where users are sorted within a given partition. For example:
CREATE TABLE user_buckets (
bucket text,
user_timestamp timeuuid,
user_username text,
PRIMARY KEY(bucket, user_timestamp)
) WITH CLUSTERING ORDER BY (user_timestamp DESC);
在这种情况下,你将写入users表和user_buckets表,是合理的(例如日期(YYYY) - 其中每个分区包含在该年中注册的所有用户,或日期(YYYYMMDD) - 其中每个分区包含在那天注册的所有用户),然后使用SELECT .. 。FROM user_buckets WHERE bucket =(current-bucket)AND user_timestamp>(您看到的最后一个时间戳)。
In this case, you would write into both the users table and the user_buckets table, with 'bucket' being something reasonable (such as date(YYYY) - where each partition contains all of the users registering in that year, or date(YYYYMMDD) - where each partition contains all of the users registering in that day), and then use SELECT ... FROM user_buckets WHERE bucket=(current-bucket) AND user_timestamp > (last timestamp you've seen).
这篇关于如何在cassandra做轮询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!