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

时间:2022-01-16 16:59:59

I am new to DynamoDB. I wish to create a table which using DeviceID as the hash key, Timestamp as my range key and some data.

我是DynamoDB的新手。我希望创建一个表,它使用DeviceID作为哈希键,Timestamp作为我的范围键和一些数据。

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

In SQL, we can use datetime type for Timestamp, but in DynamoDB there is none.

在SQL中,我们可以将datetime类型用于Timestamp,但在DynamoDB中没有。

  1. What data type should I use? String? Number?
    DynamoDB中的时间戳应该使用什么数据类型?

    我应该使用哪种数据类型?串?数?

  2. For the chosen data type, what kind of timestamp format should I write in? ISO format (e.g: 2016-11-11T17:21:07.5272333Z) or epoch time (e.g: 1478943038816)?

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

  3. I need to search through the table through a range of time, e.g: 1/1/2015 10:00:00am until 31/12/2016 11:00:00pm

    我需要在一段时间内搜索表格,例如:2015年1月1日上午10:00:00至2016年12月31日11:00:00

1 个解决方案

#1


28  

The String data type should be used for Date or Timestamp.

String数据类型应该用于Date或Timestamp。

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:

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

2016-02-15

2016年2月15日

2015-12-21T17:42:34Z

2015-12-21T17:42:34Z

20150311T122706Z

20150311T122706Z

DynamoDB Data type for Date or Timestamp

日期或时间戳的DynamoDB数据类型

Yes, the Range queries are supported when the date is stored as String. The BETWEEN can be used on FilterExpresssion. I have got the items in the result using the below filter expressions.

是的,当日期存储为String时,支持Range查询。 BETWEEN可用于FilterExpresssion。我使用下面的过滤器表达式得到了结果中的项目。

FilterExpression without time:-

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随着时间的推移: -

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

Database Values:-

数据库值: -

Format 1 - with timezone:

格式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."}}}}}

Format 2 - without timezone:-

格式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."}}}}}

#1


28  

The String data type should be used for Date or Timestamp.

String数据类型应该用于Date或Timestamp。

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:

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

2016-02-15

2016年2月15日

2015-12-21T17:42:34Z

2015-12-21T17:42:34Z

20150311T122706Z

20150311T122706Z

DynamoDB Data type for Date or Timestamp

日期或时间戳的DynamoDB数据类型

Yes, the Range queries are supported when the date is stored as String. The BETWEEN can be used on FilterExpresssion. I have got the items in the result using the below filter expressions.

是的,当日期存储为String时,支持Range查询。 BETWEEN可用于FilterExpresssion。我使用下面的过滤器表达式得到了结果中的项目。

FilterExpression without time:-

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随着时间的推移: -

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

Database Values:-

数据库值: -

Format 1 - with timezone:

格式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."}}}}}

Format 2 - without timezone:-

格式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."}}}}}