Reputation: 25296
I'd like to get ANY row in my DynamoDB table, but the table could be very big. I don't want to have the DB server return all rows to me and then I just get the first item in the set.
With JDBC I'd use a cursor which is just a "pointer" to row in the database. In DynamoDB I've been looking at the scan()
operation (https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html) but there are so many similar methods, and they vary between versions, and parameters that don't do what I was expecting that I'm really confused. I really don't want to submit JSON queries if there is a simple programmatic statement to do it.
What method should I be looking for to just get items one-by-one from client to server, rather than the server batching them up and returning multiple?
new DynamoDB(
AmazonDynamoDBClientBuilder.standard()
.withRegion(Regions.US_EAST_1.name())
.build())
.getTable("my_large_table")
// MaxResultSize just blindly returns rows, not matching rows. Since we have no filter criteria this is fine.
.scan(new ScanSpec().withMaxResultSize(1).withConsistentRead(true))
.firstPage()
.getLowLevelResult()
.getScanResult()
.getItems();
For now I'm using these but if it's changed in later versions I'd like to know the solution that minimizes future migration pain:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.11.127</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.127</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.127</version>
</dependency>
Upvotes: 1
Views: 392
Reputation: 14859
Just use the Limit Scan argument.
By default Scan will evaluate every item in a table. Using the limit operator will set "the maximum number of items to evaluate (not necessarily the number of matching items)".
So if you Scan a table with no filters, and Limit set to 1, you will always get one item returned (assuming there is at least one item).
Upvotes: 1