DynamoDB 中的时间戳应该使用什么数据类型?

What data type should be used for timestamp in DynamoDB?

我是 DynamoDB 的新手。我希望创建一个 table,它使用 DeviceID 作为散列键,时间戳作为我的范围键和一些数据。

{ DeviceID: 123, Timestamp: "2016-11-11T17:21:07.5272333Z", X: 12, Y: 35 }

在SQL中我们可以使用datetime类型作为Timestamp,但是在DynamoDB中有none。

  1. 我应该使用什么数据类型?细绳?数?

  2. 对于选择的数据类型,我应该写什么样的时间戳格式? ISO 格式(例如:2016-11-11T17:21:07.5272333Z)还是纪元时间(例如:1478943038816)?

  3. 我需要在一段时间内搜索 table,例如:1/1/2015 10:00:00am 到 31/12/2016 11:00:00pm

日期或时间戳应使用字符串数据类型

You can use the String data type to represent a date or a timestamp. One way to do this is by using ISO 8601 strings, as shown in these examples:

2016-02-15

2015-12-21T17:42:34Z

20150311T122706Z

DynamoDB Data type for Date or Timestamp

是的,当日期存储为字符串时支持范围查询。 BETWEEN 可用于 FilterExpresssion。我使用以下过滤器表达式获得了结果中的项目。

没有时间的FilterExpression:-

FilterExpression : 'createdate between :val1 and :val2',
ExpressionAttributeValues : {
        ':hkey' : year_val,
        ':rkey' : title,
        ":val1" : "2010-01-01",
        ":val2" : "2010-12-31"
    }

FilterExpression with time:-

FilterExpression : 'createdate between :val1 and :val2',
    ExpressionAttributeValues : {
        ':hkey' : year_val,
        ':rkey' : title,
        ":val1" : "2010-01-01T00:00:00",
        ":val2" : "2010-12-31T00:00:00"
    }

数据库值:-

格式 1 - 时区:

{"Item":{"createdate":{"S":"2010-12-21T17:42:34+00:00"},"title":{"S":"The Big New Movie 2010"},"yearkey":{"N":"2010"},"info":{"M":{"rating":{"N":"0"},"plot":{"S":"Nothing happens at all."}}}}}

格式 2 - 无时区:-

{"Item":{"createdate":{"S":"2010-12-21T17:42:34Z"},"title":{"S":"The Big New Movie 2010"},"yearkey":{"N":"2010"},"info":{"M":{"rating":{"N":"0"},"plot":{"S":"Nothing happens at all."}}}}}

数据类型取决于您的要求。

您可以使用 ISO 格式的字符串或纪元格式的数字。

ISO 格式(字符串)的优点是易于阅读,但 DynamoDB 不支持此格式的生存时间 (TTL)。正如 notionquest 所解释的那样,所有过滤器都可以工作,例如 'between' 和 'range'。

Time To Live (TTL) for DynamoDB allows you to define when items in a table expire so that they can be automatically deleted from the database.

使用纪元格式(数字)的好处是您可以使用 TTL 功能和所有过滤器。

TLDR;

纪元格式(数字类型)- 可以使用生存时间
ISO 格式(字符串类型)- 不能使用生存时间但更易读

数字数据类型字符串数据类型

可用于日期或时间戳 - 不仅仅是 字符串,因为此问题的已接受答案在忽略数字时错误地挑出。

You can use the number data type to represent a date or a timestamp. One way to do this is by using epoch time—the number of seconds since 00:00:00 UTC on 1 January 1970. For example, the epoch time 1437136300 represents 12:31:40 PM UTC on 17 July 2015.

For more information, see http://en.wikipedia.org/wiki/Unix_time.

...

You can use the String data type to represent a date or a timestamp. One way to do this is by using ISO 8601 strings, as shown in these examples:

2016-02-15

2015-12-21T17:42:34Z

20150311T122706Z

For more information, see http://en.wikipedia.org/wiki/ISO_8601.

DynamoDB Data type for Date or Timestamp

为了能够在发送查询请求时过滤掉结果,我为 DateTime 使用了纪元格式,它比使用字符串更有效。

想象一下这些场景:过去 31 天,过去 24 小时,...再次使用字符串格式一切皆有可能,因为它还有 begins_with 运算符(请查看 AWS 文档link 下方的第 3 个示例),但在排序(比较)和计算时,数值在性能方面效率更高。

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.KeyConditionExpressions

很容易将date-time转换为纪元格式

Javascript:

var date = new Date();
var epoch = date.getTime();

// converting back to date-time
var initial_date = new Date(epoch);

C#

var date = DateTime.UtcNow;
var epoch = new DateTimeOffset(date).ToUnixTimeSeconds();

// converting back to date-time
var initial_date = DateTimeOffset.FromUnixTimeSeconds(epoch);

Python

import time
epoch = time.time()
 
# converting back to date-time
initial_date = time.gmtime(epoch )