【问题标题】:.net Querying a Global Secondary Index in DynamoDB via DynamoDBContext.net 通过 DynamoDBContext 查询 DynamoDB 中的全局二级索引
【发布时间】:2016-09-19 03:17:16
【问题描述】:

我有一个 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。

任何想法都将不胜感激。

【问题讨论】:

    标签: asp.net amazon-dynamodb


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多