如何在并发环境中获取DynamoDB中的下一个免费项目

问题描述 投票:0回答:1

我在DynamoDB中有一个带有用户的表(部分键=键,排序键=否:]

key isActive

user1是]

user2为假

... ...

在我的代码中,我需要返回状态为非活动(isActive = false)的下一个用户。如果需要解决方案的话,什么是最好的方法呢?

  1. 一张大桌子
  2. 当前环境
  3. 我编写了有效的代码,但由于扫描和过滤器的表达,我不确定这是一个好的解决方案:

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 ...在我的代码中,我需要返回下一个具有状态的用户...

java filter amazon-dynamodb dbscan
1个回答
0
投票

GSI +查询==良好

© www.soinside.com 2019 - 2024. All rights reserved.