Querying DynamoDb by GSI using DynamoDbAsyncClient

0

Hi, I see most examples in aws website make use of either DynamoDbClient or DynamoDbEnhancedClient. can you please provide an example using DynamoDbAsyncClient to query the DB using a GSI index? This example here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSIJavaDocumentAPI.html#GSIJavaDocumentAPI.QueryAnIndex uses getTable to get the table and then the index but getTable method doesn't exist in the async client. Which method should I use? Thanks.

sjain
demandé il y a un mois74 vues
2 réponses
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()));
profile pictureAWS
EXPERT
répondu il y a un mois
profile picture
EXPERT
vérifié il y a un 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?

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

profile pictureAWS
EXPERT
répondu il y a un mois
profile picture
EXPERT
vérifié il y a un mois
profile picture
EXPERT
vérifié il y a un mois
  • Thanks! Your link helped me figure out how to query DD using GSI and using DynamoDbEnhancedAsyncClient.

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions