.net 如何使用PocoDynamo创建全局二级索引?

3wabscal  于 2023-03-09  发布在  .NET
关注(0)|答案(1)|浏览(169)

这里是PocoDynamo的文档页面。
我创建了OrderCostGlobalIndex类并注册了Order类,如文档中所示,使用以下代码:

db.RegisterTable<Order>();
db.InitSchema();

但是,当我在AWS中运行此命令创建表时,表没有索引。如何使用PocoDynamo而不是AWS控制台创建全局二级索引?

cvxl0en2

cvxl0en21#

下面是我很久以前使用Poco创建的一个示例:

using PocoDynamo;
using Amazon.DynamoDBv2.DataModel;

// Define the POCO class for your table
[DynamoDBTable("MyTable")]
public class MyTableItem
{
    [DynamoDBHashKey("MyTableItemId")]
    public string Id { get; set; }

    [DynamoDBProperty("MyTableItemName")]
    public string Name { get; set; }

    [DynamoDBGlobalSecondaryIndexRangeKey("MyTableItemDateIndex")]
    public DateTime Date { get; set; }
}

// Initialize the DynamoDB client and PocoDynamo context
var client = new AmazonDynamoDBClient();
var context = new PocoDynamo(client);

// Define the properties for the global secondary index
var gsi = new GlobalSecondaryIndex
{
    IndexName = "MyTableItemDateIndex",
    KeySchema = new List<KeySchemaElement>
    {
        new KeySchemaElement("MyTableItemId", KeyType.HASH),
        new KeySchemaElement("MyTableItemDateIndex", KeyType.RANGE)
    },
    Projection = new Projection { ProjectionType = ProjectionType.ALL },
    ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 5, WriteCapacityUnits = 5 }
};

// Create the table with the global secondary index
context.CreateTablesIfNotExists<MyTableItem>(
    new CreateTableRequest
    {
        ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 5, WriteCapacityUnits = 5 },
        GlobalSecondaryIndexes = new List<GlobalSecondaryIndex> { gsi }
    });

相关问题