gpt4 book ai didi

asp.net - .net 通过 DynamoDBContext 查询 DynamoDB 中的全局二级索引

转载 作者:行者123 更新时间:2023-12-04 16:06:37 26 4
gpt4 key购买 nike

我有一个 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] 和或 [DynamoDBGlobalSecondaryIndexHashKey]

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

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