Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

java - DynamoDB Enhanced - No result from query

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?

question from:https://stackoverflow.com/questions/65641712/dynamodb-enhanced-no-result-from-query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...