[英]How to update a value for reserved keyword in Dynamo DB. Error: Attribute name is a reserved keyword; reserved keyword: data
[英]error Attribute name is a reserved keyword
我的 dynamoDb 中有数据,例如
我的数据库使用 priceId 作为 PrimaryKey 和 symbol 作为 sortKey 和其他是属性
我尝试使用此代码
Table table = dynamoDB.getTable(tableName);
System.out.println("runFirstTime For Search Data");
String Symbols = "EURUSD";
String time = "2020-06-10 06:08:07";
try{
Item item = table.getItem("symbol", Symbols, "Time", time, "symbol, Price, Time", null);
System.out.println("Displaying retrieved items...");
String price = item.getString("Price");
System.out.println(price);
System.out.println(item.toJSONPretty());
}catch (Exception e) {
System.err.println("Cannot retrieve items.");
System.err.println(e.getMessage());
}
但我遇到了一个错误
Invalid ProjectionExpression: Attribute name is a reserved keyword; reserved keyword: Time (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 5QP69C4BA99JP3LKTKIL8NHB7RVV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null)
我已经更新了我的数据库结构,将符号作为primaryKey,priceId是sortKey,其他是属性:
当我使用此代码时
Table table = dynamoDB.getTable(tableName);
System.out.println("runFirstTime For Search Data");
String Symbols = "EURUSD";
String time = "2020-06-10 09:12:07";
try{
//GetItemSpec spec = new GetItemSpec().withPrimaryKey("symbol", Symbols).
Item item = table.getItem("symbol", Symbols, "savetime", time, "symbol, Price, savetime", null);
System.out.println("Displaying retrieved items...");
String price = item.getString("Price");
System.out.println(price);
System.out.println(item.toJSONPretty());
}catch (Exception e) {
System.err.println("Cannot retrieve items.");
System.err.println(e.getMessage());
}
我收到如下错误: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: KFCMB4GVB2QC7PAQD6AN5CE5HRVV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null)
我的问题是:1.为什么会出现这样的错误? 2. 如果我想得到所有的价格,如何使用一个叫做符号的主键和一个叫做保存时间的属性来编码或设置我的数据库? 3. 任何如何从 dynamoDb 中检索数据的示例
您可以在此处查看保留名称列表: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html
在此处查看 java 文档: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ScanJavaDocumentAPI.ZFC35FDC70D5FC69D269883A822CA7A
关于 Q1:您应该使用ExpressionAttributeNames
,这是防止名称与保留关键字发生冲突的正确工具。
我不知道Java API,所以这里只是命令行工具(并使用localstack):
awslocal dynamodb query --table-name YourTableName --index-name yourIndexName \
--key-condition-expression '#time= :time' \
--expression-attribute-values '{":time":{"S":"2015-02-18T20:27:36.165Z"}}' \
--expression-attribute-names '{"#time":"time"}' \
--return-consumed-capacity TOTAL
尽管这个答案可能并不完美,但它肯定会为您提供足够的指导如何在不迁移数据库的情况下解决 Q1!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.