简体   繁体   中英

DynamoDB primarykey timestamp

iam trying to query a dataset in dynamodb where the primary key is a timestamp. First i wanted to get all data for a specific sensorId. I tried with a scan (scan.json):

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

This Json was used via CLI command:

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

That was successful and gave me all data for the specified 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):

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

aws cli command

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; reserved keyword: timestamp

But replacing "timestamp" with "sensorId for testing purpose gave me:

An error occurred (ValidationException) when calling the Query operation: Query condition missed key schema element: timestamp

And i understand that that sensorId is not valid for key-expression.. KeyConditionExpression accepts only key attributes, hash key and range key. But how to get the result?

I want only the timestamps for a sensorId. Is my Primarykey wrong? should be better to use sensorId as primary key together with timestamp as range key?

Based on your scenario, its better to change your keys. Use SensorID as the Partition Key and Timestamp as the Sort Key.

This way you can query (Without scanning all the items) the items for a given 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.

Note: TimeStamp is a reserved word in DynamoDB. You can use Expression Attribute Names to avoid the errors when using reserved attributes in query expressions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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