我在DynamoDB中有一个带有用户的表(部分键=键,排序键=否:]
key isActive
user1是]
user2为假
... ...
在我的代码中,我需要返回状态为非活动(isActive = false)的下一个用户。如果需要解决方案的话,什么是最好的方法呢?
我编写了有效的代码,但由于扫描和过滤器的表达,我不确定这是一个好的解决方案:
public String getFreeUser() throws IOException {
Table table = dynamoDB.getTable("usersTableName");
ScanSpec spec = new ScanSpec()
.withFilterExpression("isActive = :is_active")
.withValueMap(new ValueMap().withBoolean(":is_active", false));
ItemCollection<ScanOutcome> items = table.scan(spec);
Iterator<Item> iterator = items.iterator();
Item item = null;
while (iterator.hasNext()) {
item = iterator.next();
try {
UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey(new PrimaryKey("key", item.getString("key")))
.withUpdateExpression("set #ian=:is_active_new")
.withConditionExpression("isActive = :is_active_old")
.withNameMap(new NameMap()
.with("#ian", "isActive"))
.withValueMap(new ValueMap()
.withBoolean(":is_active_old", false)
.withBoolean(":is_active_new", true))
.withReturnValues(ReturnValue.ALL_OLD);
UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
return outcome.getItem().getString("key");
} catch (Exception e) {
}
}
throw new IOException("No active users were found");
}
我在DynamoDB中有一个与我的用户一起使用的表(部分键=键,排序键=否):键isActive user1 true user2 false ...在我的代码中,我需要返回下一个具有状态的用户...
GSI +查询==良好