【发布时间】:2021-03-15 20:16:43
【问题描述】:
我在 DynamoDB 中有一个表格,格式如下:
| DeviceId (PK) | SensorDataType | SensorValue | CurrentTime (SK) |
|---|---|---|---|
| BSMD002 | HeartRate | 86 | 2021-03-13 14:50:17.292663 |
| BSMD002 | HeartRate | 106 | 2021-03-13 14:50:17.564644 |
| BSMD002 | HeartRate | 97 | 2021-03-13 14:50:17.854391 |
我正在使用 boto3 从该表中提取数据,并希望创建一个基于用户输入的新表(DeviceId、日期范围)。该表将包含传感器类型数据 - 按分钟分组的最大值、最小值和平均值。
我知道 DynamoDB 不支持聚合,使用 Streams + Lambda 是更有效的方法。但是想了解在boto3中是否有办法做到这一点。到目前为止,已经按照以下代码提取数据。
import boto3
import time
from datetime import datetime, timedelta
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('BSMDataTable')
devicetag = input(" Enter the Device ID to find: ").upper()
datefrom = input("Enter Starting Date in YYYY-MM-DD format: ")
dateto = input("Enter Ending Date in YYYY-MM-DD format: ")
fe = Key('CurrentTime').between(datefrom,dateto) & Key('DeviceId').eq(devicetag);
response = table.query(
KeyConditionExpression=fe
)
for i in response['Items']:
print(i)
【问题讨论】:
-
请将一些示例数据放入我在帖子中编辑的表格中,并告诉我们您到底在努力解决什么问题,这看起来是一个有希望的开始。同样在代码中,TimeStamp 列似乎被命名为 CurrentTime?
-
是的,CurrentTime 是时间戳。
-
DeviceId CurrentTime SensorType Reading A001 3/13/2021 11:01 Temp 82 A002 3/13/2021 11:01 Heart 92
-
请编辑您的问题,时间格式有问题,您将无法真正对它们进行范围查询 - 将其存储为 ISO8601 格式或 Unix Epoch。
标签: amazon-web-services amazon-dynamodb boto3 dynamodb-queries