cassandra - 状态随时间变化,列出在时间 A 和时间 B 之间具有状态 x = y 的所有对象

标签 cassandra cql cql3

我正在构建一个数据库来存储对象的状态。这表明例如颜色随时间变化。

我想查询在给定时间内具有特定状态的所有对象,例如所有在某一天的下午 1 点到 2 点之间至少出现过一次绿色的东西。

我的想法是这样的 table :

CREATE TABLE states (
  type text,
  value text,
  name text,
  timestamp timeuuid,
  primary key ((type, value), timestamp, name)
) WITH CLUSTERING ORDER BY (timestamp DESC);

给定一些测试数据:

// A, becomes green, turns red and back to green
insert into states(type, value, name, timestamp) values ('color', 'red', 'A',  minTimeuuid('2016-07-07T12:00:00+0000'));
insert into states(type, value, name, timestamp) values ('color', 'green', 'A',  minTimeuuid('2016-07-07T13:35:00+0000'));
insert into states(type, value, name, timestamp) values ('color', 'red', 'A',  minTimeuuid('2016-07-07T13:42:00+0000'));
insert into states(type, value, name, timestamp) values ('color', 'green', 'A',  minTimeuuid('2016-07-07T13:45:00+0000'));


// B stays red
insert into states(type, value, name, timestamp) values ('color', 'red', 'B',  minTimeuuid('2016-07-07T01:00:00+0000'));


// C stays green
insert into states(type, value, name, timestamp) values ('color', 'green', 'C',  minTimeuuid('2016-07-07T11:27:00+0000'));

// D becomes red
insert into states(type, value, name, timestamp) values ('color', 'green', 'D',  minTimeuuid('2016-07-07T13:00:00+0000'));
insert into states(type, value, name, timestamp) values ('color', 'red', 'D',  minTimeuuid('2016-07-07T13:27:00+0000'));

 type  | value | system.dateof(timestamp) | name
-------+-------+--------------------------+------
 color | green | 2016-07-07 13:45:00+0000 |    A
 color | green | 2016-07-07 13:35:00+0000 |    A
 color | green | 2016-07-07 13:00:00+0000 |    D
 color | green | 2016-07-07 11:27:00+0000 |    C
 color |   red | 2016-07-07 13:42:00+0000 |    A
 color |   red | 2016-07-07 13:27:00+0000 |    D
 color |   red | 2016-07-07 12:00:00+0000 |    A
 color |   red | 2016-07-07 01:00:00+0000 |    B

我想要得到的是 A、C、D 而不是 B,因为它在时间范围内不是绿色的。

还有一个简单的查询:

select name from states where type = 'color' and value = 'green' and timestamp >= minTimeuuid('2016-07-07T13:00:00+0000') and timestamp < minTimeuuid('2016-07-07T14:00:00+0000');

我得到A,A,D因此。我不能在这里使用不同的,因为 "SELECT DISTINCT queries must only request partition key columns and/or static columns (not name)"但我可以接受重复项,因为它们在应用程序端很容易处理。

此查询的主要问题是它无法检测到 C,因为颜色在时间范围之前已经是绿色并且在时间范围内没有改变。

更新

我可以随意修改数据库,但是 I cannot specify when the connected devices send updates. They just send data as their state changes and the middleware has to be stateless时间范围是用户在查询时定义的,我不能(也不想)将其设置为固定范围。

有一个众所周知的模式吗?

最佳答案

我认为使用接受文字值的用户定义函数和用户定义聚合可以实现您想要的目标(这是由 JIRA CASSANDRA-10783 完成的)。让我解释一下它是如何实现的:

  1. 创建一个名为 is_in_interval() 的 AccumulatorFunction(请参阅下面的代码示例)
  2. 创建聚合matching_objects_in_interval()(请参阅下面的代码示例)
  3. 在查询中使用此聚合

实现示例(如果没有 CASSANDRA-10783 的补丁则无法编译)

CREATE FUNCTION is_in_interval(state set<text>, name text, timestamp timeuuid, 
min_date timeuuid, max_date timeuuid)
RETURNS NULL ON NULL INPUT
RETURNS set<text>
LANGUAGE java
AS $$
    // The object has it timestamp inside the provided date range
    if(timestamp.compareTo(min_date) >= 0 &&
       timestamp.compareTo(max_date) <= 0) {
       // We don't care adding multiple time because Set eliminates duplicates anyway 
       state.add(name);
    }
    return state;
$$;

CREATE AGGREGATE IF NOT EXISTS matching_objects_in_interval(text, timeuuid, timeuuid, timeuuid)
SFUNC is_in_interval
STYPE set<text>
// {} is the Cassandra LITERAL SYNTAX for empty set
INITCOND {};

用法:

SELECT matching_objects_in_interval(name, timestamp, minTimeuuid('2016-07-07T13:00:00+0000'), minTimeuuid('2016-07-07T14:00:00+0000'))
FROM states 
WHERE type = 'color' 
AND value = 'green';

关于cassandra - 状态随时间变化,列出在时间 A 和时间 B 之间具有状态 x = y 的所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36742364/

相关文章:

cassandra - 更改 cassandra 中的列类型时出错

mysql - 无法将数据从 Cassandra 导出到 MySql

cassandra - 用于将日志文件加载到cassandra的堆栈

Cassandra:获取列族的列名?

cassandra - 关于如何在 DataStax java 驱动程序中使用 map cql 类型的示例

cassandra - 如何使用 CQL 在表中添加两个列值?

索引上的 Cassandra IN 子句

java - 为一个或多个 @Params 创建 Cassandra @Query

java - Cassandra Datastax 连接池监视器/指标

java - Cassandra CQL表INSERT和INDEX问题