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
질문됨 한 달 전68회 조회
2개 답변
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
전문가
답변함 한 달 전
profile picture
전문가
검토됨 한 달 전
  • 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
전문가
답변함 한 달 전
profile picture
전문가
검토됨 한 달 전
profile picture
전문가
검토됨 한 달 전
  • Thanks! Your link helped me figure out how to query DD using GSI and using DynamoDbEnhancedAsyncClient.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠