在下面的代码中,语言过滤条件是可选的,无论它可以包含数据还是null。如果它包含null,则dynamodb会引发一些错误。如何克服这个问题?
地图expressionAttributesNames = new HashMap <>(); expressionAttributesNames.put(“#Language”,“ Language”);
Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();
//expressionAttributeValues.put(":docType", new AttributeValue().withS("cp"));
expressionAttributeValues.put(":docName", new AttributeValue().withS("data part"));
//expressionAttributeValues.put(":docKey", new AttributeValue().withS("xxx yyy"));
expressionAttributeValues.put(":language", new AttributeValue().withS(null));
DynamoDBQueryExpression<Tools> queryExpression = new DynamoDBQueryExpression<PuToolsbtools>()
.withKeyConditionExpression(keyConditionExpression)
.withExpressionAttributeNames(expressionAttributesNames)
.withExpressionAttributeValues(expressionAttributeValues)
.withFilterExpression("#Language = :language"); List<Toolsools> tools = mapper.query(Tools.class, queryExpression);
例外是
ExpressionAttributeValues包含无效值:提供的AttributeValue为空,必须正好包含键:language(Service:AmazonDynamoDBv2; Status Code:400; Error Code:ValidationException;
答案 0 :(得分:1)
如果language
过滤器表达式是可选的,则仅当language
的值不为null时,才应将其包括在查询中。
.
.
.
if (language != null) {
queryExpression.withFilterExpression("#Language = :language");
expressionAttributeValues.put(":language", new AttributeValue().withS(language));
expressionAttributesNames.put("#Language", "Language");
}
List<Tools> tools = mapper.query(Pubtools.class, queryExpression);