简体   繁体   English

AWS Dynamodb扫描顺序?

[英]AWS Dynamodb scan ordering?

We have a setup where various worker nodes perform computations and update their relative states in a DynamoDB table. 我们有一个设置,其中各种工作程序节点在DynamoDB表中执行计算并更新它们的相对状态。 The table acts as a kind of history of activity of the worker nodes. 该表充当一种工作节点活动的历史记录。 A watchdog node needs to periodically scan through the table, and build an object representing the current state of the worker nodes and their jobs. 看门狗节点需要定期扫描表,并建立一个代表工作程序节点及其作业的当前状态的对象。 As such, it's important for our application to be able to scan the table and retrieve data in chronological order (ie sorted by timestamp). 因此,对于我们的应用程序来说,能够按时间顺序扫描表并检索数据(即按时间戳排序)很重要。 The table will eventually be too large to scan into local memory for later ordering, so we cannot sort it after scanning. 该表最终将太大,无法扫描到本地内存中以供以后排序,因此我们无法在扫描后对其进行排序。

Reading from the AWS documentation about the primary key: AWS文档中读取有关主键的信息:

DynamoDB uses the partition key value as input to an internal hash function. DynamoDB使用分区键值作为内部哈希函数的输入。 The output from the hash function determines the partition (physical storage internal to DynamoDB) in which the item will be stored. 哈希函数的输出确定将在其中存储项目的分区(DynamoDB内部的物理存储)。 All items with the same partition key are stored together, in sorted order by sort key value . 具有相同分区键的所有项目都按排序键值的排序顺序存储在一起。

Documentation on the scan function doesn't seem to mention anything about the order of the returned results. 扫描功能的文档似乎没有提及返回结果的顺序。 But can that last part in the quote above (the part I emphasized in bold) be interpreted to mean that the results of scans are ordered by the sort key? 但是,上面引用中的最后一部分(我用黑体强调的那部分)是否可以解释为扫描结果是按排序键排序的? If I set all partition keys to be the same value, say "0", then use my timestamp as the sort key, can I be guaranteed that the scan operation will return data in chronological order? 如果将所有分区键设置为相同的值,例如“ 0”,然后将时间戳记用作排序键,是否可以保证扫描操作将按时间顺序返回数据?

Some note: 一些注意事项:

  • All code is written in Python, and thus I'm using the boto3 module to perform scan operations. 所有代码都是用Python编写的,因此我正在使用boto3模块执行扫描操作。
  • Our system architect is steadfast against the idea of updating any entries in the table to reflect their current state, or deleting items when the job is complete. 我们的系统架构师坚决反对更新表中的任何条目以反映其当前状态,或在作业完成时删除项目的想法。 We can only ever add to the table, and thus we need to scan through the whole thing each time to determine the worker states. 我们只能添加到表中,因此我们每次都需要遍历整个事物以确定工作人员状态。
  • I am using strong read consistency for scan operations. 我对扫描操作使用强读取一致性。

Technically SCAN never guarantees order (although as an observation the lack of order guarantee seems to mean that the partition is randomly ordered, but the sort remains, well, sorted.) 从技术上讲, SCAN从不保证顺序(尽管观察到缺少顺序保证似乎意味着分区是随机排序的,但是排序仍然很好地进行了排序。)

What you've proposed will work however, but instead of scanning, you'll be doing a query on partition-key == 0 , which will then return all the items with the partition key of 0 , (up to limit and optional sorted forward/backwards) sorted by the sort key. 所建议的内容可以使用,但不是扫描,而是对partition-key == 0进行查询 ,然后将返回分区键为0所有项目(最大limit和可选排序)前进/后退)按排序键排序。

That said, this is really not the way that dynamo wants you to use it. 就是说,这真的不是发电机要您使用它的方式。 For example, it guarantees your partition will run hot (because you've explicitly put everything on the same partition), and this operation will cost you the capacity of reading every item on the table. 例如,它保证您的分区将运行热(因为您已将所有内容明确地放在同一分区上),并且此操作将使您失去读取表上每个项目的能力。

I would recommend investigating patterns such as using a dynamodb stream processed by a lambda to build and maintain a materialised view of this "current state", rather than "polling" the table with this expensive scan and resulting poor key design. 我建议您研究一些模式,例如使用由lambda处理的发电机组流来构建和维护此“当前状态”的实体化视图,而不是通过这种昂贵的扫描和不良的键设计来“轮询”表。

You're better off using yyyy-mm-dd as the partition key, rather than all 0 . 最好使用yyyy-mm-dd作为分区键,而不是全部为0 There's a limit of 10 GB of data per partition, which also means you can't have more than 10 GB of data per partition key value. 每个分区的数据限制为10 GB,这也意味着每个分区键值的数据量不能超过10 GB。

If you want to be able to retrieve data sorted by date, take the ISO 8601 time stamp format (roughly yyyy-mm-ddThh-mm-ss.sss ), split it somewhere reasonable for your data, and use the first part as the partition key and the second part as the sort key. 如果您希望能够检索按日期排序的数据,请采用ISO 8601时间戳格式(大致为yyyy-mm-ddThh-mm-ss.sss ),将其分割成适合您数据的位置,然后将第一部分用作分区键,第二部分作为排序键。 (Another advantage of this approach is that you can use eventually consistent reads for most of the queries since it's pretty safe to assume that after a day (or an hour o something) that the data is completely replicated.) (此方法的另一个优点是,您可以对大多数查询使用最终一致的读取,因为可以肯定的是,假设一天(或一个小时左右)后数据已完全复制。)

If you can manage it, it would be even better to use Worker ID or Job ID as a partition key, and then you could use the full time stamp as the sort key. 如果可以管理它,则最好使用Worker ID或Job ID作为分区键,然后可以将完整时间戳记用作排序键。

As @thomasmichaelwallace mentioned, it would be best to use DynamoDB streams with Lambda to create a materialized view. 正如@thomasmichaelwallace所提到的,最好将DynamoDB流与Lambda一起使用来创建实例化视图。

Now, that being said, if you're dealing with jobs being run on workers, then you should also consider whether you can achieve your goal by using a workflow service rather than a database. 话虽如此,如果要处理在工人上执行的作业,那么您还应该考虑是否可以通过使用工作流服务而不是数据库来实现目标。 Workflows will maintain a job history and/or current state for you. 工作流程将为您维护工作历史和/或当前状态。 AWS offers Step Functions and Simple Workflow . AWS提供了步骤功能简单工作流程

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

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