![](/img/trans.png)
[英]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.