我有一个具有以下架构的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)
};

我可以成功查询主键,如下所示:
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();
}

而且我可以查询全局二级索引,而对范围键没有任何约束,如下所示:
var client = new AmazonDynamoDBClient();
using (var context = new DynamoDBContext(client))
{
    result = await context.QueryAsync<AuditLogEntry>(userId, new DynamoDBOperationConfig {OverrideTableName = TableName, IndexName = indexName})
        .GetRemainingAsync();
}

但是,当我尝试使用范围键约束查询索引时:
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();
}

我收到以下错误:
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

谷歌搜索此错误尚未发现任何问题。对局部二级索引的引用使我感到困惑,因为我使用的是全局索引,但是我看不出我的代码有什么问题。

我可以直接在AmazonDynamoDBClient上查询而不是使用DynamoDBContext来使查询正常工作,但是我真的很想了解我做错了什么,并且能够使用DynamoDBContext。

任何想法,将不胜感激。

最佳答案

在AuditLogEntry的模型定义中,您需要使用[DynamoDBGlobalSecondaryIndexRangeKey]和[DynamoDBGlobalSecondaryIndexRangeKey]属性来装饰属于全局二级索引的属性。

关于asp.net - .net通过DynamoDBContext查询DynamoDB中的全局二级索引,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39564851/

10-11 06:44