gpt4 book ai didi

ios - AWS Batch 请求按值数组过滤的多项

转载 作者:行者123 更新时间:2023-11-29 01:03:38 25 4
gpt4 key购买 nike

我想问一下 AWSDynamoDBScanExpression 的 filterExpression。问题:

我想在数据库中扫描一个表中的所有对象,其中一个参数(我们称之为 uniqueId)是存储在数组(所需 uniqueIds 的数组)中的值之一。

对于一个对象——很容易做到

AWSDynamoDBScanExpression *scanExpression = [[AWSDynamoDBScanExpression alloc] init];
scanExpression.expressionAttributeNames = @{
@"#P": [NSString stringWithFormat:@"%@", property]
};
scanExpression.filterExpression =@"#P = :val";
scanExpression.expressionAttributeValues = @{
@":val" : @"some uniqueID"
};

所以按照同样的逻辑,我想扫描数据库中的多个对象

 AWSDynamoDBScanExpression *scanExpression = [[AWSDynamoDBScanExpression alloc] init];
scanExpression.expressionAttributeNames = @{
@"#P": [NSString stringWithFormat:@"%@", property]
};
scanExpression.filterExpression = <WHAT SHOULD BE HERE, WHAT EXPRESSION>;
scanExpression.expressionAttributeValues = @{
@":val" : [@"some unique id 1",
@"some unique id 2",
@"some unique id 3"]
};

有什么方法可以改变 scanExpression.filterExpression 来实现这个吗?

编辑 1

不,我不确定 scan 是最好的解决方案。实际上,query 是最好的变体。

表的结构

enter image description here

#P = :val1 OR #P = :val2 make sense

EDIT2

这是一些更新:

AWSDynamoDBQueryExpression *query = [[AWSDynamoDBQueryExpression alloc] init];
NSMutableDictionary *dictionaryAttributes = [[NSMutableDictionary alloc] init];
NSString *expression = @"";
for (int i = 0; i < filteredHashValues.count; i++) {
NSString *variableName = [NSString stringWithFormat:@":val%i", i];
[dictionaryAttributes setValue:filteredHashValues[i] forKey:variableName];
expression = [expression stringByAppendingString:expression.length ? [NSString stringWithFormat:@"OR #P = %@ " , variableName] : [NSString stringWithFormat:@"#P = %@ " , variableName]];
}

query.indexName = @"uniqueId-index";
query.expressionAttributeNames = @{
@"#P": [NSString stringWithFormat:@"%@", @"uniqueId"]
};
query.filterExpression = expression;
query.expressionAttributeValues = dictionaryAttributes;

AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
[[dynamoDBObjectMapper query:className expression:query] continueWithBlock:^id _Nullable(AWSTask * _Nonnull task) {

if (task.result) {
AWSDynamoDBPaginatedOutput *output = task.result;
}
return nil;
}];

但是结果

Printing description of expression: #P = :val0 OR #P = :val1 OR #P = :val2 OR #P = :val3

Printing description of dictionaryAttributes: {
":val0" = "8A93A3EA-9FB9-4396-BBF6-D0BD3CBE6BE5";
":val1" = "08533EBA-D3E5-406C-8CDE-03EECCCA801B";
":val2" = "954AE402-336E-423D-BF03-7E8AED1446FE";
":val3" = "F683BDF8-0507-4218-9927-9F14D470E593"; }

Printing description of task->_error: Error `Domain=com.amazonaws.AWSDynamoDBErrorDomain Code=0 "(null)" UserInfo={__type=com.amazon.coral.validate#ValidationException, message=ExpressionAttributeValues contains invalid value: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes for key :awsddbomhashvalueplaceholder}

看起来 ExpressionAttributeValues 是空的 - 我做的都正确吗?

最佳答案

您确定要使用扫描表达式吗?这确实会消耗大量读取容量,并会破坏您的 dynamodb 性能:扫描会读取整个表,然后才应用过滤器并返回值。

如果您向我们提供实际的 dynamodb 表结构,我们也许可以讨论另一种解决方案。例如,一个解决方案可能是创建一个全局二级索引(如果最终一致读取对你来说没问题)或一个本地二级索引(连同其限制),并在你的值上使用一个范围键来过滤。这将允许您使用查询,这更好,并且建议作为最佳实践。

也就是说,您可以使用 AND 和 OR 运算符向过滤器添加条件,结果类似于“#P = :val1 OR #P = :val2”

希望对你有帮助

关于ios - AWS Batch 请求按值数组过滤的多项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36688688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com