2

I have below two test cases implemented to test DynamoDB enhanced library.

Test case 1: Use Get Item on table "digital_form", with PK = "FORM#ABC123" and SK = "INFO#ABC123". which can return result

    @Test
    public void testGetItemWithPKSK() throws ExecutionException, InterruptedException {
        DynamoDbAsyncTable<DigitalFormDao> digitalformTable = dynamoDbEnhancedAsyncClient
                .table("digital_form", TableSchema.fromBean(DigitalFormDao.class));

        DigitalFormDao form = digitalformTable.getItem(
                Key.builder().partitionValue("FORM#ABC123").sortValue("INFO#ABC123").build()).get();

        System.out.println(form.getSk());
    }

Test case 2: Use Query on same table, with PK= "FORM#ABC123" and SK begin with "INFO". Supposely it will return set of result includes test case 1. However, no result is return.

    @Test
    public void testQueryWithPKandSKBegin(){
        DynamoDbAsyncTable<DigitalFormDao> digitalformTable = dynamoDbEnhancedAsyncClient
                .table("digital_form", TableSchema.fromBean(DigitalFormDao.class));

        PagePublisher<DigitalFormDao> digitalForms = digitalformTable.query(
                r -> r.queryConditional(
                    sortBeginsWith(k -> k.partitionValue("FORM#ABC123").sortValue("INFO"))));
        
        AtomicInteger atomicInteger = new AtomicInteger();
        atomicInteger.set(0);

        digitalForms.subscribe(page -> {
            DigitalFormDao digitalFormDao = (DigitalFormDao) page.items().get(atomicInteger.get());
            System.out.println(digitalFormDao.getSk());
            atomicInteger.incrementAndGet();
        });
    }

Is there some thing wrong in my Query statement?

jellycsc
  • 10,904
  • 2
  • 15
  • 32
toblerones
  • 21
  • 1
  • 3

1 Answers1

0

Does this work?

public static void queryTableSortKeyBetween(DynamoDbEnhancedClient enhancedClient) {

        try {
            DynamoDbTable<Customer> mappedTable =
                    enhancedClient.table("DigitalForm", TableSchema.fromBean(DigitalFormDao.class));

            // Querying the sort key Name between two values
            Key key = Key.builder().partitionValue("FORM#ABC123").sortValue("INFO").build();

            QueryConditional queryConditional = QueryConditional.sortBeginsWith(key);

            PageIterable<DigitalFormDao> forms  =
                    mappedTable.query(r -> r.queryConditional(queryConditional));

            forms.stream()
                     .forEach(p -> p.items().forEach(item -> System.out.println(item.getCustName())));

        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        System.out.println("Done");
}

Harsh Verma
  • 529
  • 6
  • 10