Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query an Amazon DynamoDB by column named "timestamp" (a reserved word)

Tags:

I have a DynamoDB table with column named "timestamp" and am trying to query by date. I don't have control over changing the column name.

var params = {
    TableName : 'REPORT_CARD',

    KeyConditionExpression: "timestamp BETWEEN :startDate AND :endDate",
    ExpressionAttributeValues: {
        ":startDate": ""+startDate,
        ":endDate": ""+endDate
    }     
}

I get the error:

ERROR: ValidationException: Invalid KeyConditionExpression: Attribute name is a reserved keyword; reserved keyword: timestamp

Is there a workaround for this besides renaming the "timestamp" column?

like image 388
fandang Avatar asked Feb 23 '17 20:02

fandang


People also ask

Does DynamoDB have timestamp?

Amazon DynamoDB TTL allows you to define a per-item timestamp to determine when an item is no longer needed. After the expiration of the TTL timestamp, DynamoDB deletes the item from your table within 48 hours without consuming any write throughput.

Is status a reserved word in DynamoDB?

"Status" is a reserved keyword per documentation (docs.aws.amazon.com/amazondynamodb/latest/developerguide/…) .

Does DynamoDB support datetime?

There are multiple ways to represent a timestamp in DynamoDB. Probably the most common is to use a Number type to represent the timestamp with the value as a Unix timestamp (seconds or milliseconds). Additionally, you can store the timestamp as a String type with the value as an ISO 8601 formatted string.

Can we Query DynamoDB table without primary key?

Hash key in DynamoDB The primary reason for that complexity is that you cannot query DynamoDB without the hash key. So, it's not allowed to query the entire database. That means you cannot do what you would call a full table scan in other databases.


1 Answers

Firstly, assuming the table 'REPORT_CARD' is defined with partition key as timestamp, you can use only equality operator (i.e. '=') in KeyConditionExpression for querying data by partition key.

You may use BETWEEN if the timestamp is defined as sort key in the table.

Regarding the keyword error, you can use ExpressionAttributeNames to provide the attribute name timestamp. Example below:-

var params = {
    TableName: 'REPORT_CARD',

    KeyConditionExpression: "parition_key_attr = :partition_val AND #timestamp BETWEEN :startDate AND :endDate",
    ExpressionAttributeNames: { "#timestamp": "timestamp" },
    ExpressionAttributeValues: {
        ':partition_val': "somevalue",
        ":startDate": startDate,
        ":endDate": endDate
    }
like image 54
notionquest Avatar answered Sep 20 '22 13:09

notionquest