我正在尝试基于一些过滤器从DynamoDB获取数据。 (例如:给我一条记录,其中productNumber和colorwayNumber为'A','B'。)以下是代码段:
AmazonDynamoDB amazonDynamoDB = DynamoDBClient.getInstance().getConnection();
Map<String, HashMap<String, String>> attrVal = new HashMap<String, HashMap<String, String>>();
Map<String,String> expressionAttributesNames = new HashMap<>();
expressionAttributesNames.put("#colorwayNumber","colorwayNumber");
expressionAttributesNames.put("#productNumber","productNumber");
Map<String,AttributeValue> expressionAttributeValues = new HashMap<>();
expressionAttributeValues.put(":colorwayNumber",new AttributeValue().withS(colorwayID));
expressionAttributeValues.put(":productNumber",new AttributeValue().withS(productNumber));
QueryRequest queryRequest = new QueryRequest()
.withTableName(ConverseConstants.PLM_PRODUCT_TABLE)
.withKeyConditionExpression("#colorwayNumber = :colorwayNumber")
.withKeyConditionExpression("#productNumber = :productNumber")
.withExpressionAttributeNames(expressionAttributesNames)
.withExpressionAttributeValues(expressionAttributeValues);
QueryResult queryResult1 = amazonDynamoDB.query(queryRequest);
List<Map<String, AttributeValue>> results1 = queryResult1.getItems();
以下是例外:
由以下原因引起:com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException:ExpressionAttributeNames中提供的未在表达式中使用的值:密钥:{#colorwayNumber}(服务:AmazonDynamoDBv2;状态代码:400;错误代码:ValidationException;
希望我提供了足够的信息。谢谢!
最佳答案
问题似乎在这里:
.withKeyConditionExpression("#colorwayNumber = :colorwayNumber")
.withKeyConditionExpression("#productNumber = :productNumber")
在这里,您用第二个(
#colorwayNumber = :colorwayNumber
)覆盖第一个表达式(#productNumber = :productNumber
)。这样,colorwayNumber
实际上在表达式中未使用。