如何使用servicestack.aws PocoDynamo批量获取项目?

时间:2016-08-31 11:27:44

标签: servicestack pocodynamo

使用Amazon原生.net lib,批处理就像这样

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

请帮帮我。谢谢!

1 个答案:

答案 0 :(得分:1)

使用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,如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();