简体   繁体   English

DynamoDB主键时间戳

[英]DynamoDB primarykey timestamp

iam trying to query a dataset in dynamodb where the primary key is a timestamp. 我试图在dynamodb中查询主键是时间戳的数据集。 First i wanted to get all data for a specific sensorId. 首先,我想获取特定传感器ID的所有数据。 I tried with a scan (scan.json): 我尝试了扫描(scan.json):

{
"sensorId": {
    "AttributeValueList": [{
        "S": "1234"
    }],
    "ComparisonOperator": "EQ"
}
  }

This Json was used via CLI command: 此Json通过CLI命令使用:

aws dynamodb scan --table-name sensorData --scan-filter file://scan.json 

That was successful and gave me all data for the specified sensorid. 那成功了,并为我提供了指定sensorid的所有数据。

Now if i want to have only the timestamp and sensorId as result, i read about projection-expression and tried to do a query (query.json): 现在,如果我只希望将时间戳记和sensorId作为结果,那么我将了解有关projection-expression的信息,并尝试执行查询(query.json):

{
    ":Id":{"S":"1234"}
}

aws cli command aws cli命令

aws dynamodb query --table-name sensorData --key-condition-expression "sensorId= :Id" --expression-attribute-values file://query2.json --projection-expression "timestamp"

Gave me : 给我 :

An error occurred (ValidationException) when calling the Query operation: Invalid ProjectionExpression: Attribute name is a reserved keyword; 调用Query操作时发生错误(ValidationException):无效的ProjectionExpression:属性名称是保留的关键字; reserved keyword: timestamp 保留关键字:时间戳记

But replacing "timestamp" with "sensorId for testing purpose gave me: 但是出于测试目的,将“ timestamp”替换为“ sensorId”给了我:

An error occurred (ValidationException) when calling the Query operation: Query condition missed key schema element: timestamp 调用Query操作时发生错误(ValidationException):查询条件缺少关键架构元素:时间戳记

And i understand that that sensorId is not valid for key-expression.. KeyConditionExpression accepts only key attributes, hash key and range key. 而且我知道该sensorId对于键表达式无效。。KeyConditionExpression仅接受键属性,哈希键和范围键。 But how to get the result? 但是如何得到结果呢?

I want only the timestamps for a sensorId. 我只需要sensorId的时间戳。 Is my Primarykey wrong? 我的主键错了吗? should be better to use sensorId as primary key together with timestamp as range key? 最好使用sensorId作为主键以及将时间戳作为范围键?

Based on your scenario, its better to change your keys. 根据您的方案,最好更改密钥。 Use SensorID as the Partition Key and Timestamp as the Sort Key. 使用SensorID作为分区键,并使用时间戳作为排序键。

This way you can query (Without scanning all the items) the items for a given SensorID. 这样,您可以查询(不扫描所有项目)给定SensorID的项目。 Also its possible to sort them in order of the Timestamp. 也可以按时间戳顺序对它们进行排序。

If you have a larger dataset (More than several Killobytes) it would be efficient to create a LSI or GSI to project the required attributes for a given query. 如果您有一个较大的数据集(超过几个千字节),则创建LSI或GSI来投影给定查询所需的属性将很有效。

Note: TimeStamp is a reserved word in DynamoDB. 注意:TimeStamp是DynamoDB中的保留字 You can use Expression Attribute Names to avoid the errors when using reserved attributes in query expressions. 您可以使用表达式属性名称来避免在查询表达式中使用保留属性时出现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM