2 réponses
- Le plus récent
- Le plus de votes
- La plupart des commentaires
3
I assume that you use the EnhancedClient and not the low level client, if so your request looks something like:
DynamoDbAsyncClient client = DynamoDbAsyncClient.builder() .region(Region.EU_WEST_1) .build(); DynamoDbEnhancedAsyncClient enhancedClient = DynamoDbEnhancedAsyncClient.builder() .dynamoDbClient(client) .build(); DynamoDbAsyncTable<Customer> customerTable = enhancedClient.table("Custmomers", TableSchema.fromBean(Customer.class)); QueryConditional condition = QueryConditional.sortBetween(Key.builder() .partitionValue(1958) .sortValue("1920") .build(), Key.builder() .partitionValue(1958) .sortValue("1950") .build()); QueryEnhancedRequest request = QueryEnhancedRequest.builder() .queryConditional(condition) .scanIndexForward(false) .build(); SdkPublisher<Page<Customer>> customers = customerTable.index("my-index").query(request);
PagePublisher<Customer> pages = PagePublisher.create(customers); CompletableFuture<Void> future = pages.subscribe(System.out::println); future.get();
List<Customer> myCustomers = new ArrayList<Customer>(); PagePublisher<Customer> pages = PagePublisher.create(customers); pages.items().subscribe(myCustomers::add); myCustomers.forEach(customer-> System.out.println("CustID: " + customer.getId()));
0
Hi,
You have a full example with AsyncClient to query DynamoDb by GSI in this post: https://nickolasfisher.com/blog/Query-a-DynamoDB-Global-Secondary-Index-in-Java
See final code section:
StepVerifier.create(Mono.fromFuture(dynamoDbAsyncClient.query(equals2012Query)))
.expectNextMatches(queryResponse ->
queryResponse.count() == 1
&& queryResponse.items().get(0).get(COLOR).s().equals("Blue")
&& queryResponse.items().get(0).get(MODEL).s().equals("Pixel 1")
)
.verifyComplete();
Best,
DIdier
Thanks! Your link helped me figure out how to query DD using GSI and using DynamoDbEnhancedAsyncClient.
Contenus pertinents
- demandé il y a 9 mois
- demandé il y a 2 ans
- demandé il y a 2 ans
- AWS OFFICIELA mis à jour il y a 9 mois
- AWS OFFICIELA mis à jour il y a un an
- AWS OFFICIELA mis à jour il y a 2 ans
- AWS OFFICIELA mis à jour il y a 4 mois
Thanks! How would you create dynamoDbClient in your example? I am creating dynamoDbAsyncClient like so and when I use it to get table, it doesn't seem to support?
@Bean public DynamoDbAsyncClient asyncDynamoDBClient() { Region regionEnum = Region.of(awsRegion); DynamoDbAsyncClient dynamoDbAsyncClient = DynamoDbAsyncClient.builder() .region(regionEnum) .credentialsProvider(DefaultCredentialsProvider.create()) .build(); return dynamoDbAsyncClient; } After I add and use this bean, getting table method now works: @Bean public DynamoDbEnhancedAsyncClient dynamoDbAsyncEnhancedClient() { DynamoDbAsyncClient dynamoDbAsyncClient = asyncDynamoDBClient(); DynamoDbEnhancedAsyncClient dynamoDbAsyncEnhancedClient = DynamoDbEnhancedAsyncClient.builder() .dynamoDbClient(dynamoDbAsyncClient) .build(); return dynamoDbAsyncEnhancedClient; }
Hi @Leeroy.Hannigan, can you please tell me how would I write the last line "SdkPublisher<Page<Customer>> customers = customerTable.index("my-index").query(request);" if I was expecting only one row back (single customer) from the query ? How would I get the Customer record back from the SdkPublisher in async way? Thanks
Added two methods to do this, but the bottom line is that Query will always expect many items, not one like GetItem. Thats why you get a PagePublisher.
How do I mark your answer as accepted answer?
Thats a question I don't know the answer to, i've not asked a question on here before. Should be a large button beside the upvote button?