2 回答
TA貢獻1847條經驗 獲得超7個贊
在DynamoDB我試圖使用是一個簡單的包裝器AmazonDynamoDB,提供了一個稍微不同的API。使用AmazonDynamoDB代替使這個函數的實現更容易,它應該看起來像這樣(請原諒糟糕的Java代碼,我實際上是用Scala編寫的):
public Boolean isEmpty(AmazonDynamoDB database, String tableName) = {
ScanRequest scanRequest = new ScanRequest().withTableName(tableName).withLimit(1);
return database.scan(scanRequest).getCount == 0;
}
或者,在 Scala 中:
def isEmpty(database: AmazonDynamoDB, tableName: String): Boolean = {
val scanRequest = new ScanRequest().withTableName(tableName).withLimit(1)
database.scan(scanRequest).getCount == 0
}
TA貢獻1796條經驗 獲得超4個贊
我不知道如何在 Java 中做到這一點,但它必須類似于 Javascript:
const params = {
TableName: tableName,
Limit: 1, // `Limit` is the most important parameter.
// The scan will not scan the whole table,
// it will only visit one item and then return.
// Very efficient!
};
// Execute the scan, whatever the syntax is...
const result = await (new AWS.DynamoDB.DocumentClient().scan(params).promise());
// Check the response
if (result.Count > 0) return false; // the table is **not** empty
return true; // the table is empty
在 Java 中,代碼應該是類似的...隨意詢問細節不夠清楚。
添加回答
舉報
