将字符串字段转换为 Azure 表存储中的日期时间字段
我在 Azure 表中有一个字段,以字符串格式保存日期时间。我想用日期时间字段替换它并转换字符串列中的值。解决这个问题的最佳方法是什么?
在 SQL 中,我会通过创建一个新列并运行更新语句来完成此操作...
I have a field in an Azure table that holds date times in string format. I want to replace this with a DateTime field and convert the values in the string column. What's the best way to approach this?
In a SQL I'd do this by creating a new column and run an update statement...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我自己还没有尝试过,但以下是/应该/有效的步骤。这需要是 C#/VB/等脚本。
//现在你的表存储包含具有该变量的新定义和旧定义的对象
现在,如果您希望新的日期时间字段采用原始字符串变量的名称,请再次执行步骤 1 到 5,这次将数据从一个字段移至另一个字段。唯一的区别是您不必再次进行转换。
一般来说,这是一个完整的 PITA,也是对象数据库和存储服务的祸根之一。
I haven't tried this myself, but here are the steps that /should/ work. This needs to be a C#/VB/etc script.
//now your table storage contains objects with new and old definition of that variable
Now, if you want the new datetime field to take the name of the original string variable do steps 1 thru 5 again, this time moving the data from one field into another. The only difference is that you don't have to do conversion again.
In general, this is a complete PITA and one of the banes of object databases and storage services.
Azure 表存储客户端本身理解 DateTime(但不理解 DateTimeOffset),因此您可能需要考虑简单地让 SDK 为您处理这个问题。
否则,将 DateTimes 表示为字符串的最佳方法是将它们显式存储和检索为使用“u”格式写入和解析的 UTC 值,例如
The Azure Table Storage Client natively understands DateTime (but not DateTimeOffset), so you may want to consider simply letting the SDK take care of the matter for you.
Otherwise, the best way to represent DateTimes as strings is to explicitly store and retrieve them as UTC values written and parsed using the "u" format, e.g.
我不知道它是否会起作用,但你可以尝试“幸福的无知”:
真实表存储实际上并不关心你的实体的形状(开发结构可能不同)。将实体属性更改为 DateTime,但在 Setter 测试中,如果不将值解析为 DT,则该值是 Datetime。 希望随着时间的推移
,或者明确地批量,如果您只是加载实体并再次保存它们,它会将它们全部设置为 DateTime。
I have no idea if it'll work, but you could try "blissful ignorance":
Real Table store doesn't actually care about the shape of your entities (Dev fabric may be different). Change your entity property to DateTime, but in the Setter test the value is a Datetime, if not parse the value into a DT. Something like
Hopefully over time, or explicitly in a batch, if you simply load the entities and save them again it'll set them all to DateTime.