Query

use condition to query

  • ExpressionAttributeNames
    • A map of placeholder names (like #n) to real attribute names in DynamoDB.
  • ExpressionAttributeValues
    • A map of placeholder values (like :v) to the actual values you want to use.
dynamodb = boto3.client("dynamodb")
now_ms = time.time() * 1000
start_ms = now_ms - range * 1000
 
resp = dynamodb.query(
	TableName=tablename,
	KeyConditionExpression="bucket_name = :b AND #ts BETWEEN :s AND :e",
	ExpressionAttributeNames={"#ts": "timestamp"},
	ExpressionAttributeValues={
		":b": {"S": key},
		":s": {"N": str(start_ms)},
		":e": {"N": str(now_ms)},
	},
	ScanIndexForward=True
)

what query look like

{
   "Items":[
      {
         "total_size_bytes":{
            "N":"31"
         },
         "bucket_name":{
            "S":"assign2-test-bucket-alan"
         },
         "timestamp":{
            "N":"1770779901052"
         }
      },
      {
         "total_size_bytes":{
            "N":"31"
         },
         "bucket_name":{
            "S":"assign2-test-bucket-alan"
         },
         "timestamp":{
            "N":"1770779901523"
         }
      },
      {...},
      {...}
   ],
   "Count":4,
   "ScannedCount":4,
   "ResponseMetadata":{
      ...
      },
      "RetryAttempts":0
   }
}