如何在 Cassandra 中为客户的最近 100 个事件建模数据
How to model data in Cassandra for last 100 events for a customer
我们有多个客户,每个客户 运行 有多个传感器。每个传感器频繁记录数据(事件 20s)。我如何在 Cassandra 中创建数据模型来回答这个查询?
我们为其他查询想到了这样的数据模型:
Create Table Data{
CustomerId,
SensorId,
Date,
DataTime
SensorData1,
SensorData2,
Primary key ((CustomerId, SensorId, Date), DataTime)
}
为了满足客户最近 100 个事件的查询,您需要对上述模型进行两项调整:
调整您的 PRIMARY KEY 定义以仅在您的 CustomerId
和日期存储桶 (Date
) 上进行分区。然后,您需要在 DataTime
上进行聚类。为了确保传感器的唯一性,您可能还想在末尾添加 SensorId
。
在 datatime
上添加排序方向为 DESC
的 CLUSTERING ORDER
。这将按 datatime
对磁盘上的数据进行聚类,最近的时间排在第一位。
基本上,我是这样创建你的 table 的:
CREATE TABLE sensordata2 (
customerid uuid,
datebucket text,
datatime timeuuid,
sensorid text,
sensordata1 text,
sensordata2 text,
PRIMARY KEY ((customerid, datebucket), datatime, sensorid)
) WITH CLUSTERING ORDER BY (datatime DESC, sensorid ASC);
插入一些测试行后,我现在可以像这样查询最后 10 个传感器读数 WHERE customerid 3221b1d7-13b4-40d4-b41c-8d885c63494f
:
aploetz@cqlsh:Whosebug2> SELECT customerid, datebucket, sensorid, dateof(datatime), datatime, sensordata1, sensordata2
FROM sensordata2 WHERE customerid=3221b1d7-13b4-40d4-b41c-8d885c63494f
AND datebucket='20150515' LIMIT 10;
customerid | datebucket | sensorid | dateof(datatime) | datatime | sensordata1 | sensordata2
--------------------------------------+------------+----------+--------------------------+--------------------------------------+-------------+-------------
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:34:34-0500 | e3a15c20-fb17-11e4-93da-21b264d4c94d | 47 | 24
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:34:34-0500 | e39ffc90-fb17-11e4-93da-21b264d4c94d | 46 | 23
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:34:34-0500 | e39e4ee0-fb17-11e4-93da-21b264d4c94d | 45 | 22
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | B1 | 2015-05-15 10:34:22-0500 | dc64a340-fb17-11e4-93da-21b264d4c94d | 47 | 24
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | B1 | 2015-05-15 10:34:22-0500 | dc60aba0-fb17-11e4-93da-21b264d4c94d | 46 | 23
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | B1 | 2015-05-15 10:34:22-0500 | dc5d0220-fb17-11e4-93da-21b264d4c94d | 45 | 22
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:32:16-0500 | 90e27fa0-fb17-11e4-93da-21b264d4c94d | 47 | 24
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:32:16-0500 | 90e0aae0-fb17-11e4-93da-21b264d4c94d | 46 | 23
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:32:16-0500 | 90de8800-fb17-11e4-93da-21b264d4c94d | 45 | 22
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:25:24-0500 | 9b5d1ae0-fb16-11e4-93da-21b264d4c94d | 47 | 24
(10 rows)
我们有多个客户,每个客户 运行 有多个传感器。每个传感器频繁记录数据(事件 20s)。我如何在 Cassandra 中创建数据模型来回答这个查询?
我们为其他查询想到了这样的数据模型:
Create Table Data{
CustomerId,
SensorId,
Date,
DataTime
SensorData1,
SensorData2,
Primary key ((CustomerId, SensorId, Date), DataTime)
}
为了满足客户最近 100 个事件的查询,您需要对上述模型进行两项调整:
调整您的 PRIMARY KEY 定义以仅在您的
CustomerId
和日期存储桶 (Date
) 上进行分区。然后,您需要在DataTime
上进行聚类。为了确保传感器的唯一性,您可能还想在末尾添加SensorId
。在
datatime
上添加排序方向为DESC
的CLUSTERING ORDER
。这将按datatime
对磁盘上的数据进行聚类,最近的时间排在第一位。
基本上,我是这样创建你的 table 的:
CREATE TABLE sensordata2 (
customerid uuid,
datebucket text,
datatime timeuuid,
sensorid text,
sensordata1 text,
sensordata2 text,
PRIMARY KEY ((customerid, datebucket), datatime, sensorid)
) WITH CLUSTERING ORDER BY (datatime DESC, sensorid ASC);
插入一些测试行后,我现在可以像这样查询最后 10 个传感器读数 WHERE customerid 3221b1d7-13b4-40d4-b41c-8d885c63494f
:
aploetz@cqlsh:Whosebug2> SELECT customerid, datebucket, sensorid, dateof(datatime), datatime, sensordata1, sensordata2
FROM sensordata2 WHERE customerid=3221b1d7-13b4-40d4-b41c-8d885c63494f
AND datebucket='20150515' LIMIT 10;
customerid | datebucket | sensorid | dateof(datatime) | datatime | sensordata1 | sensordata2
--------------------------------------+------------+----------+--------------------------+--------------------------------------+-------------+-------------
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:34:34-0500 | e3a15c20-fb17-11e4-93da-21b264d4c94d | 47 | 24
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:34:34-0500 | e39ffc90-fb17-11e4-93da-21b264d4c94d | 46 | 23
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:34:34-0500 | e39e4ee0-fb17-11e4-93da-21b264d4c94d | 45 | 22
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | B1 | 2015-05-15 10:34:22-0500 | dc64a340-fb17-11e4-93da-21b264d4c94d | 47 | 24
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | B1 | 2015-05-15 10:34:22-0500 | dc60aba0-fb17-11e4-93da-21b264d4c94d | 46 | 23
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | B1 | 2015-05-15 10:34:22-0500 | dc5d0220-fb17-11e4-93da-21b264d4c94d | 45 | 22
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:32:16-0500 | 90e27fa0-fb17-11e4-93da-21b264d4c94d | 47 | 24
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:32:16-0500 | 90e0aae0-fb17-11e4-93da-21b264d4c94d | 46 | 23
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:32:16-0500 | 90de8800-fb17-11e4-93da-21b264d4c94d | 45 | 22
3221b1d7-13b4-40d4-b41c-8d885c63494f | 20150515 | A1 | 2015-05-15 10:25:24-0500 | 9b5d1ae0-fb16-11e4-93da-21b264d4c94d | 47 | 24
(10 rows)