Search code examples
javaamazon-dynamodbamazon-dynamodb-local

Query condition missed key schema element(Service: DynamoDb


public class DynamoDBExample {
    public static void main(String[] args) {

        cred Cred = new cred();
        var akey = Cred.getkey();
        var aid = Cred.getid();
        var region = Region.AP_SOUTH_1;

        AwsCredentials Credentials = AwsBasicCredentials.create(akey, aid);
        var dynamoDBClient = DynamoDbClient.builder()
                .region(region)
                .endpointOverride(URI.create("http://localhost:8000"))
                .credentialsProvider(StaticCredentialsProvider.create(Credentials))
                .build();

        String startDate = "2023-01-01";
        String endDate = "2023-06-30";

        Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
        expressionAttributeValues.put(":startDate", AttributeValue.builder().s(startDate).build());
        expressionAttributeValues.put(":endDate", AttributeValue.builder().s(endDate).build());

        var queryRequest = QueryRequest.builder()
                .tableName("IA_REJECTION")
                .keyConditionExpression("REJECTION_DATE BETWEEN :startDate AND :endDate")
                .expressionAttributeValues(expressionAttributeValues)
                .build();

        var starttime = System.currentTimeMillis();
        QueryResponse response = dynamoDBClient.query(queryRequest);
        var endtime = System.currentTimeMillis();

        response.items().forEach(item -> {
            item.forEach((key, value) -> System.out.println(key + " --> " + value));
            System.out.println();
        });

        var timetaken = endtime - starttime;
        System.out.println("Total Time Taken To Run This Code: " + timetaken + "ms");
    }
}

i want to ressolve this issure i have a iapk -partision key and rejection no as a sort based on that i dont know use that.


Solution

  • You cannot do a Query using between on a partition key:

    .keyConditionExpression("REJECTION_DATE BETWEEN :startDate AND :endDate")
    

    If REJECTION_DATE is a non-key attribute then you must do a Scan and FilterExpression to obtain all of the items which have dates between your provided values.

    https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ScanJavaDocumentAPI.html