简体   繁体   English

DynamoDB - 新放置的项目不会在扫描中反映出来

[英]DynamoDB - Newly put items are not reflecting in scan

I have a problem with DynamoDB scan. 我有DynamoDB扫描的问题。 I added new items to the table using putItem method. 我使用putItem方法向表中添加了新项。

[[AmazonClientManager ddb] putItem:request];

But when I try to fetch using scan using scan method, that item is not coming in the result. 但是当我尝试使用扫描方法使用扫描获取时,该项目不会出现在结果中。

DynamoDBScanResponse *response = [[AmazonClientManager ddb] scan:request];

I am getting the below response, 我收到以下回复,

{Items: ( ),Count: 0,ScannedCount: 608,LastEvaluatedKey: {HashKeyElement: {S: U2575220130319062347000,N: (null),SS: ( ),NS: ( ),},RangeKeyElement: (null),},ConsumedCapacityUnits: 129,{requestId: 3GVT8PJGV4VB45IUPUA6KIN9URVV4KQNSO5AEMVJF66Q9ASUAAJG}} {Items :(),Count:0,ScannedCount:608,LastEvaluatedKey:{HashKeyElement:{S:U2575220130319062347000,N:(null),SS :(),NS:(),},RangeKeyElement:(null),}, ConsumedCapacityUnits:129,{requestId:3GVT8PJGV4VB45IUPUA6KIN9URVV4KQNSO5AEMVJF66Q9ASUAAJG}}

But these items are showing in table, when I checked using AWS Console. 但是当我使用AWS控制台检查时,这些项目显示在表格中。 Could anyone let me know what could be the issue? 谁能让我知道可能是什么问题?

Thanks. 谢谢。

Scan API is eventual consistent. Scan API最终是一致的。 Eventual consistent read may not return most recent changes. 最终的一致性读取可能不会返回最近的更改。 There is a slight delay (not more than a few seconds). 有一点延迟(不超过几秒钟)。

Query API allows consistent option. 查询API允许一致的选项。 You may use Query if that's an option for you. 如果这是您的选项,您可以使用查询。

Another possibility is that you may not have finished processing the results of the scan yet -- Scan needs to be repeated until LastEvaluatedKey is null. 另一种可能性是您可能尚未完成扫描结果的处理 - 需要重复扫描,直到LastEvaluatedKey为空。

http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html

Setting ConsistentRead to true works for me: 将ConsistentRead设置为true对我有用:

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html#DDB-Scan-request-ConsistentRead https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html#DDB-Scan-request-ConsistentRead

A Boolean value that determines the read consistency model during the scan: 一个布尔值,用于确定扫描期间的读取一致性模型:

If ConsistentRead is false, then the data returned from Scan might not contain the results from other recently completed write operations (PutItem, UpdateItem or DeleteItem). 如果ConsistentRead为false,则从Scan返回的数据可能不包含其他最近完成的写操作(PutItem,UpdateItem或DeleteItem)的结果。

If ConsistentRead is true, then all of the write operations that completed before the Scan began are guaranteed to be contained in the Scan response. 如果ConsistentRead为true,则保证扫描开始之前完成的所有写入操作都包含在扫描响应中。

The default setting for ConsistentRead is false. ConsistentRead的默认设置为false。

If anyone is having issues with consistent reads, meaning you are expecting them, but it does not seem to always work, here was my findings/solution... 如果有人遇到一致读取问题,意味着你期待它们,但它似乎并不总是有效,这是我的发现/解决方案......

  1. Assuming you are using the AWS Java SDK - com.amazonaws:aws-java-sdk-dynamodb 假设您使用的是AWS Java SDK - com.amazonaws:aws-java-sdk-dynamodb
  2. First, we made a unit test that loops at high speed to be able to recreate the read inconsistency. 首先,我们进行了一个高速循环的单元测试,以便能够重新创建读取不一致性。
  3. The test would read a specific row, update it, and read back the same row to assert the updated value. 测试将读取特定行,更新它,并读回同一行以断言更新的值。


Given there are 3 ways to fetch data: 鉴于有3种方法可以获取数据:

  • mapper.load() mapper.load()
  • mapper.query() mapper.query()
  • mapper.scan() mapper.scan()


The "mapper.load()" uses the mapper's config that was set when the mapper was instantiated. “mapper.load()”使用映射器实例化时设置的映射器配置。 For example - mapper.withConsistentReads(ConsistentReads.CONSISTENT). 例如 - mapper.withConsistentReads(ConsistentReads.CONSISTENT)。

The "mapper.query()" and "mapper.scan()" do not respect the mapper's config, rather they only use the DynamoDBQueryExpression and DynamoDBScanExpression inputs to determine consistent reads. “mapper.query()”和“mapper.scan()”不尊重映射器的配置,而是仅使用DynamoDBQueryExpression和DynamoDBScanExpression输入来确定一致的读取。


ANSWER: You need to use expression.setConsistentRead(true), even if your mapper has .withConsistentReads(ConsistentReads.CONSISTENT). 解答:您需要使用expression.setConsistentRead(true),即使您的映射器具有.withConsistentReads(ConsistentReads.CONSISTENT)。 Like this... 像这样...

public PaginatedScanList<T> getAllRowsFor() {
    final DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
    scanExpression.setConsistentRead(true);
    return mapper.scan(myClass, scanExpression);
}

You can see the AWS Java source code here that shows the mapper's config is not used - com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper#createScanRequestFromExpression 您可以在此处看到AWS Java源代码,其中显示未使用映射器的配置 - com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper #createScanRequestFromExpression

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

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