And*_*xon 6 asp.net amazon-dynamodb
我有一个dynamoDB表,其架构如下:
var request = new CreateTableRequest
{
TableName = tableName,
KeySchema = new List<KeySchemaElement>
{
new KeySchemaElement("CompanyId", KeyType.HASH),
new KeySchemaElement("Timestamp", KeyType.RANGE)
},
AttributeDefinitions = new List<AttributeDefinition>
{
new AttributeDefinition("CompanyId", ScalarAttributeType.S),
new AttributeDefinition("Timestamp", ScalarAttributeType.N),
new AttributeDefinition("UserId", ScalarAttributeType.S)
},
GlobalSecondaryIndexes = new List<GlobalSecondaryIndex>
{
new GlobalSecondaryIndex
{
IndexName = "UserIndex",
KeySchema = new List<KeySchemaElement>
{
new KeySchemaElement("UserId", KeyType.HASH),
new KeySchemaElement("Timestamp", KeyType.RANGE)
},
Projection = new Projection {ProjectionType = "ALL"},
ProvisionedThroughput = new ProvisionedThroughput(5, 6)
}
},
ProvisionedThroughput = new ProvisionedThroughput(5, 6)
};
Run Code Online (Sandbox Code Playgroud)
我可以成功查询主键,如下所示:
var client = new AmazonDynamoDBClient();
using (var context = new DynamoDBContext(client))
{
var sortKeyValues = new List<object>{minTimestamp};
result = await context.QueryAsync<AuditLogEntry>(companyId, QueryOperator.GreaterThanOrEqual, sortKeyValues,
new DynamoDBOperationConfig {OverrideTableName = TableName}).GetRemainingAsync();
}
Run Code Online (Sandbox Code Playgroud)
我可以查询全局二级索引,而不对范围键有任何约束,如下所示:
var client = new AmazonDynamoDBClient();
using (var context = new DynamoDBContext(client))
{
result = await context.QueryAsync<AuditLogEntry>(userId, new DynamoDBOperationConfig {OverrideTableName = TableName, IndexName = indexName})
.GetRemainingAsync();
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用范围键约束查询索引时:
var client = new AmazonDynamoDBClient();
using (var context = new DynamoDBContext(client))
{
var sortKeyValues = new List<object> {minTimestamp};
result = await context.QueryAsync<AuditLogEntry>(userId, QueryOperator.GreaterThan, sortKeyValues, new DynamoDBOperationConfig {OverrideTableName = TableName, IndexName = indexName}).GetRemainingAsync();
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Exception thrown: 'System.InvalidOperationException' in AWSSDK.DynamoDBv2.dll
Additional information: Local Secondary Index range key conditions are used but no index could be inferred from model. Specified index name = UserIndex
Run Code Online (Sandbox Code Playgroud)
谷歌搜索这个错误并没有对这个问题有任何启发.对本地二级索引的引用让我感到困惑,因为我使用的是全局索引,但我看不出我的代码有什么问题.
我已经能够通过直接在AmazonDynamoDBClient上查询而不是使用DynamoDBContext来查询查询,但我真的很想了解我做错了什么并且能够使用DynamoDBContext.
任何想法,将不胜感激.
在 AuditLogEntry 的模型定义中,您需要使用属性 - [DynamoDBGlobalSecondaryIndexRangeKey] 和/或 [DynamoDBGlobalSecondaryIndexHashKey] 来装饰属于全局二级索引的属性。下面的例子。
public class AuditLogEntry {
// other properties ...
[DynamoDBProperty("UserId")]
[DynamoDBGlobalSecondaryIndexHashKey("UserIndex")]
public string UserId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
740 次 |
| 最近记录: |