I am trying to get all the items whose price is greater than a certain value,but not able to use the filter expressions correctly.
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.ItemCollection;
import com.amazonaws.services.dynamodbv2.document.QueryOutcome;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.PutItemRequest;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
public class QuerySample {
// Setting up the client
static AmazonDynamoDBClient db = new AmazonDynamoDBClient(
new ProfileCredentialsProvider());
// Setting up the DB
static DynamoDB dynamoDB = new DynamoDB(db);
public static void main(String a[]) {
// Setting up the Region
Region usWest = Region.getRegion(Regions.US_WEST_2);
db.setRegion(usWest);
Table table = dynamoDB.getTable("Thread");
SimpleDateFormat dt = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSS");
long time = (new Date()).getTime();
Date date = new Date();
date.setTime(time);
System.out.println("The date is " + date);
// ScanRequest scanRequest = new ScanRequest()
// .withTableName("sys_ping");
// ScanResult result = db.scan(scanRequest);
// for (Map<String, AttributeValue> item : result.getItems()){
// System.out.println(item);
// }
Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
expressionAttributeValues.put(":val", "19");
expressionAttributeValues.put(":val1",
new AttributeValue().withN("2000"));
ScanRequest scanRequest = new ScanRequest().withTableName(
"ProductCatalog").withFilterExpression("Price >= :val");
ScanResult result = db.scan(scanRequest);
for (Map<String, AttributeValue> item : result.getItems()) {
System.out.println(item);
}
}
}
It throws the following runtime exception
Exception in thread "main" com.amazonaws.AmazonServiceException: Invalid FilterExpression: An expression attribute value used in expression is not defined; attribute value: :val (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: FEQBP55SPJIT60JVFPVO6N6BLBVV4KQNSO5AEMVJF66Q9ASUAAJG)
You are misusing the API. Look at Map<String, Object> expressionAttributeValues
:val
and :val1
there. new ScanRequest()