【问题标题】:Query Amazon DynamoDB for specific result查询 Amazon DynamoDB 以获取特定结果
【发布时间】:2014-12-18 02:09:20
【问题描述】:

我正在使用 DynamoDB SDK v2。我可以扫描数据库,但是我正在努力查询它以获取特定结果。我不知道我是数据库设置错误还是查询错误。

当我使用时

- (void)getTableRow:(NSString *)rangeKey {
          AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
            [[dynamoDBObjectMapper load:[SonicUserTableRow class]
                                hashKey:@"UserId"
                               rangeKey:rangeKey] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {
                if (!task.error) {

                   SonicUserTableRow *tableRow = task.result;

                   NSLog (@"Table Row %@", tableRow);

            } else {
               //Catch Error

            }
            return nil;
        }];
}

我使用[self getTableRow:@" ENTER UNIQUE USER ID "];调用它

但是我得到了错误

The provided key element does not match the schema

我该如何解决这个问题?

注意我使用的是新的 Amazon SDK v2。

我的数据库

哈希和范围键

SonicUserTableRow .m

+ (NSString *)dynamoDBTableName {
    return @"SonicUsers";
}

+ (NSString *)hashKeyAttribute {
    return @"UserId";
}

+ (NSString *)rangeKeyAttribute {
    return @"Username";
}

SonicUserTableRow .h

@interface SonicUserTableRow : AWSDynamoDBModel <AWSDynamoDBModeling>

@property (nonatomic, strong) NSString *UserId;
@property (nonatomic, strong) NSString *Username;
@property (nonatomic, strong) NSString *profileImage;
@property (nonatomic, strong) NSString *active;
@property (nonatomic, strong) NSString *email;
@property (nonatomic, strong) NSString *flags;
@property (nonatomic, strong) NSString *attuned;

我在数据库中有 UserId 值 UserIdString: us-east-1:4ae0a93a-59a6-45fd-80c8-ff6b99f1d152,所以我尝试调用例如:[self getTableRow:@"us-east-1:4ae0a93a-59a6-45fd-80c8-ff6b99f1d152"];

【问题讨论】:

    标签: ios amazon-web-services amazon-dynamodb


    【解决方案1】:

    如果DropId 是您表的哈希键,您可以使用- load:hashKey:rangeKey: 加载单个项目。

    【讨论】:

    • 我们的samples GitHub repo 中有一个 DynamoDB 对象映射器示例。
    • 即使在 github 上也很难做到这一点。用我所做的和赏金更新了问题
    • 您应该共享SonicUserTableRow 的定义(.h 和.m 文件)。可能设置不正确。
    • 您应该转到 AWS 管理控制台中的 详细信息 视图,并确保您看到的是 Primary Hash Key: hashKeyAttribute (String)Primary Range Key: rangeKey (String)。这些需要与+ hashKeyAttribute+ rangeKeyAttribute返回的值对应。
    • 我将它们更改为与 AWS 控制台中的匹配,现在我在返回的字段中得到一个空值(尽管错误消失了),即使我传递了一个我知道的 ID。我觉得自己像个白痴。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    相关资源
    最近更新 更多