如何将DynamoDB扫描结果添加到对象列表? [英] How to add a DynamoDB scan result to an object list?

查看:205
本文介绍了如何将DynamoDB扫描结果添加到对象列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对DynamoDB表执行扫描,然后我需要将返回项中的相应属性添加到类型用户)类型的列表中用户有一个构造函数 User(String uuid))。代码当前成功扫描数据库并返回扫描结果的 List 。但是我的迭代似乎由于某种原因返回null。

I am performing a scan on a DynamoDB table and I need to then add respective attributes from the returned items to a list of type User (User has a single constructor User(String uuid)). The code currently successfully scans the DB and returns a List of the scan results. However my iteration seems to return null for some reason.

    AmazonDynamoDBClient client = dynamoClient.getDynamoClient();
    DynamoDBMapper mapper = new DynamoDBMapper(client);

    try {
        DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();

        Map<String, Condition> scanFilter = new HashMap<String, Condition>();
        Condition scanCondition = 
            new Condition().withComparisonOperator(ComparisonOperator.NOT_NULL);
        scanFilter.put("uuid", scanCondition);
        scanExpression.setScanFilter(scanFilter);

        List scanResults = mapper.scan(UserAccounts.class, scanExpression);

        for (Iterator it = scanResults.iterator(); it.hasNext();) {
            //User user = (User) it.next();
            allUserSummary.add(new User(scanResults.get(1).toString()));
        }
    } catch (Exception e) {
        // TODO
    }


推荐答案

我建议您通过 For-Each循环,有助于避免在使用旧迭代样式时出现许多常见错误:

I suggest you start using the modern and compact list iteration by means of The For-Each Loop, which helps to avoid many common errors when using the old iteration style:


[...]

[...]

迭代器只是杂乱无章。此外,这是
错误的机会。迭代器变量在每个循环中出现三次:即
两次错误的机会。 for-each构造摆脱了
的混乱和错误的机会。以下是该示例如何使用for-each结构查看

The iterator is just clutter. Furthermore, it is an opportunity for error. The iterator variable occurs three times in each loop: that is two chances to get it wrong. The for-each construct gets rid of the clutter and the opportunity for error. Here is how the example looks with the for-each construct:

void cancelAll(Collection<TimerTask> c) {
    for (TimerTask t : c)
        t.cancel();
}


将此应用于您的用例会产生大致如下:

Applying this to your use case yields the following approximately:

    List<UserAccounts> scanResults = mapper.scan(UserAccounts.class, scanExpression);

    for (UserAccounts userAccounts : scanResults) {
        allUserSummary.add(new User(userAccounts.toString()));
    }

如果这不起作用,它可能暗示实际错误同样,在您的代码假定类 UserAccounts toString()以返回 uuid ,可能是也可能不是。通常的方法是使用 getKey() getUuidAttribute()方法和相应的注释 @DynamoDBHashKey @DynamoDBAttribute ,如类DynamoDBMapper ,例如:

In case this doesn't work already, it could hint towards the actual error as well, insofar your code assumes the toString() of class UserAccounts to return the uuid, which may or may not be the case. The usual approach is to have a getKey() or getUuidAttribute() method and respective annotations @DynamoDBHashKey or @DynamoDBAttribute, as shown in the example for Class DynamoDBMapper, e.g.:

@DynamoDBTable(tableName = "UserAccounts")
 public class UserAccounts{     
     private String key; // or uuid right away

     @DynamoDBHashKey
     public String getKey() {
         return key;
     }

     public void setKey(String key) {
         this.key = key;
     }

     // ...
 }

这显然会为您的示例产生以下结果:

This would obviously yield the following for your example:

    List<UserAccounts> scanResults = mapper.scan(UserAccounts.class, scanExpression);

    for (UserAccounts userAccounts : scanResults) {
        allUserSummary.add(new User(userAccounts.getKey()));
    }

这篇关于如何将DynamoDB扫描结果添加到对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆