如何在 Android 中按升序对 DynamoDB Hashkey 进行排序

How to sort DynamoDB Hashkey in ascending order in Android

我正在开发一个 Android 应用程序,我在其中创建一个带有 "Number" 哈希键的 DynamoDB table。

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();
}

然后我最后调用 insertUsers() 方法,我将三个键值 (800,815,830) 添加到创建的 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);
    }
} 

但是 AWS DynamoDB 服务中的顺序是:

如何按升序对 table 进行排序,以便获得如下顺序:800,815,830

哈希在 DynamoDB 中未排序。您唯一的解决方法是扫描整个 table 以在应用程序层中进行编码和排序。

来自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

希望对您有所帮助,