简体   繁体   English

Azure存储表 - 实体版本历史记录

[英]Azure storage tables - Entity version history

I am trying to get my head out of relational database design and into Azure Storage Tables for a Big Data pet project I am about to embark upon. 我正试图从关系数据库设计中脱颖而出,进入Azure存储表,我即将开始进行大数据宠物项目。

My first question is around recording version histories of my entities. 我的第一个问题是记录我的实体的版本历史。

Say I have a table called Members and I want to be able to see previous versions of the members details could I set the PartitionKey as their member number and then the RowKey of the datetime stamp that it was updated? 假设我有一个名为Members的表,我希望能够看到以前版本的成员详细信息,我可以将PartitionKey设置为其成员编号,然后将日期时间戳的RowKey设置为更新吗?

Would this be an effective / recommended approach to this version history problem? 这是解决此版本历史问题的有效/推荐方法吗?

Thanks. 谢谢。

Update: Or would I be better to store the latest entry in the members table and then have a members "history" table that records the changes? 更新:或者我会更好地将最新条目存储在成员表中,然后有一个记录更改的成员“历史”表?

You can take both approaches. 你可以采取两种方法。 Just thinking out loud, here are some of the possible approaches: 只是大声思考,这里有一些可能的方法:

Approach 1: Keeping member information and history in a single table (1) 方法1:将会员信息和历史记录保存在一个表中(1)

The way this would work is you would keep the PartitionKey as member's unique identifier and the RowKey as the timestamp from when the member's information was updated. 这样做的方法是将PartitionKey保留为成员的唯一标识符,将RowKey为更新成员信息时的时间戳。 Since you would want to get the latest information about the member, I would recommend you keep the RowKey in reverse chronological order using something like: 由于您希望获得有关该成员的最新信息,我建议您使用以下内容按时间顺序保持RowKey

var rowKey = (DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks).ToString("d20");

To get the latest information about a member, you would fetch just one row for a given PartitionKey (ie member ID). 要获取有关成员的最新信息,您只需为给定的PartitionKey (即成员ID)获取一行。 To get complete history about a member, you would just fetch all rows. 要获取有关成员的完整历史记录,您只需获取所有行。

One problem with this approach is that you can't fetch current details about any members without knowing their member IDs. 此方法的一个问题是,如果不知道其成员ID,则无法获取有关任何成员的当前详细信息。

Approach 2: Keeping member information and history in separate tables 方法2:将成员信息和历史记录保存在单独的表中

In this approach you will maintain two tables—one for member information (let's call it Member ) and other for member history (let's call it MemberHistory ). 在这种方法中,您将维护两个表 - 一个用于成员信息(让我们称之为Member ),另一个用于成员历史(让我们称之为MemberHistory )。 The PartitionKey in Member table could be member ID and row key could be any arbitrary value (or even null value). Member表中的PartitionKey可以是成员ID,行键可以是任意值(甚至是null值)。 The PartitionKey in the MemberHistory table would be member ID and the RowKey would be the timestamp when the information was updated. MemberHistory表中的PartitionKey将是成员ID, RowKey将是更新信息时的时间戳。 Again, you would want to store them in reverse chronological order. 同样,您可能希望以反向时间顺序存储它们。

The way this would work is that you always perform an InsertOrUpdate (or InsertOrMerge ) operation on entities in Members table, while you would always perform Insert operation on entities in MembersHistory table. 这将起作用的方式是您始终对Members表中的实体执行InsertOrUpdate (或InsertOrMerge )操作,而您始终对MembersHistory表中的实体执行Insert操作。

While this approach is much neater and gives you the capability of extracting current information about all members, the problem you will run into is that you can't take the advantage of the transaction feature in Azure Table Storage since you're dealing with two tables. 虽然这种方法更简洁并且使您能够提取有关所有成员的当前信息,但您将遇到的问题是,由于您正在处理两个表,因此无法利用Azure表存储中的事务功能。 So it may happen that your main table gets updated but your history table fails to update, thus you have to cover for that scenario as well. 因此,您的主表可能会更新,但您的历史记录表无法更新,因此您也必须覆盖该方案。

Approach 3: Keeping member information and history in a single table (2) 方法3:将会员信息和历史保存在一个表中(2)

In this approach, for the main record and history record, you would keep the PartitionKey as member ID but keep the RowKey as empty for the main record and RowKey as the timestamp (again in reverse chronological order) for the history tables. 在这种方法中,对主记录和历史记录,您将保持PartitionKey作为会员ID,但保持RowKey为空的主记录和RowKey的历史表的时间戳(再次按时间倒序排列)。

To query a member's current record, you would query something like (pseudo code): 要查询成员的当前记录,您将查询类似(伪代码)的内容:

PartitionKey == 'Member ID' && RowKey == ''

To query a member's history record, you would query something like (pseudo code): 要查询成员的历史记录,您可以查询类似(伪代码)的内容:

PartitionKey == 'Member ID' && RowKey != ''

To query all members for their current record, you would query something like (pseudo code): 要查询所有成员的当前记录,您将查询类似(伪代码)的内容:

RowKey == ''

Please note that the query above will do a full table scan so be prepared for continuation tokens and some performance degradation. 请注意,上面的查询将执行全表扫描,因此请为持续令牌和某些性能降级做好准备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM