2

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

2 Answers 2

2

Hash isn't ordered in DynamoDB. Your only workaround here is to Scan the whole table to code and sort in the application layer.

Sign up to request clarification or add additional context in comments.

Comments

0

From AWS Documentation:

DynamoDB builds an unordered hash index on the hash primary key attribute and a sorted range index on the range primary key attribute

Hope it help,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.