DynamoDB - 如何检查现有表是空还是非空

我正在尝试实现一个函数,该函数给定 DynamoDB 和我知道数据库中存在的表的名称,确定该表是否为空。

我希望签名在 Java 中如下所示:

public Boolean isEmpty(DynamoDB database, String tableName) = ???

就本问题而言,假设该表有一个主键,该主键由名为“UserId”的单个整数类型属性组成。

我知道可以使用扫描查看表格,但我 (a) 不知道表达式会是什么样子,并且 (b) 需要将其限制为单个项目,以便我们不在非空的情况下,不必扫描整个表(可能很大)。

编辑:

我应该AmazonDynamoDB在我的构造函数中使用 an而不是 aDynamoDB吗?前者有一个scan方法ScanRequest- 您可以使用它轻松设置限制.limit(1)- 而对于后者,我会做类似的事情database.getTable(tableName).scan(...),但是这个扫描需要一个ScanSpec我不清楚如何设置限制的方法。


翻过高山走不出你
浏览 423回答 2
2回答

aluckdog

在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}

SMILET

我不知道如何在 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 responseif (result.Count > 0) return false; // the table is **not** empty return true; // the table is empty在 Java 中,代码应该是类似的...随意询问细节不够清楚。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java