Python 是否可以将日期时间保存到 DynamoDB?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27894393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to save datetime to DynamoDB?
提问by Alexander Perechnev
I have the next code:
我有下一个代码:
users_table = Table(users_table_name, connection=Core.aws_dynamodb_connection)
users_table.put_item(data={
"login": login,
"password": hashlib.sha256(password.encode("utf-8")).hexdigest(),
"profile": profile,
"registration_date": datetime.now() # PROBLEM IS HERE
})
But when I run it, it fails with error:
但是当我运行它时,它失败并出现错误:
TypeError: Unsupported type "< type 'datetime.datetime' >" for value "2015-01-12 05:02:57.053131"
类型错误:值“2015-01-12 05:02:57.053131”不支持类型“< type 'datetime.datetime'>”
I've tried a lot of ways, but it seems that it isn't possible to save datetime
to DynamoDB. Btw it works fine in MongoDB.
尝试了很多方法,但似乎无法保存datetime
到DynamoDB。顺便说一句,它在 MongoDB 中运行良好。
Is there any solution?
有什么解决办法吗?
采纳答案by Alexander Perechnev
Okay, I see that DynamoDB does not support any date types. So the only solution is to use unix-like time as integer, or save date as string.
好的,我看到 DynamoDB 不支持任何日期类型。所以唯一的解决方案是使用类unix时间作为整数,或者将日期保存为字符串。
回答by Stephen Lin
If you want to use date to find users, you can simply invoke date()
function. Like this:
如果要使用日期查找用户,只需调用date()
函数即可。像这样:
...
users_table = Table(users_table_name, connection=Core.aws_dynamodb_connection)
current = datetime.now()
users_table.put_item(data={
"login": login,
"password": hashlib.sha256(password.encode("utf-8")).hexdigest(),
"profile": profile,
# here use a different name for the entry
"registration_time": current
"registration_date": current.date()
})
...
回答by Anzel
I'm not sure why datetime isn't supported in DynamoDB, or in fact I have no experience in it neither.
我不确定为什么 DynamoDB 不支持日期时间,或者实际上我也没有这方面的经验。
But if you're so insisted in not converting the datetime to string like people suggested, you can convert the datetime to timestamp, and so you can compare with it.
但是,如果您坚持不要像人们建议的那样将日期时间转换为字符串,那么您可以将日期时间转换为时间戳,这样您就可以与它进行比较。
updated
更新
And you may want to read this SO Question, seemed like numeric comparison is the preferred way.
您可能想阅读这个SO Question,似乎数字比较是首选方式。
回答by H6.
These are all the supported types for attribute values in DynamoDB as listed in their AWS Docs.
这些是 DynamoDB 中所有受支持的属性值类型,如其AWS 文档 中所列。
BA Binary data type.
Type: Blob
Required: No
BOOLA Boolean data type.
Type: Boolean
Required: No
BSA Binary Set data type.
Type: array of Blobs
Required: No
LA List of attribute values.
Type: array of AttributeValue objects
Required: No
MA Map of attribute values.
Type: String to AttributeValue object map
Required: No
NA Number data type.
Type: String
Required: No
NSA Number Set data type.
Type: array of Strings
Required: No
NULLA Null data type.
Type: Boolean
Required: No
SA String data type.
Type: String
Required: No
SSA String Set data type.
Type: array of Strings
Required: No
B二进制数据类型。
类型:斑点
要求:否
BOOL布尔数据类型。
类型:布尔型
要求:否
BS二进制集数据类型。
类型:Blob 数组
要求:否
L属性值列表。
类型:AttributeValue 对象数组
要求:否
M属性值的映射。
类型:字符串到 AttributeValue 对象映射
要求:否
N数字数据类型。
类型:字符串
要求:否
NS数字集数据类型。
类型:字符串数组
要求:否
NULL空数据类型。
类型:布尔型
要求:否
S字符串数据类型。
类型:字符串
要求:否
SSA String Set 数据类型。
类型:字符串数组
要求:否
回答by Fred Campos
According to the documentation: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaSDKHighLevel.html
根据文档:http: //docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaSDKHighLevel.html
Date S (string type). The Date values are stored as ISO-8601 formatted strings.
日期 S(字符串类型)。日期值存储为 ISO-8601 格式的字符串。
回答by alejo4373
回答by user1855042
According to alejandro-franco response.isoformat()
make the trick.
根据 alejandro-franco 的回应.isoformat()
使出伎俩。
Just tested and this a working example:
刚刚测试过,这是一个工作示例:
CustomerPreferenceTable.put_item(
Item={
"id": str(uuid4()),
"validAfter": datetime.utcnow().isoformat(),
"validBefore": (datetime.utcnow() + timedelta(days=365)).isoformat(),
"tags": ["potato", "eggplant"]
}
)
回答by dvoelker
old post but maybe still interesting ..
旧帖子,但也许仍然很有趣..
What you can do and how it worked for me:
您可以做什么以及它如何为我工作:
import datetime
from datetime import datetime
...
x = datetime.now()
x = now.strftime("%m/%d/%Y, %H:%M:%S")
table.put_item(
Item={
'Index': Index,
'Stamp': x,
}
)
And with adaption to the code above:
并适应上面的代码:
import datetime
from datetime import datetime
...
x = datetime.now()
x = now.strftime("%m/%d/%Y, %H:%M:%S")
users_table = Table(users_table_name, connection=Core.aws_dynamodb_connection)
users_table.put_item(data={
"login": login,
"password": hashlib.sha256(password.encode("utf-8")).hexdigest(),
"profile": profile,
"registration_date": x,
})
My Output
我的输出