How to add DynamoDB scan result to object list?

I perform a check in the DynamoDB table, and I then need to add the appropriate attributes from the returned elements to the type list User( Userhas one constructor User(String uuid)). The code currently successfully checks the database and returns Listscan results. However, my iteration somehow returns null.

    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
    }
+3
source share
1 answer

I suggest you start using a modern and compact list iteration with For-Each Loop , which helps to avoid many common mistakes when using the old iteration style:

[...]

. , . : , . . :

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

, :

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

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

, , , toString() UserAccounts uuid, . , getKey() getUuidAttribute() @DynamoDBHashKey @DynamoDBAttribute, Class DynamoDBMapper, :

@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;
     }

     // ...
 }

, , :

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

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

All Articles