如何使用 servicestack.aws PocoDynamo 批量获取物品?

How to batch get items using servicestack.aws PocoDynamo?

使用Amazon原生.net lib,batchget是这样的

var batch = context.CreateBatch<MyClass>();
batch.AddKey("hashkey1");
batch.AddKey("hashkey2");
batch.AddKey("hashkey3");
batch.Execute();
var result = batch.results;

现在我正在测试使用 servicestack.aws,但是我找不到如何使用它。我试过以下方法,都失败了。

//1st try
var q1 = db.FromQueryIndex<MyClass>(x => x.room_id == "hashkey1" || x.room_id == "hashkey2"||x.room_id == "hashkey3");
var result = db.Query(q1);

//2nd try
var result = db.GetItems<MyClass>(new string[]{"hashkey1","hashkey2","hashkey3"});

在这两种情况下,它都抛出了一个异常 附加信息:KeyConditionExpression 中使用的运算符无效:OR

请帮助我。谢谢!

使用 GetItems 应该如此 Live Example on Gistlyn:

public class MyClass
{
    public string Id { get; set; }
    public string Content { get; set; }
}

db.RegisterTable<MyClass>();

db.DeleteTable<MyClass>();  // Delete existing MyClass Table (if any)
db.InitSchema();         // Creates MyClass DynamoDB Table

var items = 5.Times(i => new MyClass { Id = $"hashkey{i}", Content = $"Content {i}" });
db.PutItems(items);

var dbItems = db.GetItems<MyClass>(new[]{ "hashkey1","hashkey2","hashkey3" });
"Saved Items: {0}".Print(dbItems.Dump());

如果您的项目同时具有哈希和范围键,您将需要使用 GetItems<T>(IEnumerable<DynamoId> ids) API,例如:

var dbItems = db.GetItems<MyClass>(new[]{
    new DynamoId("hashkey1","rangekey1"),
    new DynamoId("hashkey2","rangekey3"),
    new DynamoId("hashkey3","rangekey4"),
});

查询所有具有相同 HashKey 的项目

如果你想获取所有具有相同 HashKey 的项目,你需要 create a DynamoDB Query as seen with this Live Gistlyn Example:

var items = 5.Times(i => new MyClass { 
    Id = $"hashkey{i%2}", RangeKey = $"rangekey{i}", Content = $"Content {i}" });
db.PutItems(items);

var rows = db.FromQuery<MyClass>(x => x.Id == "hashkey1").Exec().ToArray();
rows.PrintDump();