0
votes

In the below code, language filter condition is optional whether it may contains data or null. If it contains null means dynamodb throws some error. How to overcome this ?

Map<String,String> expressionAttributesNames = new HashMap<>(); expressionAttributesNames.put("#Language", "Language");

    Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();
    //expressionAttributeValues.put(":type", new AttributeValue().withS("test1"));
    expressionAttributeValues.put(":name", new AttributeValue().withS("data part"));
    //expressionAttributeValues.put(":key", new AttributeValue().withS("xxx yyy"));
    expressionAttributeValues.put(":language", new AttributeValue().withS(null));
    
    DynamoDBQueryExpression<Sample> queryExpression = new DynamoDBQueryExpression<Sample>()
            .withKeyConditionExpression(keyConditionExpression)
            .withExpressionAttributeNames(expressionAttributesNames)
            .withExpressionAttributeValues(expressionAttributeValues)
            .withFilterExpression("#Language = :language"); List<Sample> tools = mapper.query(Sample.class, queryExpression); 

Exception is,

ExpressionAttributeValues contains invalid value: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes for key :language (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException;

1

1 Answers

1
votes

If the language filter expression is optional, you should only include it in the query if the value for language is not null.

.
.
.
if (language != null) {
    queryExpression.withFilterExpression("#Language = :language");
    expressionAttributeValues.put(":language", new AttributeValue().withS(language));
    expressionAttributesNames.put("#Language", "Language");
}

List<Tools> tools = mapper.query(Sample.class, queryExpression);