Dynamo db:无法使用filterExpression

时间:2017-10-01 11:59:14

标签: amazon-dynamodb

我是aws dynamodb的新手,所以请原谅任何愚蠢的错误。我试图从我的Activity表中获取两列。此外,我只想获取分区键以某些特定字符串开头的那些列。分区键的格式为activity_EnrolledStudentName。(例如Dance_studentName)所以我想从活动为Dance的表中获取所有这些项目。我试图使用以下查询:

public List<StudentDomain> getAllStudents(String activity) {
    List<StudentDomain> scanResult = null;
    DynamoDBUtil dynamoDBUtil = new DynamoDBUtil();
    AmazonDynamoDB dynamoDBClient = dynamoDBUtil.getDynamoDBClient();
    DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
    DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
    scanExpression.withProjectionExpression("studentId, ActivitySkills")
    .addFilterCondition(STUDENT_PRIMARY_KEY,
            new 
    Condition().withComparisonOperator(ComparisonOperator.BEGINS_WITH)
                    .withAttributeValueList(new 
    AttributeValue().withS(activity)));
    scanResult = mapper.scan(StudentDomain.class, scanExpression);
    return scanResult;

但是当我执行上面的查询时,我收到以下错误。

com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {ScanFilter} Expression parameters: {ProjectionExpression} (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: TMS27PABBC2BS3UU7LID731G0FVV4KQNSO5AEMVJF66Q9ASUAAJG)

任何人都可以建议我在哪里弄错了,否则我应该使用哪个查询?

1 个答案:

答案 0 :(得分:0)

目前还不完全清楚你想要实现的目标,但如果我理解正确,那么你就不需要扫描整个表的扫描操作,然后过滤结果。

DynamoDB dynamoDB = new DynamoDB(client);
Table table = dynamoDB.getTable("TableName");

QuerySpec spec = new QuerySpec()
    .withKeyConditionExpression("studentId = : ActivitySkills")

ItemCollection<QueryOutcome> items = table.query(spec);

Iterator<Item> iterator = items.iterator();
Item item = null;
while (iterator.hasNext()) {
   item = iterator.next();
   System.out.println(item.toJSONPretty());
}

您正在使用的过滤器表达式旨在用作辅助属性的过滤器,而不是范围或分区键。至少这是我对文档的解释

请阅读查询文档http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html