使用过滤器扫描AWS DynamoDB

时间:2017-10-10 20:57:26

标签: amazon-web-services amazon-dynamodb

AWS文档很糟糕。

如何在我的表“apps”中扫描DynamoDB以获取app_code =“TEST”的记录?

以下代码无效:

AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
                .withRegion(Regions.US_EAST_1)
                .build(); 


        Map<String, AttributeValue> expressionAttributeValues = 
                new HashMap<String, AttributeValue>();
            expressionAttributeValues.put(":val", new AttributeValue().withN("0")); 

        ScanRequest scanRequest = new ScanRequest()
                .withTableName("apps")
                .withFilterExpression("app_code = TEST")
                .withProjectionExpression("Id")
                .withExpressionAttributeValues(expressionAttributeValues);

        ScanResult scanResult = client.scan(scanRequest);
        for (Map<String, AttributeValue> item : scanResult.getItems()) {
            System.out.println(item);
        }

以下是我的错误:

Exception in thread "main" com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: Value provided in ExpressionAttributeValues unused in expressions: keys: {:val} (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: UNASODL7AHEETORAJ1Q1AF2EE3VV4KQNSO5AEMVJF66Q9ASUAAJG)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1638)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1303)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1055)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:743)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:717)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:667)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.doInvoke(AmazonDynamoDBClient.java:2186)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:2162)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.executeScan(AmazonDynamoDBClient.java:1678)
    at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.scan(AmazonDynamoDBClient.java:1654)
    at com.amazonaws.samples.Sessions.main(Sessions.java:46)

2 个答案:

答案 0 :(得分:0)

请将FilterExpression中的TEST更改为:val占位符。

ScanRequest scanRequest = new ScanRequest()
          .withTableName("apps")
          .withFilterExpression("app_code = :val")
          .withProjectionExpression("Id")
          .withExpressionAttributeValues(expressionAttributeValues);

答案 1 :(得分:0)

这是适用于Java 2.x的AWS开发工具包的版本:

DynamoDbClient client = DynamoDbClient.builder().region(Region.US_EAST_1).build();
Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();

expressionAttributeValues.put(":val", AttributeValue.builder().n("0").build());

ScanRequest request = ScanRequest
                        .builder()
                        .tableName("apps")
                        .filterExpression("app_code = :val")
                        .projectionExpression("Id")
                        .expressionAttributeValues(expressionAttributeValues)
                        .build();
ScanIterable response = client.scanPaginator(request);

for (ScanResponse page : response) {
    for (Map<String, AttributeValue> item : page.items()) {
        // Consume item
        System.out.println(item);
    }
}