2017-10-10 110 views
0

AWS文檔很糟糕。使用篩選器掃描AWS DynamoDB

如何在我的表「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) 
+1

您提供了屬性'val',但實際上並未在掃描中使用它。 DynamoDB正在試圖拯救你自己。以下是一個掃描示例:http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ScanJavaDocumentAPI.html。 – jarmod

回答

0

請更改FilterExpression的TEST:val佔位符。

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