jeudi 8 janvier 2015

How to sort DynamoDB Hashkey in ascending order in Android

I'm working on an Android application where I'm creating a DynamoDB table with a "Number" Hashkey.



public static void createTable() {

Log.d(TAG, "Create table called");

AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager
.ddb();

KeySchemaElement kse = new KeySchemaElement().withAttributeName(
"Tid").withKeyType(KeyType.HASH);
AttributeDefinition ad = new AttributeDefinition().withAttributeName(
"Tid").withAttributeType(ScalarAttributeType.N);

ProvisionedThroughput pt = new ProvisionedThroughput()
.withReadCapacityUnits(10l).withWriteCapacityUnits(5l);

CreateTableRequest request = new CreateTableRequest()
.withTableName(Constants.TEST_TABLE_NAME)
.withKeySchema(kse).withAttributeDefinitions(ad)
.withProvisionedThroughput(pt);

try {
Log.d(TAG, "Sending Create table request");
ddb.createTable(request);
Log.d(TAG, "Create request response successfully recieved");
} catch (AmazonServiceException ex) {
Log.e(TAG, "Error sending create table request", ex);
UserPreferenceDemoActivity.clientManager
.wipeCredentialsOnAuthError(ex);
}
insertUsers();
}


I'm then calling the insertUsers() method in the end, where I'm adding three Key values (800,815,830) into the created table:



public static void insertUsers() {
AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager
.ddb();
DynamoDBMapper mapper = new DynamoDBMapper(ddb);

try {
for (int i = 1; i <= 3; i++) {
UserPreference userPreference = new UserPreference();

if (i == 1)
{
userPreference.setTid(800);
}
else if (i == 2)
{
userPreference.setTid(815);
}
else
{
userPreference.setTid(830);
}

Log.d(TAG, "Inserting Tid and Dage");
mapper.save(userPreference);
Log.d(TAG, "Tid and Dage inserted");
}
} catch (AmazonServiceException ex) {
Log.e(TAG, "Error inserting users");
UserPreferenceDemoActivity.clientManager
.wipeCredentialsOnAuthError(ex);
}
}


But the ordering in AWS DynamoDB Service is:


enter image description here


How do I sort the table in ascending order, so I can obtain the ordering like : 800,815,830





Aucun commentaire:

Enregistrer un commentaire