Cassandra 卡桑德拉数据建模设计
我对卡桑德拉还很陌生,上个月我读了很多书。Cassandra 卡桑德拉数据建模设计,cassandra,Cassandra,我对卡桑德拉还很陌生,上个月我读了很多书。 我正在研究一个小用例。 查询:根据时间范围内播放的数量排名前X的玩家 因此,在任何给定的时间范围内,我都希望聚集玩家总数,并获得前X名玩家 我采用了创建UDF(使用C*-2.2.0版本)的方法来聚合玩家玩的游戏数量 下面是我为这个用例设计的时间序列数据模型 CREATE COLUMNFAMILY PlayerRating ( PlayerNumber int, ===> Unique account number GameID text, ===
我正在研究一个小用例。
查询:根据时间范围内播放的数量排名前X的玩家 因此,在任何给定的时间范围内,我都希望聚集玩家总数,并获得前X名玩家 我采用了创建UDF(使用C*-2.2.0版本)的方法来聚合玩家玩的游戏数量 下面是我为这个用例设计的时间序列数据模型
CREATE COLUMNFAMILY PlayerRating
(
PlayerNumber int, ===> Unique account number
GameID text, ===> unique machine ID per slot
AmountPlayed double, ===> AmountPlayed by the player
EventTime timestamp, ===> Event generated TimeStamp
PRIMARY KEY ((PlayerNumber, GameID),EventTime)) WITH CLUSTERING ORDER BY(EventTime desc);
请让我知道我的数据模型设计是否适合我的查询
谢谢 我认为将每场游戏的所有玩家放在一个分区中可能更容易 这样,您可以通过一个查询聚合所有玩家,而不是为每个玩家单独查询。然后,您可以将每个玩家的游戏时间聚合到一张地图中(请参见如何为此定义UDF的示例) 所以你的桌子看起来像这样:
CREATE TABLE playing_time_by_game (game_id text, event_time int, player_id text, amount_played int, PRIMARY KEY (game_id, event_time));
然后创建UDF以按玩家id总计:
CREATE FUNCTION state_group_and_total( state map<text, int>, type text, amount int )
CALLED ON NULL INPUT
RETURNS map<text, int>
LANGUAGE java AS '
Integer count = (Integer) state.get(type); if (count == null) count = amount; else count = count + amount; state.put(type, count); return state; ' ;
SELECT group_and_total(player_id, amount_played) from playing_time_by_game;
t2.group_and_total(player_id, amount_played)
----------------------------------------------
{'player1': 258, 'player2': 13}
现在您可以按玩家id进行聚合:
CREATE FUNCTION state_group_and_total( state map<text, int>, type text, amount int )
CALLED ON NULL INPUT
RETURNS map<text, int>
LANGUAGE java AS '
Integer count = (Integer) state.get(type); if (count == null) count = amount; else count = count + amount; state.put(type, count); return state; ' ;
SELECT group_and_total(player_id, amount_played) from playing_time_by_game;
t2.group_and_total(player_id, amount_played)
----------------------------------------------
{'player1': 258, 'player2': 13}
您可以将查询限制为游戏分区和时间范围:
SELECT group_and_total(player_id, amount_played) from playing_time_by_game where game_id='game1' and event_time >=0 and event_time <=7;
t2.group_and_total(player_id, amount_played)
----------------------------------------------
{'player1': 8, 'player2': 13}
从按游戏播放时间中选择组和总数(玩家id、玩的数量),其中游戏id='game1'和事件时间>=0和事件时间