[英]how to query or search with one Primary Key and One Attribute in DynamoDb
我有這樣的數據庫:
Symbol 是 primaryKey,priceId 是 SortKey,其他是屬性 priceId,因為 sortkey 總是唯一的
我只有信息符號和保存時間
例如是
字符串 Symbols = "EURUSD"; String time = "2020-06-10 09:12:07";
我嘗試使用掃描,但我的代碼堆棧
Table table = dynamoDB.getTable(tableName);
System.out.println("runFirstTime For Search Data");
String Symbols = "EURUSD";
String time = "2020-06-10 09:12:07";
try{
Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
expressionAttributeValues.put(":savetime", new AttributeValue().withS(time));
expressionAttributeValues.put(":symbol", new AttributeValue().withS(Symbols));
ScanRequest scanRequest = new ScanRequest().withTableName(tableName).withFilterExpression("savetime = :savetime AND symbol = :symbol")
.withProjectionExpression("symbol, priceId, savetime, Price").withExpressionAttributeValues(expressionAttributeValues);
ScanResult result = client.scan(scanRequest);
for (Map<String, AttributeValue> item : result.getItems()) {
System.out.println(item);
}
}catch (Exception e) {
System.err.println("Cannot retrieve items.");
System.err.println(e.getMessage());
}
}
我的代碼堆棧在 log4j:WARN 找不到記錄器的附加程序
(com.amazonaws.AmazonWebServiceClient).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
runFirstTime For Search Data
我已經嘗試編碼使用這個
Table table = dynamoDB.getTable(tableName);
System.out.println("runFirstTime For Search Data");
String Symbols = "EURUSD";
String time = "2020-06-10 11:43:31";
String Prefix = "Onezero";
ItemCollection<QueryOutcome> items = null;
Iterator<Item> iterator = null;
Item item = null;
try{
QuerySpec spec = new QuerySpec().withProjectionExpression("symbol, priceId, Price, savetime")
.withKeyConditionExpression("symbol = :v_symbol and begin_with(priceId, :begin)")
.withFilterExpression("savetime = :v_savetime")
.withValueMap(new ValueMap()
.withString(":v_symbol", Symbols)
.withString("begin", Prefix)
.withString(":v_savetime", time));
items = table.query(spec);
iterator = items.iterator();
while (iterator.hasNext()) {
item = iterator.next();
System.out.println(item.getString("symbol") + ": " + item.getString("savetime") + ": "+ item.getString("Price"));
}
}catch (Exception e) {
System.err.println("Cannot retrieve items.");
System.err.println(e.getMessage());
}
}
但我仍然遇到這樣的錯誤
ExpressionAttributeValues contains invalid key: Syntax error; key: "begin" (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 5TTRE1Q7FBT5TLNL0MTTPV05K7VV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null)
為什么這樣? 可能是掃描錯誤。 那么什么最好用於從 1 個鍵和 1 個屬性使用 dynamoDb 進行搜索? 有什么例子嗎?
我已經嘗試使用 getitem 或掃描但仍然有問題
您似乎在這里混合了多個問題...
對於您的DynamoDB 查詢:我認為,您可能應該使用KeyConditionExpression
來過濾符號(因為那是您的分區鍵)並結合FilterExpression
以節省時間。 文檔提供了一些示例: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryingJavaDocumentAPI.html
請注意:DynamoDB 將加載與KeyConditionExpression
匹配的所有條目(即這是您將支付的負載),然后應用FilterExpression
。 但這是預期的行為,您只能通過您的鍵來限制查詢。
對於您的log4j警告,請在此處檢查問題的答案:
當我使用這段代碼時,這個問題就解決了
Table table = dynamoDB.getTable(tableName);
System.out.println("runFirstTime For Search Data");
String Symbols = "EURUSD";
String time = "2020-06-11 03:20:52";
String Prefix = "Onezero";
ItemCollection<QueryOutcome> items = null;
Iterator<Item> iterator = null;
Item item = null;
try{
QuerySpec spec = new QuerySpec().withProjectionExpression("symbol, priceId, Price, savetime")
.withKeyConditionExpression("symbol = :v_symbol and begins_with(priceId, :begin)")
.withFilterExpression("begins_with(savetime, :v_savetime)")
.withValueMap(new ValueMap()
.withString(":v_symbol", Symbols)
.withString(":begin", Prefix)
.withString(":v_savetime", time));
items = table.query(spec);
iterator = items.iterator();
while (iterator.hasNext()) {
item = iterator.next();
System.out.println(item.getString("symbol") + ": " + item.getString("savetime") + ": "+ item.getString("Price"));
}
}catch (Exception e) {
System.err.println("Cannot retrieve items.");
System.err.println(e.getMessage());
}
感謝大家的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.