我正在尝试获取价格大于某个值但无法正确使用过滤器表达式的所有商品。
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);
}
}
}
它抛出以下运行时异常
线程“main”com.amazonaws.AmazonServiceException 中的异常:无效的FilterExpression:未定义表达式中使用的表达式属性值;属性值::val(服务:AmazonDynamoDBv2;状态代码:400;错误代码:ValidationException;请求 ID:FEQBP55SPJIT60JVFPVO6N6BLBVV4KQNSO5AEMVJF66Q9ASUAAJG)