Amazon DynamoDB is a fast, highly scalable, highly available, cost-effective, non-relational database service. Amazon DynamoDB removes traditional scalability limitations on data storage while maintaining low latency and predictable performance. The sample mobile application described here demonstrates how to store user preferences in Amazon DynamoDB. Because more and more people are using multiple mobile devices, connecting these devices to the cloud and storing user preferences in the cloud enables developers to provide a more uniform cross-device experience for their users.
This article shows sample code for both the iOS and Android platforms. The complete sample code and project files are included in the AWS SDKs for these mobile platforms. Links to the SDKs are available at the end of this article.
To use the sample app, you'll need to deploy a token vending machine (TVM). A TVM is a cloud-based application that manages AWS credentials for users of mobile applications. To deploy the TVM, you'll first need to obtain your own AWS credentials: an Access Key ID and Secret Key.
If you haven't already signed up for Amazon Web Services (AWS), you will need to do that first to get your AWS credentials. You can sign up for AWS here. After you sign up, you can retrieve your credentials at this page. The credentials will be used to set up the TVM to authenticate users of AWS mobile applications. Sample Java web applications are available here: Anonymous TVM and Identity TVM (This sample uses Anonymous TVM).
In Amazon DynamoDB, a database is a collection of tables. A table is a collection of items, and each item is a collection of attributes. For our app, we create a single table to store our list of users and their preferences. Each item in the table represents an individual user. Each item
has multiple attributes, which include the user's name and their preferences. Each item also has a hash key—in this case, userNo
—which is the primary key for the table.
The app demonstrates how to add and remove users, and modify and retrieve their preference data. The app also demonstrates how to create and delete Amazon DynamoDB tables.
In order to create an Amazon DynamoDB client, we must first register the mobile device with the token vending machine (TVM). For this sample, we use the Anonymous TVM to register the device. Then we store the UID and key returned by the TVM on the device.
RegisterDeviceRequest *request = [[[RegisterDeviceRequest alloc] initWithEndpoint:self.endpoint andUid:uid andKey:key usingSSL:self.useSSL] autorelease]; ResponseHandler *handler = [[[ResponseHandler alloc] init] autorelease]; response = [self processRequest:request responseHandler:handler]; if ( [response wasSuccessful]) { [AmazonKeyChainWrapper registerDeviceId:uid andKey:key]; } |
RegisterDeviceRequest registerDeviceRequest = new RegisterDeviceRequest( this.endpoint, this.useSSL, uid, key); ResponseHandler handler = new ResponseHandler(); response = this.processRequest(registerDeviceRequest, handler); if (response.requestWasSuccessful()) { AmazonSharedPreferencesWrapper.registerDeviceId( this.sharedPreferences, uid, key); } |
The following code demonstrates how to request that the TVM generate temporary credentials, and how to store the returned credentials on the device.
Request *request = [[[GetTokenRequest alloc] initWithEndpoint:self.endpoint andUid:uid andKey:key usingSSL:self.useSSL] autorelease]; ResponseHandler *handler = [[[GetTokenResponseHandler alloc] initWithKey:key] autorelease]; GetTokenResponse *response = (GetTokenResponse *)[self processRequest:request responseHandler:handler]; if ( [response wasSuccessful]) { [AmazonKeyChainWrapper storeCredentialsInKeyChain:response.accessKey secretKey:response.secretKey securityToken:response.securityToken expiration:response.expirationDate]; } |
Request getTokenRequest = new GetTokenRequest(this.endpoint, this.useSSL, uid, key); ResponseHandler handler = new GetTokenResponseHandler(key); GetTokenResponse getTokenResponse = (GetTokenResponse) this .processRequest(getTokenRequest, handler); if (getTokenResponse.requestWasSuccessful()) { AmazonSharedPreferencesWrapper.storeCredentialsInSharedPreferences( this.sharedPreferences, getTokenResponse.getAccessKey(), getTokenResponse.getSecretKey(), getTokenResponse.getSecurityToken(), getTokenResponse.getExpirationDate()); } |
To make service requests to Amazon DynamoDB, you need to instantiate an Amazon DynamoDB client. The code below shows how to create an Amazon DynamoDB client for iOS or Android using the stored temporary credentials from the TVM.
AmazonCredentials *credentials = [AmazonKeyChainWrapper getCredentialsFromKeyChain]; AmazonDynamoDBClient *ddb = [[AmazonDynamoDBClient alloc] initWithCredentials:credentials]; |
AWSCredentials credentials = AmazonSharedPreferencesWrapper .getCredentialsFromSharedPreferences(this.sharedPreferences); AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(credentials); |
Each user's preferences are stored as items in an Amazon DynamoDB table. The following code creates that table using the client we created above. Every Amazon DynamoDB table require a hash key. In this sample, we use userNo
as the hash key for the table.
DynamoDBKeySchemaElement *kse = [[[DynamoDBKeySchemaElement alloc] initWithAttributeName:@"userNo" andAttributeType:@"N"] autorelease]; DynamoDBKeySchema *ks = [[[DynamoDBKeySchema alloc] initWithHashKeyElement:kse] autorelease]; DynamoDBProvisionedThroughput *pt = [[[DynamoDBProvisionedThroughput alloc] init] autorelease]; pt.readCapacityUnits = [NSNumber numberWithInt:10]; pt.writeCapacityUnits = [NSNumber numberWithInt:5]; DynamoDBCreateTableRequest *request = [[DynamoDBCreateTableRequest alloc] initWithTableName:TEST_TABLE_NAME andKeySchema:ks andProvisionedThroughput:pt]; DynamoDBCreateTableResponse *response = [[AmazonClientManager ddb] createTable:request]; [request release]; |
AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager.ddb(); KeySchemaElement kse = new KeySchemaElement().withAttributeName( "userNo").withAttributeType(ScalarAttributeType.N); KeySchema ks = new KeySchema().withHashKeyElement(kse); ProvisionedThroughput pt = new ProvisionedThroughput() .withReadCapacityUnits(10l).withWriteCapacityUnits(5l); CreateTableRequest request = new CreateTableRequest() .withTableName(PropertyLoader.getInstance().getTestTableName()) .withKeySchema(ks).withProvisionedThroughput(pt); ddb.createTable(request); |
Before we can move to the next step (creating users), we must wait until the status of the tables is ACTIVE. To retrieve the status of the table, we use a describe table request. This request returns information about the table such as the name of the table, item count, creation date and time, and its status.
DynamoDBDescribeTableRequest *request = [[[DynamoDBDescribeTableRequest alloc] initWithTableName:TEST_TABLE_NAME] autorelease]; DynamoDBDescribeTableResponse *response = [[AmazonClientManager ddb] describeTable:request]; NSString *status = response.table.tableStatus; |
AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager.ddb(); DescribeTableRequest request = new DescribeTableRequest() .withTableName(PropertyLoader.getInstance() .getTestTableName()); DescribeTableResult result = ddb.describeTable(request); String status = result.getTable().getTableStatus(); |
For each user, we'll create an item in the table. An item is a collection of attribute/value pairs. For each item, we'll have three attributes: userNo
, firstName
and lastName
. These are added to a put item request in order to create the item.
NSMutableDictionary *userDic = [NSDictionary dictionaryWithObjectsAndKeys: [[[DynamoDBAttributeValue alloc] initWithN:[NSString stringWithFormat:@"%d", i]] autorelease], @"userNo", [[[DynamoDBAttributeValue alloc] initWithS:[Constants getRandomName]] autorelease], @"firstName", [[[DynamoDBAttributeValue alloc] initWithS:[Constants getRandomName]] autorelease], @"lastName", nil]; DynamoDBPutItemRequest *request = [[DynamoDBPutItemRequest alloc] initWithTableName:TEST_TABLE_NAME andItem:userDic]; [[AmazonClientManager ddb] putItem:request]; [request release]; |
HashMap<String, AttributeValue> item = new HashMap<String, AttributeValue>(); AttributeValue userNo = new AttributeValue().withN(String .valueOf(i)); item.put("userNo", userNo); AttributeValue firstName = new AttributeValue().withS(Constants .getRandomName()); item.put("firstName", firstName); AttributeValue lastName = new AttributeValue().withS(Constants .getRandomName()); item.put("lastName", lastName); PutItemRequest request = new PutItemRequest().withTableName( PropertyLoader.getInstance().getTestTableName()).withItem( item); ddb.putItem(request); |
To remove a user from the list simply means deleting the corresponding item from the table. We specify the item we wish to delete using the hash key for the item.
DynamoDBDeleteItemRequest *request = [[DynamoDBDeleteItemRequest alloc] initWithTableName:TEST_TABLE_NAME andKey:[[[DynamoDBKey alloc] initWithHashKeyElement:aPrimaryKey] autorelease]]; [[AmazonClientManager ddb] deleteItem:request]; [request release]; |
AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager.ddb(); Key primaryKey = new Key().withHashKeyElement(targetValue); DeleteItemRequest request = new DeleteItemRequest().withTableName( PropertyLoader.getInstance().getTestTableName()).withKey( primaryKey); ddb.deleteItem(request); |
We can retrieve a collection of users with a scan request. A scan request simply scans the table and returns the results in an undetermined order. Scan is an expensive operation and should be used with care to avoid disrupting your higher priority production traffic on the table. See the Amazon DynamoDB developer guide for more recommendations for safely using the Scan operation.
DynamoDBScanRequest *request = [[[DynamoDBScanRequest alloc] initWithTableName:TEST_TABLE_NAME] autorelease]; DynamoDBScanResponse *response = [[AmazonClientManager ddb] scan:request]; NSMutableArray *users = response.items; |
AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager.ddb(); ScanRequest request = new ScanRequest(); request.setTableName(PropertyLoader.getInstance().getTestTableName()); ScanResult result = ddb.scan(request); ArrayList<HashMap<String, AttributeValue>> users = (ArrayList<HashMap<String, AttributeValue>>) result.getItems(); |
Knowing a user's userNo
, the hash key of the table, it is easy to find the item for the user.
This next snippet shows how to get all the attributes for an item using the hash key.
DynamoDBGetItemRequest *request = [[DynamoDBGetItemRequest alloc] initWithTableName:TEST_TABLE_NAME andKey:[[[DynamoDBKey alloc] initWithHashKeyElement: [[[DynamoDBAttributeValue alloc] initWithN:[NSString stringWithFormat:@"%d", userNo]] autorelease]]autorelease]]; DynamoDBGetItemResponse *response = [[AmazonClientManager ddb] getItem:request]; [request release]; NSMutableDictionary *userPereferences = response.item; |
AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager.ddb(); AttributeValue userNoAttr = new AttributeValue().withN(String .valueOf(userNo)); Key primaryKey = new Key().withHashKeyElement(userNoAttr); GetItemRequest request = new GetItemRequest().withTableName( PropertyLoader.getInstance().getTestTableName()).withKey( primaryKey); GetItemResult result = ddb.getItem(request); HashMap<String, AttributeValue> userPreferences = (HashMap<String, AttributeValue>) result.getItem(); |
The hash key also makes it easy to update an attribute for an item.
DynamoDBAttributeValue *attr = [[DynamoDBAttributeValue alloc] initWithS:aValue]; DynamoDBAttributeValueUpdate *attrUpdate = [[DynamoDBAttributeValueUpdate alloc] initWithValue:attr andAction:@"PUT"]; [attr release]; DynamoDBUpdateItemRequest *request = [[DynamoDBUpdateItemRequest alloc] initWithTableName:TEST_TABLE_NAME andKey:[[[DynamoDBKey alloc] initWithHashKeyElement:aPrimaryKey] autorelease] andAttributeUpdates:[NSMutableDictionary dictionaryWithObject: attrUpdate forKey:aKey]]; [attrUpdate release]; [[AmazonClientManager ddb] updateItem:request]; [request release]; |
AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager.ddb(); AttributeValue av = new AttributeValue().withS(value); AttributeValueUpdate avu = new AttributeValueUpdate().withValue(av) .withAction(AttributeAction.PUT); Key primaryKey = new Key().withHashKeyElement(targetValue); HashMap<String, AttributeValueUpdate> updates = new HashMap<String, AttributeValueUpdate>(); updates.put(key, avu); UpdateItemRequest request = new UpdateItemRequest() .withTableName(PropertyLoader.getInstance().getTestTableName()) .withKey(primaryKey).withAttributeUpdates(updates); ddb.updateItem(request); |
The easiest way to remove all the user preference data is to delete the Amazon DynamoDB table. The following code show how.
DynamoDBDeleteTableRequest *request = [[DynamoDBDeleteTableRequest alloc] initWithTableName:TEST_TABLE_NAME]; [[AmazonClientManager ddb] deleteTable:request]; [request release]; |
AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager.ddb(); DeleteTableRequest request = new DeleteTableRequest() .withTableName(PropertyLoader.getInstance().getTestTableName()); ddb.deleteTable(request); |
The code in this article demonstrates how to use Amazon DynamoDB as a storage device for your mobile application. You can find more information about Amazon DynamoDB here.
Sample apps that include the code from this article are provided with both mobile SDKs. You can download the SDKs using the following links:
For more information about using AWS credentials with mobile applications see the following article:
Please feel free to ask questions or provide comments in the Mobile Development Forum.