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

2024-05-12

我是 DynamoDB 新手。我希望创建一个使用 DeviceID 作为哈希键、时间戳作为范围键和一些数据的表。

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

在 SQL 中,我们可以使用日期时间类型作为时间戳,但在 DynamoDB 中没有。

  1. What data type should I use? String? Number?
    enter image description here

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

  3. 我需要在某个时间范围内搜索表格,例如:1/1/2015 10:00:00am 到 31/12/2016 11:00:00pm


The 字符串数据类型应用于日期或时间戳。

您可以使用字符串数据类型来表示日期或时间戳。 实现此目的的一种方法是使用 ISO 8601 字符串,如下所示 例子:

2016-02-15

2015-12-21T17:42:34Z

20150311T122706Z

DynamoDB 日期或时间戳的数据类型 https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes.String

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

没有时间的 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."}}}}}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DynamoDB 中的时间戳应使用什么数据类型? 的相关文章

随机推荐