Object modeling is a common practice in mobile applications. We present two methods for modeling objects backed by Amazon DynamoDB, the AWS Persistence Framework for Core Data and DynamoDBMapper. We cover the benefits and limitations of these two solutions and demonstrate sample applications built with both technologies.
5. @DynamoDBTable(tableName = ”UserPreference")
public class UserPreference {
@DynamoDBHashKey(attributeName = "userId")
public int getUserId() {
return userId;
}
@DynamoDBAttribute(attributeName = "firstName")
public String getFirstName() {
return firstName;
}
@DynamoDBAttribute(attributeName = "lastName")
public String getLastName() {
return lastName;
}
6.
7. // Create a client
AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(creds);
// Create a mapping of values
HashMap item = new HashMap();
AttributeValue userId = new AttributeValue().withN(1337);
item.put("userId", userId);
AttributeValue firstName = new AttributeValue().withS("Bob");
item.put("firstName", firstName);
AttributeValue lastName = new AttributeValue().withS("Kinney");
item.put("lastName", lastName);
// Put the item in DynamoDB
PutItemRequest request = new PutItemRequest()
.withTableName("Prefs").withItem(item);
ddb.putItem(request);
8. // create our client and mapper
AmazonDynamoDBClient ddb = new
AmazonDynamoDBClient(credentials);
DynamoDBMapper mapper = new DynamoDBMapper(ddb);
// create our object, set metadata
UserPreference up = new UserPreference();
up.setUserId(1337);
up.setFirstName(“Bob”);
up.setLastName(“Kinney”);
// save to Amazon DynamoDB
mapper.save(up);
// load the object by key
up = mapper.load(UserPreference.class, 1337);
26. Creating AWS Persistence Store
// Registers the AWSNSIncrementalStore
[NSPersistentStoreCoordinator
registerStoreClass:
[AWSPersistenceDynamoDBIncrementalStore class]
forStoreType:
AWSPersistenceDynamoDBIncrementalStoreType];
// Adds the store to the coordinator
[persistentStoreCoordinator
addPersistentStoreWithType:
AWSPersistenceDynamoDBIncrementalStoreType
configuration:nil URL:nil
options:options error:&error];